Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

feat(chrome): opt-in Chrome sandbox, no_new_privs and no setuid binaries (#3280)

Mohamed Bassem2453aaa4bc26
diff --git a/.github/workflows/chrome.yml b/.github/workflows/chrome.yml
index 555dd410..014d97e5 100644
--- a/.github/workflows/chrome.yml
+++ b/.github/workflows/chrome.yml
@@ -21,7 +21,7 @@ env:
IMAGE: ghcr.io/karakeep-app/karakeep-chrome
# Google Chrome version (amd64); arm64 images use Debian's Chromium.
GOOGLE_CHROME_VERSION: 156.0.8078.47
- IMAGE_REVISION: r1
+ IMAGE_REVISION: r2
jobs:
metadata:
diff --git a/docker/chrome/Dockerfile b/docker/chrome/Dockerfile
index 1f497b08..bd058b59 100644
--- a/docker/chrome/Dockerfile
+++ b/docker/chrome/Dockerfile
@@ -47,6 +47,10 @@ RUN apt-get update \
COPY --chmod=755 docker/chrome/entrypoint.sh /usr/local/bin/chrome-entrypoint.sh
+# Nothing here needs setuid/setgid binaries (Chrome runs as nobody and doesn't
+# use the setuid chrome-sandbox helper), so remove every path back to root.
+RUN find / -xdev -perm /6000 -type f -exec chmod a-s {} +
+
ENV HOME=/tmp
USER 65534:65534
EXPOSE 9222
diff --git a/docker/chrome/README.md b/docker/chrome/README.md
index 9a082976..8166ee72 100644
--- a/docker/chrome/README.md
+++ b/docker/chrome/README.md
@@ -10,6 +10,28 @@ Shell: in A/B tests against bot-protected sites, the headless shell was blocked
far more often (it leaks CDP automation even with a patched client), and so,
to a lesser extent, was the Chromium build.
+## Sandbox
+
+Chrome runs as `nobody` with `no_new_privs` set, and the image has no setuid or
+setgid binaries. Chrome's own sandbox (which isolates every renderer) is off by
+default because it needs unprivileged user namespaces, which Docker's default
+seccomp profile blocks. To turn it on, set `KARAKEEP_CHROME_SANDBOX=true` and
+run the container where user namespaces are allowed, e.g. on Docker:
+
+```yaml
+chrome:
+ environment:
+ KARAKEEP_CHROME_SANDBOX: "true"
+ security_opt:
+ - seccomp=unconfined
+```
+
+Some hosts also restrict user namespaces themselves. Lifting that is a
+host-wide change: `sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0`
+on Ubuntu 23.10+, or `kernel.unprivileged_userns_clone=1` on older Debian.
+
+If the sandbox can't start, Chrome exits instead of running without it.
+
## Testing locally
Run Karakeep's e2e suite, which builds this image through its Compose file and
diff --git a/docker/chrome/entrypoint.sh b/docker/chrome/entrypoint.sh
index 1dbc93a8..eeb04a9a 100755
--- a/docker/chrome/entrypoint.sh
+++ b/docker/chrome/entrypoint.sh
@@ -6,11 +6,18 @@ set -e
socat TCP4-LISTEN:9222,fork,reuseaddr TCP4:127.0.0.1:9223 &
+# Chrome's sandbox needs unprivileged user namespaces, which Docker's default
+# seccomp profile blocks, so it's opt-in (see README.md).
+if [ "${KARAKEEP_CHROME_SANDBOX:-false}" != "true" ]; then
+ set -- --no-sandbox "$@"
+fi
+
# SwiftShader keeps WebGL available without a GPU (even with --disable-gpu);
# a browser without WebGL is an easy headless tell.
-exec karakeep-chrome \
+# no_new_privs keeps setuid binaries and file capabilities from granting
+# Chrome any more privileges.
+exec setpriv --no-new-privs karakeep-chrome \
--headless=new \
- --no-sandbox \
--no-first-run \
--no-default-browser-check \
--user-data-dir=/tmp/chrome-profile \