From e66a39d24bcc805fcf718c601b471e1f09ccd12d Mon Sep 17 00:00:00 2001 From: cedric Date: Mon, 5 Jan 2026 09:17:10 +0000 Subject: [PATCH] Add robust CSS injection and size limit validation - Ensure CSS is injected correctly even if is missing - Escape special characters in CSS to prevent sed errors - Validate --size argument is an integer - Support both .md and .markdown extensions for Markdown conversion - Improve temporary file handling --- content2url.sh | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/content2url.sh b/content2url.sh index b1ec0d2..a072424 100644 --- a/content2url.sh +++ b/content2url.sh @@ -1,7 +1,7 @@ #!/bin/bash # Usage: ./md2data_safe.sh FILE [--css CSS_FILE] [--size LIMIT] -# Converts HTML or Markdown to Base64 Data URL +# Converts HTML or Markdown to Base64 Data URL safely # Default size limit is 30000 characters # ----------------- Functions ----------------- @@ -24,6 +24,14 @@ check_file_exists() { fi } +validate_limit() { + local limit="$1" + if ! [[ "$limit" =~ ^[0-9]+$ ]]; then + echo "Error: size limit must be an integer." >&2 + exit 1 + fi +} + convert_md_to_html() { local file="$1" if ! command -v pandoc >/dev/null 2>&1; then @@ -41,8 +49,8 @@ minify_css() { escape_css_for_sed() { local css="$1" css=${css//\\/\\\\} # Escape backslash - css=${css//\//\\/} # Escape slashes css=${css//&/\\&} # Escape & + css=${css//\"/\\\"} # Escape double quotes echo "$css" } @@ -52,11 +60,12 @@ inject_css() { local escaped_css escaped_css=$(escape_css_for_sed "$css") - if grep -q "" "$html_file"; then + if grep -qi "" "$html_file"; then sed "/<\/head>/i " "$html_file" else - echo "" + echo "" cat "$html_file" + echo "" fi } @@ -96,6 +105,7 @@ while [[ $# -gt 0 ]]; do ;; --size) LIMIT="$2" + validate_limit "$LIMIT" shift 2 ;; -*) @@ -118,12 +128,11 @@ EXT="${FILE##*.}" # ----------------- Main Processing ----------------- -# Temporary HTML file for streaming TMP_HTML=$(mktemp) trap 'rm -f "$TMP_HTML"' EXIT # Convert Markdown or copy HTML -if [[ "$EXT" == "md" ]]; then +if [[ "$EXT" =~ ^(md|markdown)$ ]]; then convert_md_to_html "$FILE" > "$TMP_HTML" else cp "$FILE" "$TMP_HTML" @@ -134,6 +143,7 @@ if [[ -n "$CSS_FILE" ]]; then check_file_exists "$CSS_FILE" MIN_CSS=$(minify_css "$CSS_FILE") TMP_HTML_INJECTED=$(mktemp) + trap 'rm -f "$TMP_HTML" "$TMP_HTML_INJECTED"' EXIT inject_css "$TMP_HTML" "$MIN_CSS" > "$TMP_HTML_INJECTED" mv "$TMP_HTML_INJECTED" "$TMP_HTML" echo "Injected CSS size: ${#MIN_CSS} characters"