diff --git a/pkgs/opencode-config-normalize/normalize.sh b/pkgs/opencode-config-normalize/normalize.sh index 131f34134a..2a4b97b937 100755 --- a/pkgs/opencode-config-normalize/normalize.sh +++ b/pkgs/opencode-config-normalize/normalize.sh @@ -39,12 +39,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() @@ -674,8 +682,22 @@ if 'agent' 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 04c4a63e60..c69a81ad16 100755 --- a/pkgs/opencode-config-normalize/normalize_test.sh +++ b/pkgs/opencode-config-normalize/normalize_test.sh @@ -540,6 +540,48 @@ grep -qF '/* example syntax: /* text */' "$tmp/block-comment-inner-delimiter.jso failures=$((failures + 1)) } +# 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' +{ + "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")" != '{"plugins": ["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' +{ + "plugin": ["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