fix: use mktemp in _sprite_fix_config to prevent race conditions (#2359)

Replaces ${cfg}.fix$$ temp pattern with mktemp for guaranteed uniqueness.
Both temp file usages in the function are updated.

Fixes #2354

Agent: security-auditor

Co-authored-by: B <6723574+louisgv@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
A 2026-03-08 18:46:48 -07:00 committed by GitHub
parent 62e1df9be5
commit bd1399c861
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -31,14 +31,16 @@ _sprite_fix_config() {
# The sprite CLI's concurrent writes append an extra } at the end.
# Use grep on the whole file for any line that is just }}
if grep -q '^}}$' "${cfg}" 2>/dev/null; then
local tmp="${cfg}.fix$$"
local tmp
tmp=$(mktemp "${cfg}.XXXXXX") || return
sed 's/^}}$/}/' "${cfg}" > "${tmp}" 2>/dev/null && mv "${tmp}" "${cfg}" 2>/dev/null || rm -f "${tmp}"
fi
# Also check if last non-empty line ends with }}
local last_content
last_content=$(tail -5 "${cfg}" | grep -v '^$' | tail -1)
if printf '%s' "${last_content}" | grep -q '}}$'; then
local tmp="${cfg}.fix$$"
local tmp
tmp=$(mktemp "${cfg}.XXXXXX") || return
# Replace the LAST occurrence of }} with }
sed '$ s/}}$/}/' "${cfg}" > "${tmp}" 2>/dev/null && mv "${tmp}" "${cfg}" 2>/dev/null || rm -f "${tmp}"
fi