Only forward valid URLs in open-urls-in-browser

This is unlikely to be exploited since the combinator only allows a
single argument and is not parsed by any shell, but it seems like a good
thing to ensure that only valid non-local URLs will be forwarded to
$BROWSER.
This commit is contained in:
Alex David 2025-10-04 14:34:48 -07:00
commit e45e8a7ccb
3 changed files with 41 additions and 5 deletions

View file

@ -93,7 +93,7 @@ in
''; '';
} }
) )
} "$1" } "''${1-}"
''; '';
}) })
]) ])

View file

@ -15,9 +15,15 @@ in
all URLs to the `$BROWSER` outside of the jail. This way the jailed all URLs to the `$BROWSER` outside of the jail. This way the jailed
program can launch your browser, even if it has a subset of the program can launch your browser, even if it has a subset of the
permissions your browser has. permissions your browser has.
Only URLs beginning with `http(s)://` will be forwarded.
''; '';
impl = compose [ impl = compose [
(jail-to-host-channel "browserchannel" ''"$BROWSER" "$1"'')
(set-env "BROWSER" "browserchannel") (set-env "BROWSER" "browserchannel")
(jail-to-host-channel "browserchannel" ''
if [[ "$1" =~ ^https://|^http:// ]]; then
"$BROWSER" "$1"
fi
'')
]; ];
} }

View file

@ -121,14 +121,44 @@ spec = parallel $ inTestM $ do
fakeBrowser = sh '' fakeBrowser = sh ''
echo "got url: $1" >> #{tmpDir </> "fake-browser"} echo "got url: $1" >> #{tmpDir </> "fake-browser"}
''; '';
jailed = jail "test" (sh ''$BROWSER https://example.org'') (c: [ jailed = jail "test" (sh ''$BROWSER "$@"'') (c: [
c.open-urls-in-browser c.open-urls-in-browser
]); ]);
in sh '' in sh ''
BROWSER=${lib.getExe fakeBrowser} ${lib.getExe jailed} export BROWSER=${lib.getExe fakeBrowser}
${lib.getExe jailed} "https://example.org"
${lib.getExe jailed} "http://example.org"
'' ''
|] |]
liftIO $ readFile (tmpDir </> "fake-browser") `shouldReturn` "got url: https://example.org\n" liftIO $
readFile (tmpDir </> "fake-browser")
`shouldReturn` unindent
[i|
got url: https://example.org
got url: http://example.org
|]
it "only allows urs that start with http(s)://" $ do
tmpDir <- getTestDir
void $
runNixDrv
[i|
let
fakeBrowser = sh ''
echo "got url: $1" >> #{tmpDir </> "fake-browser"}
'';
jailed = jail "test" (sh ''$BROWSER "$@"'') (c: [
c.open-urls-in-browser
]);
in sh ''
export BROWSER=${lib.getExe fakeBrowser}
${lib.getExe jailed} "file:///some/path"
${lib.getExe jailed} "--headless"
${lib.getExe jailed} ""
${lib.getExe jailed}
''
|]
liftIO $ doesFileExist (tmpDir </> "fake-browser") `shouldReturn` False
describe "readonly-from-path-var" $ do describe "readonly-from-path-var" $ do
forM_ [":", " "] $ \separator -> forM_ [":", " "] $ \separator ->