|
| 1 | +#!/bin/bash |
| 2 | +# -------------------------------------------------------------------------------------------- |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# Licensed under the MIT license. |
| 5 | +# -------------------------------------------------------------------------------------------- |
| 6 | +# |
| 7 | +# Validates PHP SHA256 hashes in constants.yml against official php.net releases. |
| 8 | +# Usage: ./validate-php-sha.sh [path/to/constants.yml] |
| 9 | +# |
| 10 | +# Exit codes: |
| 11 | +# 0 - All SHAs validated successfully |
| 12 | +# 1 - One or more SHA mismatches found |
| 13 | +# 2 - Script error (missing dependencies, file not found, etc.) |
| 14 | + |
| 15 | +set -euo pipefail |
| 16 | + |
| 17 | +CONSTANTS_FILE="${1:-images/constants.yml}" |
| 18 | + |
| 19 | +# Security: Restrict to expected file paths within the repository |
| 20 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 21 | +CONSTANTS_REALPATH="$(realpath -m "$CONSTANTS_FILE" 2>/dev/null || echo "")" |
| 22 | + |
| 23 | +# Validate path was resolved and is within repository |
| 24 | +if [[ -z "$CONSTANTS_REALPATH" ]]; then |
| 25 | + echo "Error: Could not resolve constants file path: $CONSTANTS_FILE" >&2 |
| 26 | + exit 2 |
| 27 | +fi |
| 28 | + |
| 29 | +case "$CONSTANTS_REALPATH" in |
| 30 | + "$REPO_ROOT"/*) |
| 31 | + # Path is safe - within repository |
| 32 | + ;; |
| 33 | + *) |
| 34 | + echo "Error: Constants file must be within the repository: $REPO_ROOT" >&2 |
| 35 | + exit 2 |
| 36 | + ;; |
| 37 | +esac |
| 38 | + |
| 39 | +# Colors for output (disabled if not a terminal) |
| 40 | +if [[ -t 1 ]]; then |
| 41 | + RED='\033[0;31m' |
| 42 | + GREEN='\033[0;32m' |
| 43 | + YELLOW='\033[0;33m' |
| 44 | + NC='\033[0m' # No Color |
| 45 | +else |
| 46 | + RED='' |
| 47 | + GREEN='' |
| 48 | + YELLOW='' |
| 49 | + NC='' |
| 50 | +fi |
| 51 | + |
| 52 | +# Check dependencies |
| 53 | +for cmd in curl jq grep; do |
| 54 | + if ! command -v "$cmd" &> /dev/null; then |
| 55 | + echo "Error: Required command '$cmd' not found" >&2 |
| 56 | + exit 2 |
| 57 | + fi |
| 58 | +done |
| 59 | + |
| 60 | +# Function to fetch with retry logic |
| 61 | +fetch_with_retry() { |
| 62 | + local url="$1" |
| 63 | + local max_attempts=3 |
| 64 | + local timeout=10 |
| 65 | + local result="" |
| 66 | + |
| 67 | + for attempt in $(seq 1 $max_attempts); do |
| 68 | + result=$(curl -sf --max-time "$timeout" --proto '=https' --tlsv1.2 "$url" 2>/dev/null) && break |
| 69 | + [[ $attempt -lt $max_attempts ]] && sleep $((attempt * 2)) |
| 70 | + done |
| 71 | + |
| 72 | + echo "$result" |
| 73 | +} |
| 74 | + |
| 75 | +echo "Validating PHP SHA256 hashes from: $CONSTANTS_FILE" |
| 76 | +echo "==================================================" |
| 77 | + |
| 78 | +failed=0 |
| 79 | +validated=0 |
| 80 | + |
| 81 | +# Extract PHP versions and SHAs from constants.yml |
| 82 | +while IFS= read -r line; do |
| 83 | + # Match lines like: php85Version: 8.5.1 (with optional leading spaces) |
| 84 | + if [[ "$line" =~ ^[[:space:]]*php([0-9]+)Version:[[:space:]]*([0-9.]+)$ ]]; then |
| 85 | + php_key="php${BASH_REMATCH[1]}" |
| 86 | + version="${BASH_REMATCH[2]}" |
| 87 | + |
| 88 | + # Security: Validate version format strictly (only digits and dots, reasonable length) |
| 89 | + if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || [[ ${#version} -gt 20 ]]; then |
| 90 | + echo "Warning: Skipping invalid version format: $version" >&2 |
| 91 | + continue |
| 92 | + fi |
| 93 | + |
| 94 | + # Get corresponding SHA from constants.yml using fixed string match |
| 95 | + sha_line=$(grep -F "${php_key}Version_SHA:" "$CONSTANTS_FILE" 2>/dev/null || true) |
| 96 | + if [[ "$sha_line" =~ _SHA:[[:space:]]*([a-f0-9]{64})$ ]]; then |
| 97 | + local_sha="${BASH_REMATCH[1]}" |
| 98 | + |
| 99 | + # Security: Sanitize output to prevent log injection |
| 100 | + safe_version="${version//[^0-9.]/}" |
| 101 | + echo -n "PHP $safe_version: " |
| 102 | + |
| 103 | + # Fetch official SHA from php.net with retry (URL-encode version just in case) |
| 104 | + encoded_version=$(printf '%s' "$version" | jq -sRr @uri) |
| 105 | + response=$(fetch_with_retry "https://www.php.net/releases/?json&version=${encoded_version}") |
| 106 | + |
| 107 | + # Validate JSON response before parsing |
| 108 | + if [[ -n "$response" ]] && ! echo "$response" | jq empty 2>/dev/null; then |
| 109 | + echo -e "${YELLOW}WARNING - Invalid JSON response from php.net${NC}" |
| 110 | + continue |
| 111 | + fi |
| 112 | + |
| 113 | + official=$(echo "$response" | jq -r '.source[] | select(.filename | endswith(".tar.xz")) | .sha256 // empty' 2>/dev/null || echo "") |
| 114 | + |
| 115 | + # Security: Validate response is a valid SHA256 (64 hex chars) |
| 116 | + if [[ -n "$official" ]] && [[ ! "$official" =~ ^[a-f0-9]{64}$ ]]; then |
| 117 | + echo -e "${YELLOW}WARNING - Invalid SHA format from php.net${NC}" |
| 118 | + continue |
| 119 | + fi |
| 120 | + |
| 121 | + if [[ -z "$official" ]]; then |
| 122 | + echo -e "${YELLOW}WARNING - Could not fetch from php.net${NC}" |
| 123 | + elif [[ "$official" != "$local_sha" ]]; then |
| 124 | + echo -e "${RED}FAILED${NC}" |
| 125 | + echo " Expected: $official" |
| 126 | + echo " Got: $local_sha" |
| 127 | + failed=1 |
| 128 | + else |
| 129 | + echo -e "${GREEN}OK${NC}" |
| 130 | + validated=$((validated + 1)) |
| 131 | + fi |
| 132 | + else |
| 133 | + # Version found but no SHA - this is likely a configuration error |
| 134 | + safe_version="${version//[^0-9.]/}" |
| 135 | + echo -e "PHP $safe_version: ${YELLOW}WARNING - Version found but ${php_key}Version_SHA is missing${NC}" |
| 136 | + fi |
| 137 | + fi |
| 138 | +done < "$CONSTANTS_FILE" || { |
| 139 | + echo "Error: Could not read constants file: $CONSTANTS_FILE" >&2 |
| 140 | + exit 2 |
| 141 | +} |
| 142 | + |
| 143 | +echo "==================================================" |
| 144 | +echo "Validated: $validated PHP version(s)" |
| 145 | + |
| 146 | +if [[ $failed -eq 1 ]]; then |
| 147 | + echo -e "${RED}VALIDATION FAILED - SHA256 mismatches detected!${NC}" |
| 148 | + exit 1 |
| 149 | +else |
| 150 | + echo -e "${GREEN}VALIDATION PASSED${NC}" |
| 151 | + exit 0 |
| 152 | +fi |
0 commit comments