From 9bde2c4e3c550e94deee66aaf626c142e45751ed Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Wed, 14 Oct 2026 11:27:22 +0000 Subject: [PATCH] fix(install): keep .env readable only by its owner when the line runs again setenv rewrote .env through a temporary file made with the shell's umask, so on an update (where install.sh sets no umask, and the upgrade agent sets 022) .env became 0644 and its secrets readable to other local users. The copy is now made with umask 077, and .env is set to 600 on every run, which also closes installs an earlier update left open. CI checks the mode after the second run in both the Docker and microsandbox jobs. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_016M1M7EXU6uJVnqfp77L19k --- .github/workflows/install.yml | 2 ++ CHANGELOG.md | 3 +++ install.sh | 5 +++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/install.yml b/.github/workflows/install.yml index 13b5c9e81..89df5457f 100644 --- a/.github/workflows/install.yml +++ b/.github/workflows/install.yml @@ -177,6 +177,7 @@ jobs: sh < install.sh after="$(grep -E '^(ACCESS_SECRET_KEY|ARCHIVE_SECRET_KEY|AUTH_SECRET|SURREAL_PASS|LENS_SETUP_CODE)=' "$LENS_DIR/.env" | sha256sum)" test "$before" = "$after" || { echo "the second run changed the secrets"; exit 1; } + test "$(stat -c %a "$LENS_DIR/.env")" = 600 || { echo "the second run left .env readable to others"; exit 1; } curl -fsS -o /dev/null "$(cat setup-url)" - name: An upgrade asked for in the web app is installed by the machine's agent @@ -262,6 +263,7 @@ jobs: LENS_BACKEND='' sh < install.sh | tee again.log after="$(grep -E '^(ACCESS_SECRET_KEY|ARCHIVE_SECRET_KEY|AUTH_SECRET|SURREAL_PASS|LENS_SETUP_CODE)=' "$LENS_DIR/.env" | sha256sum)" test "$before" = "$after" || { echo "the second run changed the secrets"; exit 1; } + test "$(stat -c %a "$LENS_DIR/.env")" = 600 || { echo "the second run left .env readable to others"; exit 1; } grep -q "Stopping the Lens that's running" again.log || { echo "the second run didn't stop the running sandbox first"; exit 1; } curl -fsS -o /dev/null "$(cat setup-url)" diff --git a/CHANGELOG.md b/CHANGELOG.md index ff633b785..843bd1da4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ## Unreleased +- **Running the install line again keeps `.env` readable only by you.** Each update rewrote `.env` with the shell's + default permissions, so after an update its secrets could be read by other users of the machine. It is now written + for its owner alone, and an install an earlier update left open is closed again the next time the line runs. - **A microsandbox install whose keys don't open its database comes up when the line runs again.** A reinstall made before 0.8.1, after `~/lens` was deleted, wrote new keys but kept the earlier install's `lens-data` volume, and every update after it kept both: its API stopped at every start with `SURREAL_USER/SURREAL_PASS don't match`. The sandbox diff --git a/install.sh b/install.sh index eaeafeb44..47c18ef43 100755 --- a/install.sh +++ b/install.sh @@ -352,9 +352,9 @@ HTTPS_PORT="${HTTPS_PORT:-443}" HTTP_PORT="${HTTP_PORT:-80}" # --- Secrets, once: changing them later would sign everyone out and make stored credentials unreadable ------------ -setenv() { # key value: replace the line, or add it +setenv() { # key value: replace the line, or add it (only this user can read the copy that replaces it) if grep -q "^$1=" .env; then - sed "s|^$1=.*|$1=$2|" .env >.env.tmp && mv .env.tmp .env + (umask 077 && sed "s|^$1=.*|$1=$2|" .env >.env.tmp) && mv .env.tmp .env else printf '%s=%s\n' "$1" "$2" >>.env fi @@ -369,6 +369,7 @@ if [ ! -f .env ]; then setenv AUTH_SECRET "$(secret)" setenv SURREAL_PASS "$(secret)" fi +chmod 600 .env # the secrets are this user's alone, also where an earlier update left them readable to others grep -q '^LENS_SETUP_CODE=' .env || setenv LENS_SETUP_CODE "$(code)" # only used until the first admin exists setenv LENS_BACKEND "$BACKEND" setenv LENS_TARGET "$TARGET"