|
1 | 1 | #!/usr/bin/env bash -il |
| 2 | +SCRIPT_VERSION="v1.0.0" |
2 | 3 |
|
3 | 4 | GIT_ROOT=$(git rev-parse --show-toplevel 2>/dev/null) |
4 | 5 | SCRIPT_PATH=$(realpath --relative-to="$GIT_ROOT" "$0" 2>/dev/null || realpath "$0") |
@@ -79,12 +80,42 @@ a11y_scan() { |
79 | 80 | } |
80 | 81 |
|
81 | 82 | script_self_update() { |
82 | | - local remote_url="https://raw.githubusercontent.com/browserstack/AccessibilityDevTools/refs/heads/main/scripts/bash/cli.sh" |
| 83 | + local repo_base="https://raw.githubusercontent.com/browserstack/AccessibilityDevTools" |
| 84 | + local version_url="${repo_base}/refs/heads/main/scripts/latest-version.txt" |
| 85 | + local script_rel_path="bash/cli.sh" |
83 | 86 |
|
84 | | - updated_script=$(curl -R -z "$SCRIPT_PATH" "$remote_url") |
85 | | - if [[ $updated_script =~ ^#! ]]; then |
86 | | - echo "$updated_script" > "$SCRIPT_PATH" |
| 87 | + # Fetch remote version (lightweight metadata from main, not executable code) |
| 88 | + local remote_version |
| 89 | + remote_version=$(curl -fsSL --max-time 10 "$version_url" 2>/dev/null | tr -d '[:space:]') |
| 90 | + if [[ -z "$remote_version" || "$remote_version" == "$SCRIPT_VERSION" ]]; then |
| 91 | + return 0 |
87 | 92 | fi |
| 93 | + |
| 94 | + # Fetch script and checksums from immutable tagged ref |
| 95 | + local tag_base="${repo_base}/refs/tags/${remote_version}/scripts" |
| 96 | + local tmp_script tmp_sums |
| 97 | + tmp_script=$(mktemp) |
| 98 | + tmp_sums=$(mktemp) |
| 99 | + trap 'rm -f "$tmp_script" "$tmp_sums"' RETURN |
| 100 | + |
| 101 | + if ! curl -fsSL --max-time 30 "${tag_base}/${script_rel_path}" -o "$tmp_script" 2>/dev/null; then |
| 102 | + return 0 |
| 103 | + fi |
| 104 | + if ! curl -fsSL --max-time 10 "${tag_base}/SHA256SUMS" -o "$tmp_sums" 2>/dev/null; then |
| 105 | + return 0 |
| 106 | + fi |
| 107 | + |
| 108 | + # Verify SHA-256 checksum |
| 109 | + local expected actual |
| 110 | + expected=$(grep " ${script_rel_path}$" "$tmp_sums" | cut -d' ' -f1) |
| 111 | + actual=$(shasum -a 256 "$tmp_script" | cut -d' ' -f1) |
| 112 | + if [[ -z "$expected" || "$actual" != "$expected" ]]; then |
| 113 | + echo "[self-update] WARNING: Checksum verification failed for ${script_rel_path}. Update aborted." >&2 |
| 114 | + return 1 |
| 115 | + fi |
| 116 | + |
| 117 | + cp "$tmp_script" "$0" |
| 118 | + echo "[self-update] Updated to ${remote_version}." >&2 |
88 | 119 | } |
89 | 120 |
|
90 | 121 | download_binary() { |
|
0 commit comments