Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion pkgs/opencode-config-normalize/normalize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
# native-wins semantics), preserving everything else including comments.
# It is idempotent and a no-op when the file is already canonical, so it
# is safe to run on a path-unit trigger.
#
# Writes are atomic (temp file in the same directory, then rename) and
# keep the original's metadata: mode bits (#3180) and its POSIX access
# ACL (#11836), copied as the raw system.posix_acl_access xattr and
# verified on the new file before the rename. A file with no extended
# ACL stays without one, even under a directory default ACL. If the ACL
# cannot be read, restored or verified, the config is left untouched and
# the script exits 1. On a platform or filesystem without POSIX ACLs
# (no os.getxattr, or ENOTSUP) only the mode bits are carried over.

set -euo pipefail

Expand All @@ -46,13 +55,60 @@ fi
[ -r "$CONFIG" ] || exit 0

python3 - "$CONFIG" <<'PY'
import json, os, pathlib, re, stat, sys, tempfile
import errno, 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)

# POSIX access ACL of the original file (#11836). The atomic replace
# below writes a new inode, which would otherwise come out with no
# file-specific ACL entries (or with entries inherited from a directory
# default ACL, which are not the original's). Snapshot the raw
# `system.posix_acl_access` xattr (numeric ids, so it restores exactly)
# and put it back on the temp inode before the rename.
# - None: the original has no extended ACL (or the platform or
# filesystem has no POSIX ACLs); the new inode must carry none either.
# - bytes: restore and verify these exact bytes.
# Any other failure to read it fails closed: the original is left
# untouched rather than silently changing its access policy.
ACL_XATTR = 'system.posix_acl_access'
ACL_UNSUPPORTED = {errno.ENOTSUP, getattr(errno, 'EOPNOTSUPP', errno.ENOTSUP)}
ACL_ABSENT = {errno.ENODATA} if hasattr(errno, 'ENODATA') else set()
acl_supported = hasattr(os, 'getxattr')
original_acl = None
if acl_supported:
try:
original_acl = os.getxattr(path, ACL_XATTR, follow_symlinks=False)
except OSError as e:
if e.errno in ACL_UNSUPPORTED:
acl_supported = False
elif e.errno not in ACL_ABSENT:
print(f'opencode-config-normalize: cannot read the ACL of {path} ({e}); '
'leaving it untouched', file=sys.stderr)
sys.exit(1)


def apply_original_acl(fd):
"""Give the temp inode exactly the original's access ACL, then verify."""
if not acl_supported:
return
if original_acl is None:
# Drop anything inherited from a directory default ACL so the
# repaired file keeps the original's (absent) extended ACL.
try:
os.removexattr(fd, ACL_XATTR)
except OSError as e:
if e.errno not in ACL_ABSENT | ACL_UNSUPPORTED:
raise
return
os.setxattr(fd, ACL_XATTR, original_acl)
if os.getxattr(fd, ACL_XATTR) != original_acl:
raise OSError(errno.EIO, 'restored ACL does not match the original')


raw = path.read_text()


Expand Down Expand Up @@ -690,6 +746,16 @@ try:
f.write(out)
f.flush()
os.fchmod(f.fileno(), original_mode)
try:
apply_original_acl(f.fileno())
except OSError as e:
print(f'opencode-config-normalize: cannot carry the ACL of {path} over to '
f'the repaired file ({e}); leaving it untouched', file=sys.stderr)
sys.exit(1)
if stat.S_IMODE(os.fstat(f.fileno()).st_mode) != original_mode:
print(f'opencode-config-normalize: repaired file mode does not match {path}; '
'leaving it untouched', file=sys.stderr)
sys.exit(1)
os.fsync(f.fileno())
tmp.replace(path)
finally:
Expand Down
79 changes: 79 additions & 0 deletions pkgs/opencode-config-normalize/normalize_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,85 @@ else
echo "PASS symlink-refused"
fi

# Case 9 (#11836): the atomic replace must carry the original file's
# POSIX access ACL over to the new inode, and must not pass off entries
# inherited from a directory default ACL as the original's. ACLs are
# set and read as the raw system.posix_acl_access xattr (numeric ids),
# so no acl tooling is needed; on a platform or filesystem without
# POSIX ACLs the case reports an explicit SKIP.
acl() {
python3 - "$@" <<'PY'
import os, struct, sys
U = 0xFFFFFFFF
TAGS = {'u': 1, 'U': 2, 'g': 4, 'G': 8, 'm': 0x10, 'o': 0x20}
def pack(spec):
out = [struct.pack('<I', 2)]
for item in spec.split(','):
tag, ident, perm = item.split(':')
out.append(struct.pack('<HHI', TAGS[tag], int(perm, 8), int(ident) if ident else U))
return b''.join(out)
op, path = sys.argv[1], sys.argv[2]
name = 'system.posix_acl_default' if op == 'set-default' else 'system.posix_acl_access'
try:
if op in ('set', 'set-default'):
os.setxattr(path, name, pack(sys.argv[3]))
else:
try:
print(os.getxattr(path, name).hex())
except OSError as e:
if e.errno == getattr(__import__('errno'), 'ENODATA', -1):
print('none')
else:
raise
except (OSError, AttributeError) as e:
print(f'unsupported: {e}', file=sys.stderr)
sys.exit(77)
PY
}
acl_dir="$tmp/acl"
mkdir -p "$acl_dir"
printf '{\n "plugin": ["opencode-plugin-x"]\n}\n' >"$acl_dir/with-acl.jsonc"
chmod 0640 "$acl_dir/with-acl.jsonc"
if ! acl set "$acl_dir/with-acl.jsonc" 'u::6,U:65533:4,g::0,m::4,o::0' 2>/dev/null ||
! acl set-default "$acl_dir" 'u::6,U:65534:4,g::0,m::4,o::0' 2>/dev/null; then
echo "SKIP acl-preserved: no POSIX ACL support on this platform or filesystem"
echo "SKIP acl-not-inherited: no POSIX ACL support on this platform or filesystem"
else
acl_before="$(acl get "$acl_dir/with-acl.jsonc")"
OPENCODE_CONFIG="$acl_dir/with-acl.jsonc" bash "$script" 2>/dev/null
acl_exit=$?
acl_after="$(acl get "$acl_dir/with-acl.jsonc")"
acl_mode="$(stat -c %a "$acl_dir/with-acl.jsonc")"
acl_doc="$(parsed_doc "$acl_dir/with-acl.jsonc")"
if [ "$acl_exit" -ne 0 ] || [ "$acl_before" = none ] ||
[ "$acl_after" != "$acl_before" ] || [ "$acl_mode" != 640 ] ||
[ "$acl_doc" != '{"plugins": ["opencode-plugin-x"]}' ]; then
echo "FAIL acl-preserved: exit=$acl_exit mode=$acl_mode acl before=$acl_before after=$acl_after"
failures=$((failures + 1))
else
echo "PASS acl-preserved"
fi

# The original has no extended ACL; the directory's default ACL must
# not appear on the repaired file as if it were the original's.
printf '{\n "plugin": ["opencode-plugin-x"]\n}\n' >"$acl_dir/no-acl.jsonc.src"
cp "$acl_dir/no-acl.jsonc.src" "$tmp/no-acl.jsonc"
chmod 0600 "$tmp/no-acl.jsonc"
mv "$tmp/no-acl.jsonc" "$acl_dir/no-acl.jsonc"
noacl_before="$(acl get "$acl_dir/no-acl.jsonc")"
OPENCODE_CONFIG="$acl_dir/no-acl.jsonc" bash "$script" 2>/dev/null
noacl_exit=$?
noacl_after="$(acl get "$acl_dir/no-acl.jsonc")"
noacl_mode="$(stat -c %a "$acl_dir/no-acl.jsonc")"
if [ "$noacl_exit" -ne 0 ] || [ "$noacl_before" != none ] ||
[ "$noacl_after" != none ] || [ "$noacl_mode" != 600 ]; then
echo "FAIL acl-not-inherited: exit=$noacl_exit mode=$noacl_mode acl before=$noacl_before after=$noacl_after"
failures=$((failures + 1))
else
echo "PASS acl-not-inherited"
fi
fi

if [ "$failures" -ne 0 ]; then
echo "normalize_test: $failures failure(s)"
exit 1
Expand Down