fix(scripts): refuse a dependency URL with a port in the allow-sync gate

How apm 0.39 writes a port into a package identity is unverified, so a
key derived from a git: URL with a port could be wrong while looking
right. The gate now fails clearly on one, naming `apm approve --list` as
the way to read the real identity. The script and gates.md record that
only an https URL on a lowercase host was checked against apm.

The test drops its GNU-only `sed -i` for a python3 rewrite, and covers
ssh:// and https URLs with a port under both readers.

Refs: #271

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
This commit is contained in:
Defame1297andClaude Opus 5.5 committed 2026-12-02 09:47:38 +00:00
1 parent 615d3dd881
commit dca0b5823b
3 files changed
+81 -14

No files matched your search

+4 -1
View File
@@ -1273,7 +1273,10 @@ matched no longer matches: the first 0.39 install parks the hooks, rewrites `.cl
without them, and root `apm audit --ci` still passes (spike #269, on #268). So the gate now derives
each expected key from root `apm.yml`'s own `git:` + `path:` dependency entry for the plugin (URL
without scheme, user or `.git`, then the path), appends `#<version>`, and fails a bare key with a
message saying why. A shipping plugin with no such entry fails, since no key can be derived. The
message saying why. A shipping plugin with no such entry fails, since no key can be derived, and so
does one whose `git:` URL names a port, because how apm writes a port into the identity is
unverified. Only an https URL on a lowercase host was checked against `apm approve --list`; the
scp-like and `ssh://` forms follow the same rule unchecked, and the host's case is kept as written. The
`#<version>` suffix is still stripped before apm matches, so the grant stays version-blind and the
version half stays a repo-level record. The grants are type-level (`hooks: true`), not
content-pinned: `apm approve` would pin content, strip the block's comments, and park the hook on
+49 -12
View File
@@ -331,25 +331,51 @@ norm_path() {
printf '%s' "${path%/}"
}
# identity <plugin dir> — <host>/<owner>/<repo>/<path> from root apm.yml's
# git+path dependency entry for plugins/<plugin dir>; empty when it has none.
identity() {
# dep_url <plugin dir> — the git: URL of root apm.yml's git+path dependency
# entry for plugins/<plugin dir>; empty when it has none.
dep_url() {
local want="plugins/$1" tag path url
while IFS=$'\t' read -r tag path url; do
[[ "$tag" == d && "$(norm_path "$path")" == "$want" ]] || continue
if [[ "$url" == *://* ]]; then
url="${url#*://}"
elif [[ "$url" == *:* ]]; then
url="${url/://}" # scp-like user@host:owner/repo
fi
url="${url#*@}"
url="${url%/}"
url="${url%.git}"
printf '%s/%s' "$url" "$want"
printf '%s' "$url"
return
done <<< "$FACTS"
}
# url_has_port <url> — true when a scheme URL names a port (host:port).
url_has_port() {
local rest
[[ "$1" == *://* ]] || return 1
rest="${1#*://}"
rest="${rest%%/*}"
rest="${rest#*@}"
[[ "$rest" == *:* ]]
}
# identity <plugin dir> — <host>/<owner>/<repo>/<path> from that entry; empty
# when there is none.
#
# Verified against apm 0.39.3 (`apm approve --list`) for an https URL on a
# lowercase host only. The scp-like (user@host:owner/repo) and ssh:// forms are
# derived by the same rule but were not checked against apm, and the host's
# case is kept as written, also unchecked. A URL with a port is refused before
# this runs (see the per-package checks): where apm puts a port in the
# identity is unknown, so a derived key could be wrong while looking right.
identity() {
local want="plugins/$1" url
url="$(dep_url "$1")"
[[ -n "$url" ]] || return 0
if [[ "$url" == *://* ]]; then
url="${url#*://}"
elif [[ "$url" == *:* ]]; then
url="${url/://}" # scp-like user@host:owner/repo
fi
url="${url#*@}"
url="${url%/}"
url="${url%.git}"
printf '%s/%s' "$url" "$want"
}
# expected_key <plugin dir> — '<identity>#<version>'.
expected_key() {
printf '%s#%s' "$(identity "$1")" "$(fact v "$1")"
@@ -404,6 +430,17 @@ for plugin in ${READABLE[@]+"${READABLE[@]}"}; do
continue
fi
if url_has_port "$(dep_url "$plugin")"; then
echo "FAIL: root apm.yml's dependency entry for plugins/$plugin uses a git: URL with a port," >&2
echo " '$(dep_url "$plugin")', so its executables.allow key cannot be derived reliably." >&2
echo " Why: this gate derives the full package identity apm 0.39 approves under from that URL," >&2
echo " and how apm writes a port into the identity was never verified." >&2
echo " Fix: use the URL without a port if the host allows it; otherwise read the identity from" >&2
echo " 'uv run --frozen apm approve --list' and extend this check to match it." >&2
FAILED=1
continue
fi
EXPECTED+=("$plugin")
done
+28 -1
View File
@@ -316,7 +316,13 @@ grep -qF "full package identity" <<< "$OUT" && pass "says why a bare key no long
# No git+path dependency entry for the plugin means no identity to derive.
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
sed -i '/path: plugins\/kyberforge/d' "$F/apm.yml"
# python3, not `sed -i`, whose in-place syntax differs between GNU and BSD sed.
python3 - "$F/apm.yml" << 'PY2'
import sys
path = sys.argv[1]
lines = open(path).read().splitlines(keepends=True)
open(path, "w").write("".join(l for l in lines if "path: plugins/kyberforge" not in l))
PY2
run_gate "$F"
[[ $GATE_RC -ne 0 ]] && pass "fails when root apm.yml has no git+path entry for the plugin" \
|| fail "with no dependency entry the identity cannot be derived; must fail"
@@ -416,6 +422,27 @@ F="$(make_fixture "version: 1.5.0" "$BARE_BLOCK")"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback fails a bare key" || fail "fallback missed a bare key"
# A URL with a port: where apm puts the port in the identity is unverified, so
# the gate refuses to derive a key rather than derive a plausible wrong one.
for port_url in "ssh://[email protected]:2222/Owner/Repo.git" "https://git.example.invalid:8443/Owner/Repo.git"; do
F="$(make_fixture "version: 1.5.0" "$MATCHING_BLOCK")"
python3 - "$F/apm.yml" "$port_url" << 'PY2'
import sys
path, url = sys.argv[1], sys.argv[2]
text = open(path).read()
old = " - git: https://git.example.invalid/Owner/Repo.git\n path: plugins/kyberforge\n"
assert old in text
open(path, "w").write(text.replace(old, " - git: %s\n path: plugins/kyberforge\n" % url))
PY2
OUT="$(bash "$SCRIPT" "$F" 2>&1)" && rc=0 || rc=$?
[[ $rc -ne 0 ]] && grep -qF "URL with a port" <<< "$OUT" \
&& pass "refuses a git: URL with a port: $port_url" \
|| fail "a URL with a port must fail clearly ($port_url), got rc=$rc: $OUT"
run_fallback "$F"
[[ $GATE_RC -ne 0 ]] && pass "fallback refuses a git: URL with a port: $port_url" \
|| fail "fallback must refuse a URL with a port ($port_url)"
done
# ---------------------------------------------------------------------------
echo ""
echo "--- wired into the pre-push gate ---"