From 21f8ae5db73be083340bb65b4e1ab7ec3abb41b7 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 2 Jan 2022 22:18:57 +0000 Subject: Resolve CGI directories from more to less specific Closes issue #2. --- .../src/net/usebox/gemini/server/ServiceConf.scala | 6 +++- server/test/resources/dir/sub/cgiOk | 4 +++ server/test/src/ServerSpec.scala | 39 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100755 server/test/resources/dir/sub/cgiOk (limited to 'server') 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 -- cgit v1.2.3