summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-01-02 22:18:57 +0000
committerJuan J. Martinez <jjm@usebox.net>2022-01-02 22:24:48 +0000
commit21f8ae5db73be083340bb65b4e1ab7ec3abb41b7 (patch)
treeb3421907d2133b06d0800a1c6dc0c6a47abff64d
parentb5b419ad0a89b36e136f4509a59a145f991e833d (diff)
downloadspacebeans-21f8ae5db73be083340bb65b4e1ab7ec3abb41b7.tar.gz
spacebeans-21f8ae5db73be083340bb65b4e1ab7ec3abb41b7.zip
Resolve CGI directories from more to less specific
Closes issue #2.
-rw-r--r--server/src/net/usebox/gemini/server/ServiceConf.scala6
-rwxr-xr-xserver/test/resources/dir/sub/cgiOk4
-rw-r--r--server/test/src/ServerSpec.scala39
3 files changed, 48 insertions, 1 deletions
diff --git a/server/src/net/usebox/gemini/server/ServiceConf.scala b/server/src/net/usebox/gemini/server/ServiceConf.scala
index 43786af..ec082cf 100644
--- a/server/src/net/usebox/gemini/server/ServiceConf.scala
+++ b/server/src/net/usebox/gemini/server/ServiceConf.scala
@@ -44,10 +44,14 @@ object VirtualHost {
def getCgi(path: Path): Option[Path] =
vhost.directories
+ .filter(_.allowCgi == Some(true))
+ .sortWith {
+ case (d1, d2) => d1.path.length() > d2.path.length()
+ }
.find(d =>
path.startsWith(
d.path
- ) && path.toString != d.path && d.allowCgi == Some(true)
+ ) && path.toString != d.path
)
.collect {
case d =>
diff --git a/server/test/resources/dir/sub/cgiOk b/server/test/resources/dir/sub/cgiOk
new file mode 100755
index 0000000..05a3e53
--- /dev/null
+++ b/server/test/resources/dir/sub/cgiOk
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo -e "20 text/gemini\r\n"
+env
diff --git a/server/test/src/ServerSpec.scala b/server/test/src/ServerSpec.scala
index dd6bf20..7d8c1fa 100644
--- a/server/test/src/ServerSpec.scala
+++ b/server/test/src/ServerSpec.scala
@@ -692,6 +692,21 @@ class ServerSpec extends AnyFlatSpec with Matchers {
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 = Server(TestData.cgiPrefConf)
+ .handleReq(
+ "gemini://localhost/dir/sub/cgiOk/path/info",
+ "127.0.0.1"
+ )
+ .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")
+ }
+
object TestData {
val host = "localhost"
@@ -733,6 +748,30 @@ class ServerSpec extends AnyFlatSpec with Matchers {
)
)
+ val cgiPrefConf = ServiceConf.initConf(
+ conf.copy(virtualHosts =
+ List(
+ conf
+ .virtualHosts(0)
+ .copy(
+ directoryListing = true,
+ directories = List(
+ Directory(
+ "dir/",
+ directoryListing = Some(false),
+ allowCgi = Some(true)
+ ),
+ Directory(
+ "dir/sub/",
+ directoryListing = Some(false),
+ allowCgi = Some(true)
+ )
+ )
+ )
+ )
+ )
+ )
+
val cgiEnvConf = cgiConf.copy(virtualHosts =
List(
cgiConf