summaryrefslogtreecommitdiff
path: root/server/test/src/handlers/GeminiHandlerSpec.scala
blob: 6b177f89808b09db453354638055e77fd05d89c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package net.usebox.gemini.server.handlers

import java.net.URI

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import net.usebox.gemini.server._

class GeminiHandlerSpec extends AnyFlatSpec with Matchers {

  behavior of "handle"

  val handler = new GeminiHandler(TestData.conf)
  val handlerUserDir = new GeminiHandler(TestData.confUserDir)
  val handlerCgi = new GeminiHandler(TestData.cgiConf)

  def handleWith(
      geminiHandler: GeminiHandler,
      req: String,
      remoteAddr: String = "127.0.0.1"
  ): Response =
    geminiHandler.handle(req, URI.create(req), remoteAddr)

  it should "handle host case insensitive" in {
    handleWith(handler, "gemini://LOCALHOST/") should be(a[Success])
  }

  it should "return proxy request refused when the vhost is not found" in {
    handleWith(handler, "gemini://otherhost/") should be(
      a[ProxyRequestRefused]
    )
  }

  it should "return bad request when user info is present" in {
    handleWith(handler, "gemini://user@localhost/") should be(
      a[BadRequest]
    )
  }

  it should "return bad request when the path is out of root dir" in {
    handleWith(handler, "gemini://localhost/../../") should be(
      a[BadRequest]
    )
  }

  it should "redirect to normalize the URL" in {
    handleWith(handler, "gemini://localhost/./") should be(
      a[PermanentRedirect]
    )
  }

  it should "return not found if the path doesn't exist" in {
    handleWith(
      handler,
      "gemini://localhost/doesnotexist"
    ) should be(a[NotFound])
  }

  it should "return not found if a dot file" in {
    handleWith(handler, "gemini://localhost/.dotfile") should be(
      a[NotFound]
    )
  }

  it should "return success on reading file" in {
    handleWith(
      handler,
      "gemini://localhost/index.gmi"
    ) should matchPattern {
      case Success(_, "text/gemini", Some(_), 25L) =>
    }
  }

  it should "redirect and normalize request on a directory" in {
    handleWith(handler, "gemini://localhost/dir") should be(
      a[PermanentRedirect]
    )
  }

  it should "return an existing index file when requesting a directory" in {
    handleWith(handler, "gemini://localhost/") should matchPattern {
      case Success(_, "text/gemini", Some(_), 25L) =>
    }
  }

  it should "include gemini params for gemini MIME type" in {
    handleWith(
      new GeminiHandler(
        TestData.conf.copy(virtualHosts =
          List(TestData.conf.virtualHosts(0).copy(geminiParams = Some("test")))
        )
      ),
      "gemini://localhost/index.gmi"
    ) should matchPattern {
      case Success(_, "text/gemini; test", Some(_), 25L) =>
    }
  }

  behavior of "handler, directory listings"

  it should "return a directory listing if is enabled and no index" in {
    handleWith(handler, "gemini://localhost/dir/") should be(
      a[DirListing]
    )
  }

  it should "return a directory listing, directory listing flags: vhost flag false, directories flag true" in {
    handleWith(
      new GeminiHandler(
        ServiceConf.initConf(
          TestData.conf.copy(virtualHosts =
            List(
              TestData.conf
                .virtualHosts(0)
                .copy(
                  directoryListing = false,
                  directories = List(
                    Directory(
                      "dir/",
                      directoryListing = Some(true),
                      allowCgi = None
                    )
                  )
                )
            )
          )
        )
      ),
      "gemini://localhost/dir/"
    ) should be(a[DirListing])
  }

  it should "return not found with no index, directory listing flags: vhost flag true, directories flag false" in {
    handleWith(
      new GeminiHandler(
        ServiceConf.initConf(
          TestData.conf.copy(virtualHosts =
            List(
              TestData.conf
                .virtualHosts(0)
                .copy(
                  directoryListing = true,
                  directories = List(
                    Directory(
                      "dir/",
                      directoryListing = Some(false),
                      allowCgi = None
                    )
                  )
                )
            )
          )
        )
      ),
      "gemini://localhost/dir/"
    ) should be(a[NotFound])
  }

  it should "not apply directory listing override to subdirectories" in {
    handleWith(
      new GeminiHandler(
        ServiceConf.initConf(
          TestData.conf.copy(virtualHosts =
            List(
              TestData.conf
                .virtualHosts(0)
                .copy(
                  directoryListing = false,
                  directories = List(
                    Directory(
                      "dir/",
                      directoryListing = Some(true),
                      allowCgi = None
                    )
                  )
                )
            )
          )
        )
      ),
      "gemini://localhost/dir/sub/"
    ) should be(
      a[NotFound]
    )
  }

  it should "return not found if directory listing is not enabled and no index" in {
    handleWith(
      new GeminiHandler(
        TestData.conf.copy(virtualHosts =
          List(TestData.conf.virtualHosts(0).copy(directoryListing = false))
        )
      ),
      "gemini://localhost/dir/"
    ) should be(a[NotFound])
  }

  behavior of "handler, user directories"

  it should "return success on reading file" in {
    handleWith(
      handlerUserDir,
      "gemini://localhost/~username/index.gmi"
    ) should matchPattern {
      case Success(_, "text/gemini", Some(_), 38L) =>
    }
  }

  it should "return redirect accessing the user directory without ending slash" in {
    handleWith(handlerUserDir, "gemini://localhost/~username") should be(
      a[PermanentRedirect]
    )
  }

  it should "return success accessing the user directory index" in {
    handleWith(
      handlerUserDir,
      "gemini://localhost/~username/"
    ) should matchPattern {
      case Success(_, "text/gemini", Some(_), 38L) =>
    }
  }

  it should "return bad request trying to exit the root directory" in {
    handleWith(handlerUserDir, "gemini://localhost/~username/../../") should be(
      a[BadRequest]
    )
  }

  it should "return redirect to the virtual host root when leaving the user dir" in {
    handleWith(handlerUserDir, "gemini://localhost/~username/../") should be(
      a[PermanentRedirect]
    )
  }

  it should "not translate root if used an invalid user pattern" in {
    handleWith(handlerUserDir, "gemini://localhost/~username../") should be(
      a[NotFound]
    )

    handleWith(handlerUserDir, "gemini://localhost/~0invalid/") should be(
      a[NotFound]
    )
  }

  it should "not translate root if no user directory path was provided" in {
    handleWith(
      new GeminiHandler(
        TestData.conf.copy(virtualHosts =
          List(
            TestData.conf
              .virtualHosts(0)
              .copy(userDirectories = true)
          )
        )
      ),
      "gemini://localhost/~username/",
      "127.0.0.1"
    ) should be(a[NotFound])
  }

  it should "not execute a CGI if the target resource is not executable" in {
    handleWith(
      handlerCgi,
      "gemini://localhost/dir/file.txt"
    ) should matchPattern {
      case Success(_, "text/plain", Some(_), 5L) =>
    }
  }

  it should "not execute a CGI if the target resource is a directory" in {
    handleWith(handlerCgi, "gemini://localhost/dir/sub/") should be(
      a[DirListing]
    )
  }

  it should "not apply allow CGI to subdirectories" in {
    handleWith(
      handlerCgi,
      "gemini://localhost/dir/sub/cgi"
    ) should matchPattern {
      case Success(_, "text/plain", Some(_), 72) =>
    }
  }

  it should "execute a CGI" in {
    val cgi = handleWith(handlerCgi, "gemini://localhost/dir/cgi")
      .asInstanceOf[Cgi]

    cgi.status should be(20)
    cgi.meta should be("text/gemini")
    cgi.body should include("GATEWAY_INTERFACE=CGI/1.1")
  }

  it should "execute a CGI: empty parameters, host and port" in {
    handleWith(
      handlerCgi,
      "gemini://localhost/dir/cgi"
    ) should matchPattern {
      case Cgi(
            _,
            _,
            "",
            "",
            "cgi",
            TestData.host,
            TestData.portStr,
            _,
            _
          ) =>
    }
  }

  it should "execute a CGI: host is case-insensitive and the value in the conf is used" in {
    handleWith(
      handlerCgi,
      "gemini://LOCALHOST/dir/cgi"
    ) should matchPattern {
      case Cgi(
            _,
            _,
            "",
            "",
            "cgi",
            TestData.host,
            TestData.portStr,
            _,
            _
          ) =>
    }
  }

  it should "execute a CGI: query string" in {
    handleWith(
      handlerCgi,
      "gemini://localhost/dir/cgi?query&string"
    ) should matchPattern {
      case Cgi(
            _,
            _,
            "query&string",
            "",
            "cgi",
            TestData.host,
            TestData.portStr,
            _,
            _
          ) =>
    }
  }

  it should "execute a CGI: path info" in {
    handleWith(
      handlerCgi,
      "gemini://localhost/dir/cgi/path/info"
    ) should matchPattern {
      case Cgi(
            _,
            _,
            "",
            "/path/info",
            "cgi",
            TestData.host,
            TestData.portStr,
            _,
            _
          ) =>
    }
  }

  it should "execute a CGI: query string and path info" in {
    handleWith(
      handlerCgi,
      "gemini://localhost/dir/cgi/path/info?query=string"
    ) should matchPattern {
      case Cgi(
            _,
            _,
            "query=string",
            "/path/info",
            "cgi",
            TestData.host,
            TestData.portStr,
            _,
            _
          ) =>
    }
  }

  it should "not execute an executable if allow CGI is off" in {
    handleWith(
      handler,
      "gemini://localhost/dir/cgi"
    ) should matchPattern {
      case Success(_, "text/plain", Some(_), _) =>
    }
  }

  it should "response with an error if the CGI exits with non 0" in {
    val bad =
      handleWith(handlerCgi, "gemini://localhost/dir/bad-cgi")
        .asInstanceOf[Cgi]

    val meta = "Error executing CGI"
    bad.status should be(50)
    bad.meta should be(meta)
    bad.body should include(meta)
  }

  it should "return a response with an error if the CGI exits with non 0" in {
    val bad =
      handleWith(handlerCgi, "gemini://localhost/dir/bad-cgi")
        .asInstanceOf[Cgi]

    val meta = "Error executing CGI"
    bad.status should be(50)
    bad.meta should be(meta)
    bad.body should include(meta)
  }

  it should "return a response with an error if the CGI response is invalid" in {
    val bad =
      handleWith(handlerCgi, "gemini://localhost/dir/bad-response")
        .asInstanceOf[Cgi]

    val meta = "Invalid response from CGI"
    bad.status should be(40)
    bad.meta should be(meta)
    bad.body should include(meta)
  }

  it should "environment variables are optional" in {
    handleWith(
      handlerCgi,
      "gemini://localhost/dir/cgi/"
    ) should matchPattern {
      case Cgi(
            _,
            _,
            _,
            _,
            "cgi",
            TestData.host,
            TestData.portStr,
            _,
            m
          ) if m == Map() =>
    }
  }

  it should "pass environment variables to the CGI" in {
    handleWith(
      new GeminiHandler(TestData.cgiEnvConf),
      "gemini://localhost/dir/cgi/"
    ) should matchPattern {
      case Cgi(
            _,
            _,
            _,
            _,
            "cgi",
            TestData.host,
            TestData.portStr,
            _,
            m
          ) if m == Map("env1" -> "value") =>
    }
  }

  it should "execute a CGI with the environment variables" in {
    val cgi = handleWith(
      new GeminiHandler(TestData.cgiEnvConf),
      "gemini://localhost/dir/cgi"
    ).asInstanceOf[Cgi]

    cgi.status should be(20)
    cgi.meta should be("text/gemini")
    cgi.body should include("env1=value")
  }

  it should "execute a CGI when it is the index document" in {
    val cgi = handleWith(
      new GeminiHandler(TestData.cgiIndexConf),
      "gemini://localhost/dir/"
    ).asInstanceOf[Cgi]

    cgi.status should be(20)
    cgi.meta should be("text/gemini")
    cgi.body should include("GATEWAY_INTERFACE=CGI/1.1")
  }

  it should "execute a CGI when it is the index document (full name)" in {
    val cgi = handleWith(
      new GeminiHandler(TestData.cgiIndexConf),
      "gemini://localhost/dir/cgi"
    ).asInstanceOf[Cgi]

    cgi.status should be(20)
    cgi.meta should be("text/gemini")
    cgi.body should include("GATEWAY_INTERFACE=CGI/1.1")
  }

  it should "execute a CGI when it is the index document (full name, path info)" in {
    val cgi = handleWith(
      new GeminiHandler(TestData.cgiIndexConf),
      "gemini://localhost/dir/cgi/path/info"
    ).asInstanceOf[Cgi]

    cgi.status should be(20)
    cgi.meta should be("text/gemini")
    cgi.body should include("GATEWAY_INTERFACE=CGI/1.1")
    cgi.body should include("PATH_INFO=/path/info")
  }

  it should "resolve CGI directories from more to less specific" in {
    // issue: https://gitlab.com/reidrac/spacebeans/-/issues/2
    val cgi = handleWith(
      new GeminiHandler(TestData.cgiPrefConf),
      "gemini://localhost/dir/sub/cgiOk/path/info"
    ).asInstanceOf[Cgi]

    cgi.status should be(20)
    cgi.meta should be("text/gemini")
    cgi.body should include("GATEWAY_INTERFACE=CGI/1.1")
    cgi.body should include("PATH_INFO=/path/info")
  }
}