aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-22 11:41:01 +0000
committertv <tv@krebsco.de>2026-11-22 11:41:01 +0000
commit5d92f09978a2e7b5f40becb5215a28f2407f4bb4 (patch)
tree5930cfd1010c83e3d259305a9e05ca13f5a4b3ce
parent5089201bd78c82687377a50ef33878d35407d23b (diff)
host: path rules match whole path segments
A path rule was a plain string prefix of the request's path, so https://api.github.com/search/issues, meant for that API endpoint (with whatever query), also allowed /search/issuesomethingelse, and the only way around it, /search/issues/, didn't allow the endpoint itself. Now a rule's path allows itself and the paths below it, /search/issues/..., but not /search/issuesx; one ending in "/" still allows whatever starts with it. The query was never part of the match, and still isn't. The requests' checks and POST /check share the code now.
-rw-r--r--README.md1
-rw-r--r--modules/host/default.nix4
-rw-r--r--modules/host/egress.lua53
3 files changed, 29 insertions, 29 deletions
diff --git a/README.md b/README.md
index 709aeb6..9746906 100644
--- a/README.md
+++ b/README.md
@@ -220,6 +220,7 @@ Each user has any number of named whitelists, in
fc-egress list
fc-egress allow https://cache.nixos.org # the domain and its subdomains
fc-egress allow https://github.com/NixOS/ # only paths starting with /NixOS/
+fc-egress allow https://api.github.com/search/issues # that path and below, any query
fc-egress allow tcp://192.0.2.7:22 # plain TCP to that address and port
fc-egress allow tcp://git.example.org:22 # or name (resolved by the host)
fc-egress allow udp://192.0.2.0/24:5000-5100
diff --git a/modules/host/default.nix b/modules/host/default.nix
index 26120b8..6e20219 100644
--- a/modules/host/default.nix
+++ b/modules/host/default.nix
@@ -325,7 +325,9 @@ ${lib.concatMapStrings (v: " \"${v.user} ${v.vm}\") vip=${v.ip} vip6=${
echo "VM is a running VM's name (run-firecracker-vm --name) or tap;"
echo " -v VM means the list it uses"
echo "RULE is https://DOMAIN (with subdomains, not decrypted)"
- echo " or https://DOMAIN/PATH-PREFIX (decrypted to check the path)"
+ echo " or https://DOMAIN/PATH (decrypted to check the path: PATH itself,"
+ echo " and below it, PATH/..., with any query; with PATH ending in /,"
+ echo " whatever starts with it)"
echo " or tcp://ADDRESS[/PREFIXLEN]:PORT[-PORT] (likewise udp://...)"
echo " or tcp://[ADDRESS6[/PREFIXLEN]]:PORT[-PORT] (IPv6, not port 443)"
echo " or tcp://NAME:PORT[-PORT] (resolved by the host)"
diff --git a/modules/host/egress.lua b/modules/host/egress.lua
index 3741b71..fe626b2 100644
--- a/modules/host/egress.lua
+++ b/modules/host/egress.lua
@@ -9,6 +9,9 @@
-- through without decrypting
-- https://example.com/foo/ only requests whose path starts with /foo/,
-- decrypted to check that
+-- https://example.com/foo only /foo itself and paths below it (/foo/...;
+-- not /foo-bar), with any query,
+-- decrypted to check that
-- (tcp:// and udp:// rules are for nftables, see default.nix.)
-- A domain with any whole-domain rule is passed through, whatever path
-- rules it has. Everything else gets denied.
@@ -114,6 +117,25 @@ local function policy(host, guest, name)
end
end
+-- Whether a (percent-decoded) path is allowed by the path rules: no "." or
+-- ".." segments, and a rule's path itself or one below it, whole segments
+-- only: /search/issues allows /search/issues and /search/issues/..., not
+-- /search/issuesx; a rule ending in "/" allows what starts with it.
+local function path_allowed(path, prefixes)
+ for segment in path:gmatch("[^/]+") do
+ if segment == "." or segment == ".." then
+ return false
+ end
+ end
+ for _, p in ipairs(prefixes) do
+ if path:sub(1, #p) == p and
+ (#path == #p or p:sub(-1) == "/" or path:sub(#p + 1, #p + 1) == "/") then
+ return true
+ end
+ end
+ return false
+end
+
-- stream, preread: pass through, decrypt, or close
function _M.preread()
local sni = ngx.var.ssl_preread_server_name
@@ -209,22 +231,8 @@ function _M.access()
local host = (ngx.var.host or ""):lower()
local path = ngx.unescape_uri((ngx.var.request_uri or ""):match("^[^?]*"))
local p = policy(sni, guest, name)
- local ok = type(p) == "table" and host == sni and path:sub(1, 1) == "/"
- if ok then
- for segment in path:gmatch("[^/]+") do
- if segment == "." or segment == ".." then
- ok = false
- end
- end
- end
- if ok then
- ok = false
- for _, prefix in ipairs(p) do
- if path:sub(1, #prefix) == prefix then
- ok = true
- end
- end
- end
+ local ok = type(p) == "table" and host == sni and path:sub(1, 1) == "/" and
+ path_allowed(path, p)
ngx.var.egress_verdict = ok and "ALLOW" or "DENY"
if not ok then
return ngx.exit(403)
@@ -443,18 +451,7 @@ local function allowed(guest, name, uri)
if type(p) ~= "table" then
return false
end
- path = ngx.unescape_uri(path ~= "" and path or "/")
- for segment in path:gmatch("[^/]+") do
- if segment == "." or segment == ".." then
- return false
- end
- end
- for _, prefix in ipairs(p) do
- if path:sub(1, #prefix) == prefix then
- return true
- end
- end
- return false
+ return path_allowed(ngx.unescape_uri(path ~= "" and path or "/"), p)
elseif scheme == "tcp" or scheme == "udp" then
local host, port = rest:match("^(.+):(%d+)$")
if not host then