Trim credentials before use; fix redact on awkward tokens

A GITEA_TOKEN secret stored with a trailing newline makes curl emit
`Authorization: token <value>\n`. The bare LF terminates the header
block early, so Content-Type and everything after it is parsed as
request body and Gitea rejects the call with 422 "Empty Content-Type".
GETs were unaffected because nothing they need follows the auth header,
which is why this presented as a proxy stripping the header.

- trim CR/LF and surrounding whitespace off GITHUB_TOKEN, GITEA_TOKEN,
  GITEA_URL and GITEA_OWNER, warning when a value needed it
- rebuild redact() on bash substitution; the previous sed s/// blew up
  with "unterminated `s' command" on a token containing a newline
- add AGENTS.md / CLAUDE.md documenting the run flow and these traps
This commit is contained in:
2026-08-19 08:44:58 +03:00
parent 3a07c298f6
commit 028d478b05
3 changed files with 135 additions and 1 deletions
+30 -1
View File
@@ -15,11 +15,40 @@ ONLY="${ONLY:-}"
# DEBUG_HTTP=1 prints the request/response headers of every write call (tokens redacted).
DEBUG_HTTP="${DEBUG_HTTP:-}"
# --- Sanitise the credentials ---------------------------------------------
# A secret pasted into GitHub Actions with a trailing newline is invisible in
# the logs but fatal here: curl emits `Authorization: token <value>\n` and the
# bare LF terminates the header block early, so every header curl writes after
# it - Content-Type included - lands in the request body instead. Gitea then
# rejects the call with 422 "Empty Content-Type". GETs survive because nothing
# they need comes after the Authorization header.
trim() { printf '%s' "$1" | tr -d '\r\n' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'; }
for _var in GITHUB_TOKEN GITEA_TOKEN GITEA_URL GITEA_OWNER; do
_raw="${!_var}"
_clean=$(trim "$_raw")
if [ "$_raw" != "$_clean" ]; then
echo "Warning: ${_var} had surrounding whitespace or a newline; trimmed it." >&2
echo " Fix the secret itself - re-add it without a trailing newline." >&2
fi
printf -v "$_var" '%s' "$_clean"
done
unset _var _raw _clean
GITEA_URL="${GITEA_URL%/}" # re-strip: trimming may have exposed a trailing slash
work_dir=$(mktemp -d)
trap 'rm -rf "$work_dir"' EXIT
# Literal, delimiter-free substitution - building a sed s/// out of a secret
# breaks as soon as the secret contains a slash or a newline.
redact() {
sed -e "s/${GITEA_TOKEN}/<GITEA_TOKEN>/g" -e "s/${GITHUB_TOKEN}/<GITHUB_TOKEN>/g"
local line
while IFS= read -r line || [ -n "$line" ]; do
line=${line//"$GITEA_TOKEN"/<GITEA_TOKEN>}
line=${line//"$GITHUB_TOKEN"/<GITHUB_TOKEN>}
printf '%s\n' "$line"
done
}
# Send a JSON body to Gitea and echo the HTTP status.