fix: harden CHRNX responsive image builds

This commit is contained in:
chrnx committed 2026-08-13 01:22:17 -05:00
1 parent 6443a46126
commit 06d2c88b81
2 files changed
+286 -17

No files matched your search

+158 -16
View File
@@ -1,7 +1,7 @@
#!/bin/sh
set -eu
chrnx_image_script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
chrnx_image_script_dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
chrnx_image_root=$(dirname -- "$chrnx_image_script_dir")
chrnx_image_default_output="$chrnx_image_root/assets/images/generated"
chrnx_image_output=${1:-"$chrnx_image_default_output"}
@@ -25,10 +25,10 @@ if [ "$chrnx_image_output" != "$chrnx_image_default_output" ]; then
echo "Temporary image output does not exist: $chrnx_image_output" >&2
exit 2
fi
chrnx_image_safe_tmp=$(CDPATH= cd -P -- "${TMPDIR:-/tmp}" && pwd)
chrnx_image_safe_tmp=$(CDPATH='' cd -P -- "${TMPDIR:-/tmp}" && pwd)
chrnx_image_output_parent=$(dirname -- "$chrnx_image_output")
chrnx_image_output_name=$(basename -- "$chrnx_image_output")
chrnx_image_output_parent=$(CDPATH= cd -P -- "$chrnx_image_output_parent" && pwd)
chrnx_image_output_parent=$(CDPATH='' cd -P -- "$chrnx_image_output_parent" && pwd)
case "$chrnx_image_output_name" in
chrnx-*) ;;
*)
@@ -40,11 +40,8 @@ if [ "$chrnx_image_output" != "$chrnx_image_default_output" ]; then
echo "Temporary image output must be a direct child of $chrnx_image_safe_tmp." >&2
exit 2
fi
chrnx_image_output=$(CDPATH= cd -P -- "$chrnx_image_output" && pwd)
chrnx_image_output=$(CDPATH='' cd -P -- "$chrnx_image_output" && pwd)
fi
chrnx_image_tmp=$(mktemp -d "${TMPDIR:-/tmp}/chrnx-images.XXXXXX")
trap 'rm -rf -- "$chrnx_image_tmp"' EXIT HUP INT TERM
for chrnx_image_tool in sips cwebp avifenc; do
if ! command -v "$chrnx_image_tool" >/dev/null 2>&1; then
echo "Required image tool is missing: $chrnx_image_tool" >&2
@@ -53,6 +50,88 @@ for chrnx_image_tool in sips cwebp avifenc; do
done
mkdir -p -- "$chrnx_image_output"
chrnx_image_stage=$(mktemp -d "$chrnx_image_output/.chrnx-stage.XXXXXX")
trap 'rm -rf -- "$chrnx_image_stage"' EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
chrnx_image_tmp=$(mktemp -d "${TMPDIR:-/tmp}/chrnx-images.XXXXXX")
trap 'rm -rf -- "$chrnx_image_tmp" "$chrnx_image_stage"' EXIT
chrnx_image_build_output=$chrnx_image_stage
chrnx_image_backup="$chrnx_image_stage/.backup"
chrnx_image_discard="$chrnx_image_stage/.discard"
chrnx_image_promoted_log="$chrnx_image_stage/.promoted"
chrnx_image_promotion_active=0
chrnx_rollback_responsive_images() {
chrnx_image_rollback_failed=0
mkdir -p -- "$chrnx_image_discard"
if [ -f "$chrnx_image_promoted_log" ]; then
while IFS= read -r chrnx_image_name; do
chrnx_image_staged="$chrnx_image_stage/$chrnx_image_name"
chrnx_image_published="$chrnx_image_output/$chrnx_image_name"
if [ ! -e "$chrnx_image_staged" ] && [ ! -L "$chrnx_image_staged" ] && { [ -e "$chrnx_image_published" ] || [ -L "$chrnx_image_published" ]; }; then
if [ -d "$chrnx_image_published" ] || ! mv -f -- "$chrnx_image_published" "$chrnx_image_discard/$chrnx_image_name"; then
echo "Responsive image rollback could not remove: $chrnx_image_published" >&2
chrnx_image_rollback_failed=1
fi
fi
done < "$chrnx_image_promoted_log"
fi
if [ -d "$chrnx_image_backup" ]; then
for chrnx_image_backed_up in "$chrnx_image_backup"/*; do
if [ ! -e "$chrnx_image_backed_up" ] && [ ! -L "$chrnx_image_backed_up" ]; then
continue
fi
chrnx_image_name=$(basename -- "$chrnx_image_backed_up")
chrnx_image_published="$chrnx_image_output/$chrnx_image_name"
if [ -e "$chrnx_image_published" ] || [ -L "$chrnx_image_published" ]; then
if [ -d "$chrnx_image_published" ] || ! mv -f -- "$chrnx_image_published" "$chrnx_image_discard/$chrnx_image_name"; then
echo "Responsive image rollback could not clear: $chrnx_image_published" >&2
chrnx_image_rollback_failed=1
continue
fi
fi
if ! mv -f -- "$chrnx_image_backed_up" "$chrnx_image_published"; then
echo "Responsive image rollback could not restore: $chrnx_image_published" >&2
chrnx_image_rollback_failed=1
fi
done
fi
return "$chrnx_image_rollback_failed"
}
chrnx_cleanup_responsive_images() {
chrnx_image_status=$?
chrnx_image_rollback_failed=0
trap - EXIT HUP INT TERM
set +e
if [ "$chrnx_image_promotion_active" -eq 1 ]; then
if ! chrnx_rollback_responsive_images; then
chrnx_image_rollback_failed=1
if [ "$chrnx_image_status" -eq 0 ]; then
chrnx_image_status=1
fi
fi
fi
if ! rm -rf -- "$chrnx_image_tmp"; then
echo "Responsive image temporary cleanup failed: $chrnx_image_tmp" >&2
chrnx_image_status=1
fi
if [ "$chrnx_image_rollback_failed" -eq 0 ]; then
if ! rm -rf -- "$chrnx_image_stage"; then
echo "Responsive image staging cleanup failed: $chrnx_image_stage" >&2
chrnx_image_status=1
fi
else
echo "Responsive image rollback backup preserved at: $chrnx_image_stage" >&2
fi
exit "$chrnx_image_status"
}
trap 'chrnx_cleanup_responsive_images' EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
chrnx_build_responsive_image() {
chrnx_image_basename=$1
@@ -68,8 +147,8 @@ chrnx_build_responsive_image() {
for chrnx_image_width in 384 768; do
chrnx_image_resized="$chrnx_image_tmp/$chrnx_image_basename-$chrnx_image_width.$chrnx_image_source_extension"
sips --resampleWidth "$chrnx_image_width" "$chrnx_image_source" --out "$chrnx_image_resized" >/dev/null
cwebp -quiet -preset picture -q "$chrnx_image_webp_quality" -alpha_q 90 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_output/$chrnx_image_basename-$chrnx_image_width.webp"
avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 58 --qalpha 85 --speed 6 "$chrnx_image_resized" "$chrnx_image_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null
cwebp -quiet -preset picture -q "$chrnx_image_webp_quality" -alpha_q 90 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.webp"
avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 58 --qalpha 85 --speed 6 "$chrnx_image_resized" "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null
done
}
@@ -85,24 +164,87 @@ chrnx_build_responsive_background() {
for chrnx_image_width in 384 768 1536; do
chrnx_image_resized="$chrnx_image_tmp/$chrnx_image_basename-$chrnx_image_width.png"
sips --resampleWidth "$chrnx_image_width" "$chrnx_image_source" --out "$chrnx_image_resized" >/dev/null
cwebp -quiet -preset picture -q 62 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_output/$chrnx_image_basename-$chrnx_image_width.webp"
avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 52 --speed 6 "$chrnx_image_resized" "$chrnx_image_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null
cwebp -quiet -preset picture -q 62 -m 6 -metadata none "$chrnx_image_resized" -o "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.webp"
avifenc --ignore-exif --ignore-xmp --ignore-icc --qcolor 52 --speed 6 "$chrnx_image_resized" "$chrnx_image_build_output/$chrnx_image_basename-$chrnx_image_width.avif" >/dev/null
done
}
for chrnx_image_topic in code gaming life literature podcasts research; do
chrnx_build_responsive_image "alter-ego-$chrnx_image_topic" "alter-ego-$chrnx_image_topic.png"
printf '{\n\t"source": "alter-ego-%s.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' "$chrnx_image_topic" > "$chrnx_image_output/alter-ego-$chrnx_image_topic.json"
printf '{\n\t"source": "alter-ego-%s.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' "$chrnx_image_topic" > "$chrnx_image_build_output/alter-ego-$chrnx_image_topic.json"
done
chrnx_build_responsive_image "alter-ego-gaming-half-body" "alter-ego-gaming-half-body.png"
printf '{\n\t"source": "alter-ego-gaming-half-body.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_output/alter-ego-gaming-half-body.json"
printf '{\n\t"source": "alter-ego-gaming-half-body.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_build_output/alter-ego-gaming-half-body.json"
chrnx_build_responsive_image "the-no-names-musician-cutout" "the-no-names-musician-cutout.png"
printf '{\n\t"source": "the-no-names-musician-cutout.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_output/the-no-names-musician-cutout.json"
printf '{\n\t"source": "the-no-names-musician-cutout.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_build_output/the-no-names-musician-cutout.json"
chrnx_build_responsive_image "the-no-names-collage" "the-no-names-collage.jpeg" 58
printf '{\n\t"source": "the-no-names-collage.jpeg",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_output/the-no-names-collage.json"
printf '{\n\t"source": "the-no-names-collage.jpeg",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768 ]\n}\n' > "$chrnx_image_build_output/the-no-names-collage.json"
chrnx_build_responsive_background "podcast-frequency-grunge" "podcast-frequency-grunge.png"
printf '{\n\t"source": "podcast-frequency-grunge.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768, 1536 ]\n}\n' > "$chrnx_image_output/podcast-frequency-grunge.json"
printf '{\n\t"source": "podcast-frequency-grunge.png",\n\t"formats": [ "avif", "webp" ],\n\t"widths": [ 384, 768, 1536 ]\n}\n' > "$chrnx_image_build_output/podcast-frequency-grunge.json"
chrnx_validate_staged_family() {
chrnx_image_basename=$1
shift
for chrnx_image_width in "$@"; do
for chrnx_image_extension in avif webp; do
chrnx_image_staged="$chrnx_image_stage/$chrnx_image_basename-$chrnx_image_width.$chrnx_image_extension"
if [ ! -f "$chrnx_image_staged" ] || [ -L "$chrnx_image_staged" ] || [ ! -s "$chrnx_image_staged" ]; then
echo "Responsive image staging is incomplete: $chrnx_image_staged" >&2
exit 1
fi
chrnx_image_actual_width=$(sips -g pixelWidth "$chrnx_image_staged" 2>/dev/null | awk '/pixelWidth:/ { print $2 }')
if [ "$chrnx_image_actual_width" != "$chrnx_image_width" ]; then
echo "Responsive image staging width mismatch: $chrnx_image_staged" >&2
exit 1
fi
done
done
chrnx_image_manifest="$chrnx_image_stage/$chrnx_image_basename.json"
if [ ! -f "$chrnx_image_manifest" ] || [ -L "$chrnx_image_manifest" ] || [ ! -s "$chrnx_image_manifest" ]; then
echo "Responsive image staging is incomplete: $chrnx_image_manifest" >&2
exit 1
fi
}
for chrnx_image_topic in code gaming life literature podcasts research; do
chrnx_validate_staged_family "alter-ego-$chrnx_image_topic" 384 768
done
chrnx_validate_staged_family "alter-ego-gaming-half-body" 384 768
chrnx_validate_staged_family "the-no-names-musician-cutout" 384 768
chrnx_validate_staged_family "the-no-names-collage" 384 768
chrnx_validate_staged_family "podcast-frequency-grunge" 384 768 1536
set -- "$chrnx_image_stage"/*
if [ "$#" -ne 52 ]; then
echo "Responsive image staging expected 52 files, got $#." >&2
exit 1
fi
for chrnx_image_staged in "$@"; do
if [ ! -f "$chrnx_image_staged" ] || [ -L "$chrnx_image_staged" ]; then
echo "Responsive image staging contains an unsafe entry: $chrnx_image_staged" >&2
exit 1
fi
chrnx_image_name=$(basename -- "$chrnx_image_staged")
chrnx_image_published="$chrnx_image_output/$chrnx_image_name"
if [ -d "$chrnx_image_published" ]; then
echo "Refusing directory-shaped responsive image output: $chrnx_image_published" >&2
exit 1
fi
done
mkdir -p -- "$chrnx_image_backup"
chrnx_image_promotion_active=1
for chrnx_image_staged in "$@"; do
chrnx_image_name=$(basename -- "$chrnx_image_staged")
chrnx_image_published="$chrnx_image_output/$chrnx_image_name"
if [ -e "$chrnx_image_published" ] || [ -L "$chrnx_image_published" ]; then
mv -f -- "$chrnx_image_published" "$chrnx_image_backup/$chrnx_image_name"
fi
printf '%s\n' "$chrnx_image_name" >> "$chrnx_image_promoted_log"
mv -f -- "$chrnx_image_staged" "$chrnx_image_published"
done
chrnx_image_promotion_active=0