diff --git a/lib/combinators/jail-to-host-channel.nix b/lib/combinators/jail-to-host-channel.nix index d85eccf..909a39e 100644 --- a/lib/combinators/jail-to-host-channel.nix +++ b/lib/combinators/jail-to-host-channel.nix @@ -93,7 +93,7 @@ in ''; } ) - } "$1" + } "''${1-}" ''; }) ]) diff --git a/lib/combinators/open-urls-in-browser.nix b/lib/combinators/open-urls-in-browser.nix index d4cc72e..1db4038 100644 --- a/lib/combinators/open-urls-in-browser.nix +++ b/lib/combinators/open-urls-in-browser.nix @@ -15,9 +15,15 @@ in 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 permissions your browser has. + + Only URLs beginning with `http(s)://` will be forwarded. ''; impl = compose [ - (jail-to-host-channel "browserchannel" ''"$BROWSER" "$1"'') (set-env "BROWSER" "browserchannel") + (jail-to-host-channel "browserchannel" '' + if [[ "$1" =~ ^https://|^http:// ]]; then + "$BROWSER" "$1" + fi + '') ]; } diff --git a/tests/spec/CombinatorsSpec.hs b/tests/spec/CombinatorsSpec.hs index d9b7685..9b12eb1 100644 --- a/tests/spec/CombinatorsSpec.hs +++ b/tests/spec/CombinatorsSpec.hs @@ -121,14 +121,44 @@ spec = parallel $ inTestM $ do fakeBrowser = sh '' 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 ]); 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 forM_ [":", " "] $ \separator ->