From 93c921c58270ed706ba5af385341aeda15c8f9ce Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 04:13:29 -0600 Subject: [PATCH 1/2] fix(normalize): preserve mode and reject symlink configs --- pkgs/opencode-config-normalize/normalize.sh | 30 ++++++++++++-- .../normalize_test.sh | 40 +++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index 115c0e610a..e72011255b 100644 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -24,12 +24,20 @@ set -euo pipefail CONFIG="${OPENCODE_CONFIG:-$HOME/.config/opencode/opencode.jsonc}" +if [ -L "$CONFIG" ]; then + printf 'opencode-config-normalize: refusing symlinked config %s\n' "$CONFIG" >&2 + exit 1 +fi [ -r "$CONFIG" ] || exit 0 python3 - "$CONFIG" <<'PY' -import json, re, sys, pathlib +import json, os, pathlib, re, stat, sys, tempfile path = pathlib.Path(sys.argv[1]) +if path.is_symlink(): + print(f'opencode-config-normalize: refusing symlinked config {path}', file=sys.stderr) + sys.exit(1) +original_mode = stat.S_IMODE(path.stat().st_mode) raw = path.read_text() # Cheap JSONC -> JSON for the membership test only; we never write this back. @@ -123,8 +131,22 @@ if 'agents' in expected: if parsed != expected: sys.exit(1) -tmp = path.with_suffix(path.suffix + '.normalize-tmp') -tmp.write_text(out) -tmp.replace(path) +fd, tmp_name = tempfile.mkstemp(prefix=f'.{path.name}.normalize-', dir=path.parent) +tmp = pathlib.Path(tmp_name) +try: + with os.fdopen(fd, 'w') as f: + fd = -1 # ownership transferred to the file object + f.write(out) + f.flush() + os.fchmod(f.fileno(), original_mode) + os.fsync(f.fileno()) + tmp.replace(path) +finally: + if fd >= 0: + os.close(fd) + try: + tmp.unlink() + except FileNotFoundError: + pass print(f"opencode-config-normalize: repaired {path}", file=sys.stderr) PY diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index a8ac68cb71..eb916f8707 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -148,6 +148,46 @@ cat > "$tmp/unrecognised.fixture" <<'EOF' EOF check unrecognised "$tmp/unrecognised.fixture" '' 1 +# Case 7: a repair preserves the config's original permission bits. +cat > "$tmp/mode.fixture" <<'EOF' +{ + "plugins": ["opencode-plugin-x"] +} +EOF +cp "$tmp/mode.fixture" "$tmp/mode.jsonc" +chmod 0600 "$tmp/mode.jsonc" +OPENCODE_CONFIG="$tmp/mode.jsonc" bash "$script" 2>/dev/null +if [ "$?" -ne 0 ] || [ "$(stat -c '%a' "$tmp/mode.jsonc")" != "600" ] \ + || [ "$(parsed_doc "$tmp/mode.jsonc")" != '{"plugin": ["opencode-plugin-x"]}' ]; then + echo "FAIL mode-preserved: repair must preserve 0600 and normalize plugins" + failures=$((failures+1)) +else + echo "PASS mode-preserved" +fi + +# Case 8: a symlinked config is refused before writes; keep both the link +# target and its contents unchanged. +cat > "$tmp/symlink-target.jsonc" <<'EOF' +{ + "plugins": ["opencode-plugin-x"] +} +EOF +ln -s symlink-target.jsonc "$tmp/symlink.jsonc" +link_before="$(readlink "$tmp/symlink.jsonc")" +target_before="$(sha256sum "$tmp/symlink-target.jsonc" | cut -d' ' -f1)" +OPENCODE_CONFIG="$tmp/symlink.jsonc" bash "$script" 2>"$tmp/symlink.stderr" +symlink_exit=$? +target_after="$(sha256sum "$tmp/symlink-target.jsonc" | cut -d' ' -f1)" +if [ "$symlink_exit" -eq 0 ] || [ ! -L "$tmp/symlink.jsonc" ] \ + || [ "$(readlink "$tmp/symlink.jsonc")" != "$link_before" ] \ + || [ "$target_before" != "$target_after" ] \ + || ! grep -q 'refusing symlinked config' "$tmp/symlink.stderr"; then + echo "FAIL symlink-refused: expected a clear refusal with link and target unchanged" + failures=$((failures+1)) +else + echo "PASS symlink-refused" +fi + if [ "$failures" -ne 0 ]; then echo "normalize_test: $failures failure(s)" exit 1 From 7a32363c36db6392b0589e3a1883a809fac6f41e Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 04:52:41 -0600 Subject: [PATCH 2/2] fix(normalize): align mode/symlink fixtures with the v2 plugin rename (#3180) Main now normalizes the v1 `plugin` key to canonical `plugins`, so the fixtures added with the mode/symlink fix (which used `plugins` as input) became no-ops after merging main: the repair never wrote, and the mode-preserved case asserted the old rename direction. Flip both fixtures to the v1 input spelling so each case still exercises a real rewrite, and expect the canonical `plugins` output. --- pkgs/opencode-config-normalize/normalize_test.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/opencode-config-normalize/normalize_test.sh b/pkgs/opencode-config-normalize/normalize_test.sh index 93cc0c4952..c69a81ad16 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -541,16 +541,18 @@ grep -qF '/* example syntax: /* text */' "$tmp/block-comment-inner-delimiter.jso } # Case 7: a repair preserves the config's original permission bits. +# (Input is the v1 `plugin` spelling — the canonical direction since +# the v2 rename — so the run performs a real rewrite.) cat > "$tmp/mode.fixture" <<'EOF' { - "plugins": ["opencode-plugin-x"] + "plugin": ["opencode-plugin-x"] } EOF cp "$tmp/mode.fixture" "$tmp/mode.jsonc" chmod 0600 "$tmp/mode.jsonc" OPENCODE_CONFIG="$tmp/mode.jsonc" bash "$script" 2>/dev/null if [ "$?" -ne 0 ] || [ "$(stat -c '%a' "$tmp/mode.jsonc")" != "600" ] \ - || [ "$(parsed_doc "$tmp/mode.jsonc")" != '{"plugin": ["opencode-plugin-x"]}' ]; then + || [ "$(parsed_doc "$tmp/mode.jsonc")" != '{"plugins": ["opencode-plugin-x"]}' ]; then echo "FAIL mode-preserved: repair must preserve 0600 and normalize plugins" failures=$((failures+1)) else @@ -561,7 +563,7 @@ fi # target and its contents unchanged. cat > "$tmp/symlink-target.jsonc" <<'EOF' { - "plugins": ["opencode-plugin-x"] + "plugin": ["opencode-plugin-x"] } EOF ln -s symlink-target.jsonc "$tmp/symlink.jsonc"