diff --git a/assets/views/layout/default.jet.html b/assets/views/layout/default.jet.html index b752faf..b8d2497 100644 --- a/assets/views/layout/default.jet.html +++ b/assets/views/layout/default.jet.html @@ -11,12 +11,12 @@ {{ end -}} {{ title }} - + {* TODO: figure out custom themes (e.g. charcoal theme) *} + - {* TODO: figure out custom themes (e.g. charcoal theme) *} - + diff --git a/build.sh b/build.sh index 44e05dc..f8ec47d 100755 --- a/build.sh +++ b/build.sh @@ -37,6 +37,7 @@ build() { echo "jq and semgrep found. " i18n fi + version_css CGO_ENABLED=0 go build -v -ldflags="-extldflags=-static -X codeberg.org/vnpower/pixivfe/v2/config.revision=${REVISION}" -o "${BINARY_NAME}" } @@ -46,6 +47,54 @@ build_docker() { CGO_ENABLED=0 go build -v -ldflags="-extldflags=-static -X codeberg.org/vnpower/pixivfe/v2/config.revision=${REVISION}" -o "${BINARY_NAME}" } +version_css() { + CSS_FILE="./assets/css/bootstrap-style.css" + HTML_FILE="./assets/views/layout/default.jet.html" + + echo "Computing SHA256 checksum for ${CSS_FILE}..." + + if [ ! -f "${CSS_FILE}" ]; then + echo "Error: ${CSS_FILE} does not exist." + exit 1 + fi + + # Compute checksum and truncate to first 8 characters for the href query parameter + checksum=$(sha256sum "${CSS_FILE}" | awk '{print $1}' | cut -c1-8) + echo "Truncated SHA256 checksum for ${CSS_FILE}: ${checksum}" + + # Compute the SRI hash for the integrity attribute + echo "Computing SRI hash for ${CSS_FILE}..." + # SRI_HASH=$(openssl dgst -sha256 -binary "${CSS_FILE}" | openssl base64 -A) + SRI_HASH=$(sha256sum "${CSS_FILE}" | awk '{print $1}' | xxd -r -p | base64 -w 0) + echo "Computed SRI hash: sha256-${SRI_HASH}" + + echo "Updating ${HTML_FILE} with the truncated checksum and SRI hash..." + + if [ ! -f "${HTML_FILE}" ]; then + echo "Error: ${HTML_FILE} does not exist." + exit 1 + fi + + # Create a backup before modifying + cp "${HTML_FILE}" "${HTML_FILE}.bak" + + # Replace the href attribute's query parameter with the new truncated hash + sed -i -E 's|(href="/css/bootstrap-style\.css)[^"]*(" [^>]*>)|\1?hash='"${checksum}"'\2|' "${HTML_FILE}" + + # Replace the integrity attribute with the new SRI hash + sed -i -E 's|(href="/css/bootstrap-style\.css[^"]*"[^>]*integrity=")[^"]*(")|\1sha256-'"${SRI_HASH}"'\2|' "${HTML_FILE}" + + # Check if sed was successful + if [ $? -eq 0 ]; then + echo "Successfully updated ${HTML_FILE} with the new truncated hash and SRI hash." + rm "${HTML_FILE}.bak" + else + echo "Failed to update ${HTML_FILE}. Restoring from backup." + mv "${HTML_FILE}.bak" "${HTML_FILE}" + exit 1 + fi +} + test() { echo "Running tests..." go test ./... @@ -123,6 +172,7 @@ help() { echo " build - Build the binary" echo " build_docker - Build the binary (for Docker, skips i18n refresh)" echo " scan - Scan Go code" + echo " version_css - Compute SHA256 checksum for bootstrap-style.css and update HTML" echo " test - Run tests" echo " i18n - Extract i18n strings" echo " i18n-up - Upload strings to Crowdin" @@ -148,6 +198,7 @@ execute_command() { fmt) fmt ;; build) build ;; build_docker) build_docker ;; + version_css) version_css ;; test) test ;; scan) scan ;; i18n) i18n ;;