[meta] release-notes.sh: replace control characters in commit subjects
Commit subjects were printed as-is, so escape sequences, carriage returns, and other control characters in a subject reached the terminal during `make release-notes` and `make release`, where they could hide or rewrite lines of the notes being reviewed, and were then written verbatim into the bump commit and the release tag. A tab in a subject also corrupted the tab-separated intermediate file. Subjects are now filtered as they are read from `git log`, replacing C0 controls, DEL, and UTF-8-encoded C1 controls with `?`. They are replaced rather than removed so that the bytes on either side cannot join into a new control character, and so the change stays visible in the notes. The section `grep` now matches bytes, because in a UTF-8 locale GNU grep stops printing matches at a line that is not valid UTF-8, which silently dropped bullets. Lowercasing a subject to find its category now works on bytes too, because in a UTF-8 locale BSD `tr` stops at a byte that is not valid UTF-8. Discovered by Anthropic's OSS Scanner, as bug ANT-2026-INF3JLG6; the scanner correctly classified it as not a vulnerability.
This commit is contained in:
2 files changed
+96
-3
No files matched your search
+9
-3
@@ -66,14 +66,19 @@ fi
|
|||||||
: "${REPO_SLUG:=nvm-sh/nvm}"
|
: "${REPO_SLUG:=nvm-sh/nvm}"
|
||||||
|
|
||||||
TAB="$(printf '\t')"
|
TAB="$(printf '\t')"
|
||||||
|
# U+0080 through U+009F, as UTF-8 bytes
|
||||||
|
C1_CONTROLS="$(printf '\302[\200-\237]')"
|
||||||
|
|
||||||
COMMITS_FILE="$(mktemp)" || exit 1
|
COMMITS_FILE="$(mktemp)" || exit 1
|
||||||
GH_ERR_FILE="$(mktemp)" || exit 1
|
GH_ERR_FILE="$(mktemp)" || exit 1
|
||||||
trap 'rm -f "${COMMITS_FILE}" "${GH_ERR_FILE}"' EXIT
|
trap 'rm -f "${COMMITS_FILE}" "${GH_ERR_FILE}"' EXIT
|
||||||
|
|
||||||
# each line: <section key> TAB <sha> TAB <bullet text>, in git log order
|
# each line: <section key> TAB <sha> TAB <bullet text>, in git log order
|
||||||
git log --no-merges --format='%H %s' "${PREV_REF}..${END_REF}" | while IFS=' ' read -r SHA SUBJECT; do
|
# control characters in a subject would corrupt the terminal and the tab-separated output;
|
||||||
LOWERED="$(printf '%s' "${SUBJECT}" | tr '[:upper:]' '[:lower:]')"
|
# replacing them (rather than removing them) keeps the bytes around them from joining into another one
|
||||||
|
git log --no-merges --format='%H %s' "${PREV_REF}..${END_REF}" | LC_ALL=C sed "s/[[:cntrl:]]/?/g;s/${C1_CONTROLS}/?/g" | while IFS=' ' read -r SHA SUBJECT; do
|
||||||
|
# match bytes: in a UTF-8 locale, BSD tr stops at a byte that is not valid UTF-8
|
||||||
|
LOWERED="$(printf '%s' "${SUBJECT}" | LC_ALL=C tr '[:upper:]' '[:lower:]')"
|
||||||
STRIPPED="${SUBJECT#\[*\] }"
|
STRIPPED="${SUBJECT#\[*\] }"
|
||||||
case "${LOWERED}" in
|
case "${LOWERED}" in
|
||||||
v[0-9]*.[0-9]*.[0-9]*)
|
v[0-9]*.[0-9]*.[0-9]*)
|
||||||
@@ -115,7 +120,8 @@ print_section() {
|
|||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
printf '\n%s\n\n' "$2"
|
printf '\n%s\n\n' "$2"
|
||||||
grep "^$1${TAB}" "${COMMITS_FILE}" | while IFS="${TAB}" read -r _KEY SHA TEXT; do
|
# match bytes: in a UTF-8 locale, GNU grep stops printing matches at a line that is not valid UTF-8
|
||||||
|
LC_ALL=C grep "^$1${TAB}" "${COMMITS_FILE}" | while IFS="${TAB}" read -r _KEY SHA TEXT; do
|
||||||
REF=''
|
REF=''
|
||||||
case "${TEXT}" in
|
case "${TEXT}" in
|
||||||
*'(#'[0-9]*')') ;; # the subject already carries a reference
|
*'(#'[0-9]*')') ;; # the subject already carries a reference
|
||||||
|
|||||||
+87
@@ -0,0 +1,87 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
RELEASE_NOTES="${PWD}/../../../release-notes.sh"
|
||||||
|
WORK="$(mktemp -d)" || exit 1
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
cd / && rm -rf "${WORK}"
|
||||||
|
}
|
||||||
|
die() { echo "$@" ; cleanup ; exit 1; }
|
||||||
|
|
||||||
|
export HOME="${WORK}/home"
|
||||||
|
export GIT_CONFIG_NOSYSTEM=1
|
||||||
|
unset XDG_CONFIG_HOME GIT_DIR GIT_WORK_TREE
|
||||||
|
export NVM_RELEASE_NOTES_SKIP_PRS=1
|
||||||
|
export GIT_COMMITTER_NAME=nvm GIT_COMMITTER_EMAIL=nvm@example.com
|
||||||
|
|
||||||
|
IDENT='contributor <contributor@example.com> 0 +0000'
|
||||||
|
|
||||||
|
# writes the commit object byte for byte; `git commit` would repair a subject that is not valid UTF-8
|
||||||
|
commit() {
|
||||||
|
PARENT=''
|
||||||
|
if git rev-parse --quiet --verify HEAD >/dev/null; then
|
||||||
|
PARENT="parent $(git rev-parse HEAD)
|
||||||
|
"
|
||||||
|
fi
|
||||||
|
COMMIT="$(
|
||||||
|
{
|
||||||
|
printf 'tree %s\n%sauthor %s\ncommitter %s\n\n' "${EMPTY_TREE}" "${PARENT}" "${IDENT}" "${IDENT}"
|
||||||
|
# shellcheck disable=SC2059
|
||||||
|
printf "$1\n"
|
||||||
|
} | git hash-object -t commit -w --stdin
|
||||||
|
)" || die 'failed to write a commit'
|
||||||
|
git update-ref HEAD "${COMMIT}" || die 'failed to update HEAD'
|
||||||
|
}
|
||||||
|
|
||||||
|
mkdir -p "${HOME}" "${WORK}/repo" || die 'failed to create the scratch dirs'
|
||||||
|
cd "${WORK}/repo" || die 'failed to enter the scratch repo'
|
||||||
|
git init --quiet . || die 'failed to create the scratch repo'
|
||||||
|
EMPTY_TREE="$(git hash-object -t tree -w /dev/null)" || die 'failed to write the empty tree'
|
||||||
|
|
||||||
|
commit 'v0.0.1'
|
||||||
|
PREV_REF="$(git rev-parse HEAD)"
|
||||||
|
commit '[Fix] older bullet'
|
||||||
|
commit '[New] \342\200\234quoted\342\200\235 bullet'
|
||||||
|
commit '[Fix] stray \233 byte'
|
||||||
|
commit '[Fix] U+009B once the escape is gone: \302\033\2332K'
|
||||||
|
commit '[Fix] U+009B: \302\2332K'
|
||||||
|
commit '[Fix] tab\tand delete\177'
|
||||||
|
commit '[Fix] escapes\033]0;title\007\r\033[2K\033[1A'
|
||||||
|
commit 'no prefix\033[2K'
|
||||||
|
|
||||||
|
EXPECTED_OUTPUT="$(printf '%b\n' \
|
||||||
|
'v0.0.2' \
|
||||||
|
'' \
|
||||||
|
'New Stuff' \
|
||||||
|
'' \
|
||||||
|
' - \0342\0200\0234quoted\0342\0200\0235 bullet' \
|
||||||
|
'' \
|
||||||
|
'Bug Fixes' \
|
||||||
|
'' \
|
||||||
|
' - escapes?]0;title???[2K?[1A' \
|
||||||
|
' - tab?and delete?' \
|
||||||
|
' - U+009B: ?2K' \
|
||||||
|
' - U+009B once the escape is gone: \0302?\02332K' \
|
||||||
|
' - stray \0233 byte' \
|
||||||
|
' - older bullet' \
|
||||||
|
'' \
|
||||||
|
'Misc' \
|
||||||
|
'' \
|
||||||
|
' - no prefix?[2K')"
|
||||||
|
EXPECTED_STDERR='release-notes: no category prefix, filing under Misc: no prefix?[2K'
|
||||||
|
|
||||||
|
for LOCALE in C C.UTF-8 en_US.UTF-8; do
|
||||||
|
case "${LOCALE}" in
|
||||||
|
*.UTF-8) [ "$(LC_ALL="${LOCALE}" locale charmap 2>/dev/null)" = 'UTF-8' ] || continue ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
OUTPUT="$(LC_ALL="${LOCALE}" "${RELEASE_NOTES}" v0.0.2 "${PREV_REF}" 2>/dev/null)"
|
||||||
|
[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] \
|
||||||
|
|| die "${LOCALE}: expected >${EXPECTED_OUTPUT}<, got >$(printf '%s' "${OUTPUT}" | od -c)<"
|
||||||
|
|
||||||
|
STDERR="$(LC_ALL="${LOCALE}" "${RELEASE_NOTES}" v0.0.2 "${PREV_REF}" 2>&1 >/dev/null)"
|
||||||
|
[ "_${STDERR}" = "_${EXPECTED_STDERR}" ] \
|
||||||
|
|| die "${LOCALE}: expected stderr >${EXPECTED_STDERR}<, got >$(printf '%s' "${STDERR}" | od -c)<"
|
||||||
|
done
|
||||||
|
|
||||||
|
cleanup
|
||||||
Reference in new issue
Block a user