diff --git a/tools/linuxfs/preload/webhelper.c b/tools/linuxfs/preload/webhelper.c index 5263b0f464..bbccd890b1 100644 --- a/tools/linuxfs/preload/webhelper.c +++ b/tools/linuxfs/preload/webhelper.c @@ -58,6 +58,25 @@ static int beside_chrome_sandbox(const char *path) { return found; } +static char *folder_of(const char *path) { + char *real = realpath(path, NULL); + char *slash = real ? strrchr(real, '/') : NULL; + if (slash) *slash = 0; + return real; +} + +static int started_by_its_own_app(const char *path) { + char self[4096]; + ssize_t n = readlink("/proc/self/exe", self, sizeof self - 1); + if (n <= 0) return 0; + self[n] = 0; + char *mine = folder_of(self), *theirs = folder_of(path); + int same = mine && theirs && !strcmp(mine, theirs) && beside_chrome_sandbox(self); + free(mine); + free(theirs); + return same; +} + static char *const *chromium_argv(const char *path, char *const argv[], char *const envp[]) { if (!path || !argv || !argv[0]) return argv; int argc = 0, sandbox = 1, ozone = 0; @@ -69,7 +88,7 @@ static char *const *chromium_argv(const char *path, char *const argv[], char *co } const char *display = env_value(envp, "DISPLAY"); int x11 = !ozone && display && *display; - if ((!sandbox && !x11) || !beside_chrome_sandbox(path)) return argv; + if ((!sandbox && !x11) || !beside_chrome_sandbox(path) || started_by_its_own_app(path)) return argv; char **out = calloc((size_t)argc + 3, sizeof *out); if (!out) return argv; int n = 0; diff --git a/tools/tests/test_webhelper_preload.py b/tools/tests/test_webhelper_preload.py index 24e854dbb8..5b530566c1 100644 --- a/tools/tests/test_webhelper_preload.py +++ b/tools/tests/test_webhelper_preload.py @@ -122,6 +122,22 @@ class WebhelperPreloadTest(unittest.TestCase): (root / "bin/app").touch() self.assertEqual(self.argv(str(root / "bin/app")), [str(root / "bin/app"), "--no-sandbox"]) + def test_a_chromium_app_starting_a_program_from_its_own_folder_leaves_it_alone(self): + handler = self.chromium("chrome-app", "chrome_crashpad_handler") + browser = self.tmp / "chrome-app/chrome" + shutil.copy(self.program, browser) + out = subprocess.run([str(browser), handler, "--database=/tmp/x"], env={"TEST_DISPLAY": ":0"}, + capture_output=True, text=True, check=True).stdout.splitlines() + self.assertEqual([handler, "--database=/tmp/x"], out) + + def test_a_chromium_app_starting_another_chromium_app_still_flags_it(self): + other = self.chromium("other-app", "chrome") + editor = self.tmp / "editor-app/code" + self.chromium("editor-app", "code") + shutil.copy(self.program, editor) + out = subprocess.run([str(editor), other], env={}, capture_output=True, text=True, check=True).stdout.splitlines() + self.assertEqual([other, "--no-sandbox"], out) + def test_with_an_x_display_chromium_apps_use_x11(self): chrome = self.chromium("chromium-x11", "chrome") self.assertEqual(self.argv(chrome, "--new-window", display=":0"),