Przeglądaj źródła

build(ci): pin actions and verify downloaded artifacts (#1630)

Pin every GitHub Action to a full commit SHA and verify OpenWrt SDKs, release assets and the gitleaks tarball against their published checksums.

Closes #1629
Nacho 1 miesiąc temu
rodzic
commit
6b94750b94

+ 3 - 3
.github/workflows/dist.yml

@@ -19,9 +19,9 @@ jobs:
     permissions:
       contents: write # publicar assets en la prerelease dist-latest
     steps:
-      - uses: actions/checkout@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
 
-      - uses: actions/setup-node@v4
+      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
         with:
           node-version: 24
           cache: npm
@@ -35,7 +35,7 @@ jobs:
         run: tar czf app-dist-${{ github.sha }}.tar.gz -C app/dist .
 
       - name: Publica en prerelease dist-latest
-        uses: softprops/action-gh-release@v2
+        uses: softprops/action-gh-release@3bb12739c298aeb8a4eeaf626c5b8d85266b0e65 # v2.6.2
         with:
           tag_name: dist-latest
           name: dist-latest

+ 7 - 2
.github/workflows/gitleaks.yml

@@ -4,9 +4,14 @@ jobs:
   gitleaks:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
         with: { fetch-depth: 0 }
       - name: Instalar gitleaks
-        run: curl -sL https://github.com/gitleaks/gitleaks/releases/download/v8.30.1/gitleaks_8.30.1_linux_x64.tar.gz | sudo tar xz -C /usr/local/bin gitleaks
+        run: |
+          set -euo pipefail
+          curl -fsSL -o /tmp/gitleaks.tar.gz \
+            https://github.com/gitleaks/gitleaks/releases/download/v8.30.1/gitleaks_8.30.1_linux_x64.tar.gz
+          echo "551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb  /tmp/gitleaks.tar.gz" | sha256sum -c -
+          sudo tar xzf /tmp/gitleaks.tar.gz -C /usr/local/bin gitleaks
       - name: Escanear secretos en todo el historial
         run: gitleaks git . --redact --no-banner

+ 6 - 6
.github/workflows/go.yml

@@ -19,8 +19,8 @@ jobs:
   lint:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v4
-      - uses: actions/setup-node@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
+      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
         with:
           node-version: 24
           cache: npm
@@ -37,7 +37,7 @@ jobs:
       matrix:
         arch: [amd64, arm64]
     steps:
-      - uses: actions/checkout@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
         with:
           fetch-depth: 0 # tags necesarias para git describe (httpapi.Version)
 
@@ -56,13 +56,13 @@ jobs:
           fi
           echo "AGENT_VERSION=$AGENT_VERSION" >> "$GITHUB_ENV"
 
-      - uses: actions/setup-node@v4
+      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
         with:
           node-version: 24
           cache: npm
           cache-dependency-path: app/package-lock.json
 
-      - uses: actions/setup-go@v5
+      - uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
         with:
           go-version: '1.26'
           cache-dependency-path: server-go/go.sum
@@ -123,7 +123,7 @@ jobs:
         run: tar czf netpulse-server-${{ github.sha }}-linux-${{ matrix.arch }}.tar.gz -C server-go netpulse .env.example -C .. deploy
 
       - name: Publica en prerelease go-latest
-        uses: softprops/action-gh-release@v2
+        uses: softprops/action-gh-release@3bb12739c298aeb8a4eeaf626c5b8d85266b0e65 # v2.6.2
         with:
           tag_name: go-latest
           name: go-latest

+ 27 - 15
.github/workflows/openwrt-package.yml

@@ -57,7 +57,7 @@ jobs:
             goarch: amd64
 
     steps:
-      - uses: actions/checkout@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
 
       - name: Resolve release tag
         id: tag
@@ -76,14 +76,16 @@ jobs:
           TAG: ${{ steps.tag.outputs.tag }}
         run: |
           for i in $(seq 1 80); do
-            if gh release view "$TAG" --json assets --jq '.assets[].name' 2>/dev/null | grep -q "netpulse-agent_.*_linux_${{ matrix.goarch }}.tar.gz"; then
-              echo "Agent tarballs present after $i attempts"
+            assets="$(gh release view "$TAG" --json assets --jq '.assets[].name' 2>/dev/null || true)"
+            if printf '%s\n' "$assets" | grep -q "netpulse-agent_.*_linux_${{ matrix.goarch }}.tar.gz" \
+              && printf '%s\n' "$assets" | grep -qx 'checksums.txt'; then
+              echo "Agent tarballs and checksums present after $i attempts"
               exit 0
             fi
-            echo "Waiting for agent tarballs ($i/80)..."
+            echo "Waiting for agent tarballs and checksums ($i/80)..."
             sleep 15
           done
-          echo "ERROR: agent tarballs not found on $TAG after 20 min" >&2
+          echo "ERROR: agent tarballs or checksums not found on $TAG after 20 min" >&2
           exit 1
 
       - name: Download netpulse-agent ${{ matrix.goarch }} binary
@@ -91,9 +93,13 @@ jobs:
           GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
           TAG: ${{ steps.tag.outputs.tag }}
         run: |
-          gh release download "$TAG" -p "netpulse-agent_*_linux_${{ matrix.goarch }}.tar.gz" --dir /tmp/agent
+          gh release download "$TAG" -p "netpulse-agent_*_linux_${{ matrix.goarch }}.tar.gz" -p checksums.txt --dir /tmp/agent
           cd /tmp/agent
-          tar xzf netpulse-agent_*_linux_${{ matrix.goarch }}.tar.gz
+          asset="$(ls netpulse-agent_*_linux_${{ matrix.goarch }}.tar.gz)"
+          awk -v n="$asset" '{candidate=$NF; sub(/^\*/, "", candidate); if (candidate == n) print}' checksums.txt > agent-checksums.txt
+          test -s agent-checksums.txt
+          sha256sum -c agent-checksums.txt
+          tar xzf "$asset"
           ls -la netpulse-agent
           file netpulse-agent
 
@@ -134,7 +140,7 @@ jobs:
             format: apk
 
     steps:
-      - uses: actions/checkout@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
 
       - name: Resolve release tag
         id: tag
@@ -209,7 +215,7 @@ jobs:
             goarch: amd64
 
     steps:
-      - uses: actions/checkout@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
 
       - name: Resolve release tag
         id: tag
@@ -228,14 +234,16 @@ jobs:
           TAG: ${{ steps.tag.outputs.tag }}
         run: |
           for i in $(seq 1 80); do
-            if gh release view "$TAG" --json assets --jq '.assets[].name' 2>/dev/null | grep -q "netpulse_.*_linux_${{ matrix.goarch }}.tar.gz"; then
-              echo "Server tarball present after $i attempts"
+            assets="$(gh release view "$TAG" --json assets --jq '.assets[].name' 2>/dev/null || true)"
+            if printf '%s\n' "$assets" | grep -q "netpulse_.*_linux_${{ matrix.goarch }}.tar.gz" \
+              && printf '%s\n' "$assets" | grep -qx 'checksums.txt'; then
+              echo "Server tarball and checksums present after $i attempts"
               exit 0
             fi
-            echo "Waiting for server tarball ($i/80)..."
+            echo "Waiting for server tarball and checksums ($i/80)..."
             sleep 15
           done
-          echo "ERROR: server tarball not found on $TAG after 20 min" >&2
+          echo "ERROR: server tarball or checksums not found on $TAG after 20 min" >&2
           exit 1
 
       - name: Download netpulse server ${{ matrix.goarch }} binary
@@ -243,9 +251,13 @@ jobs:
           GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
           TAG: ${{ steps.tag.outputs.tag }}
         run: |
-          gh release download "$TAG" -p "netpulse_*_linux_${{ matrix.goarch }}.tar.gz" --dir /tmp/server
+          gh release download "$TAG" -p "netpulse_*_linux_${{ matrix.goarch }}.tar.gz" -p checksums.txt --dir /tmp/server
           cd /tmp/server
-          tar xzf netpulse_*_linux_${{ matrix.goarch }}.tar.gz
+          asset="$(ls netpulse_*_linux_${{ matrix.goarch }}.tar.gz)"
+          awk -v n="$asset" '{candidate=$NF; sub(/^\*/, "", candidate); if (candidate == n) print}' checksums.txt > server-checksums.txt
+          test -s server-checksums.txt
+          sha256sum -c server-checksums.txt
+          tar xzf "$asset"
           ls -la netpulse
           file netpulse
 

+ 5 - 5
.github/workflows/pr.yml

@@ -17,8 +17,8 @@ jobs:
   eslint:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v4
-      - uses: actions/setup-node@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
+      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
         with:
           node-version: 24
           cache: npm
@@ -30,15 +30,15 @@ jobs:
   server:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
 
-      - uses: actions/setup-node@v4
+      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
         with:
           node-version: 24
           cache: npm
           cache-dependency-path: app/package-lock.json
 
-      - uses: actions/setup-go@v5
+      - uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
         with:
           go-version: '1.26'
           cache-dependency-path: |

+ 6 - 6
.github/workflows/release.yml

@@ -18,17 +18,17 @@ jobs:
   release:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v4
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
         with:
           fetch-depth: 0 # tags e historia: el gate de agent/VERSION difiere contra el tag anterior
 
-      - uses: actions/setup-node@v4
+      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
         with:
           node-version: 24
           cache: npm
           cache-dependency-path: app/package-lock.json
 
-      - uses: actions/setup-go@v5
+      - uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
         with:
           go-version: '1.26'
           cache-dependency-path: |
@@ -153,14 +153,14 @@ jobs:
           (cd collector && go test ./...)
 
       - name: Valida config goreleaser
-        uses: goreleaser/goreleaser-action@v6
+        uses: goreleaser/goreleaser-action@e435ccd777264be153ace6237001ef4d979d3a7a # v6.4.0
         with:
           version: '~> v2'
           args: check
 
       - name: Release (tag v* — publica)
         if: github.ref_type == 'tag'
-        uses: goreleaser/goreleaser-action@v6
+        uses: goreleaser/goreleaser-action@e435ccd777264be153ace6237001ef4d979d3a7a # v6.4.0
         with:
           version: '~> v2'
           args: release --clean
@@ -170,7 +170,7 @@ jobs:
 
       - name: Release (workflow_dispatch — snapshot sin publicar)
         if: github.ref_type != 'tag'
-        uses: goreleaser/goreleaser-action@v6
+        uses: goreleaser/goreleaser-action@e435ccd777264be153ace6237001ef4d979d3a7a # v6.4.0
         with:
           version: '~> v2'
           args: release --clean --snapshot

+ 3 - 12
deploy/openwrt/package-luci.sh

@@ -41,19 +41,10 @@ PKG_RELEASE="${PKG_RELEASE:-$(sed -n 's/^PKG_RELEASE:=//p' "$APP_DIR/Makefile" |
 # toolchain component differs across OpenWrt releases); same scheme as
 # package.sh.
 SDK_DIR_URL="https://downloads.openwrt.org/releases/${SDK_VERSION}/targets/${SDK_TARGET}/"
-SDK_NAME="$(curl -fsSL "$SDK_DIR_URL" 2>/dev/null | grep -o 'openwrt-sdk-[^"]*\.tar\.zst' | head -1 || true)"
-if [ -z "$SDK_NAME" ]; then
-  echo "ERROR: SDK not found under ${SDK_DIR_URL}" >&2
-  exit 1
-fi
-SDK_URL="${SDK_DIR_URL}${SDK_NAME}"
-
-echo "  SDK: $SDK_URL"
-
+# shellcheck source=deploy/openwrt/sdk.sh
+. "$SCRIPT_DIR/sdk.sh"
+SDK_DIR="$(download_verified_sdk "$SDK_DIR_URL" "$WORK_DIR")" || exit 1
 cd "$WORK_DIR"
-curl -fsSL "$SDK_URL" -o sdk.tar.zst
-tar --zstd -xf sdk.tar.zst
-SDK_DIR="$WORK_DIR/${SDK_NAME%.tar.zst}"
 
 OUT_DIR="$REPO_ROOT/dist/packages"
 mkdir -p "$OUT_DIR"

+ 3 - 11
deploy/openwrt/package-server.sh

@@ -39,18 +39,10 @@ WORK_DIR="$(mktemp -d)"
 trap 'rm -rf "$WORK_DIR"' EXIT
 
 SDK_DIR_URL="https://downloads.openwrt.org/releases/${SDK_VERSION}/targets/${SDK_TARGET}/"
-SDK_NAME="$(curl -fsSL "$SDK_DIR_URL" 2>/dev/null | grep -o 'openwrt-sdk-[^"]*\.tar\.zst' | head -1 || true)"
-if [ -z "$SDK_NAME" ]; then
-  echo "ERROR: SDK not found under ${SDK_DIR_URL}" >&2
-  exit 1
-fi
-SDK_URL="${SDK_DIR_URL}${SDK_NAME}"
-echo "  SDK: $SDK_URL"
-
+# shellcheck source=deploy/openwrt/sdk.sh
+. "$SCRIPT_DIR/sdk.sh"
+SDK_DIR="$(download_verified_sdk "$SDK_DIR_URL" "$WORK_DIR")" || exit 1
 cd "$WORK_DIR"
-curl -fsSL "$SDK_URL" -o sdk.tar.zst
-tar --zstd -xf sdk.tar.zst
-SDK_DIR="$WORK_DIR/${SDK_NAME%.tar.zst}"
 
 # Package architecture for the .ipk control file. OpenWrt names the staging
 # dir "target-<arch>_musl", so derive it from there instead of hardcoding a

+ 3 - 13
deploy/openwrt/package.sh

@@ -47,20 +47,10 @@ trap 'rm -rf "$WORK_DIR"' EXIT
 # (24.10 uses gcc-13.3.0, 25.12 uses gcc-14.3.0), so resolve the real name
 # from the download index instead of hardcoding it.
 SDK_DIR_URL="https://downloads.openwrt.org/releases/${SDK_VERSION}/targets/${SDK_TARGET}/"
-SDK_NAME="$(curl -fsSL "$SDK_DIR_URL" 2>/dev/null | grep -o 'openwrt-sdk-[^"]*\.tar\.zst' | head -1 || true)"
-if [ -z "$SDK_NAME" ]; then
-  echo "ERROR: SDK not found under ${SDK_DIR_URL}" >&2
-  exit 1
-fi
-SDK_URL="${SDK_DIR_URL}${SDK_NAME}"
-
-echo "  SDK: $SDK_URL"
-
-# Download and extract the SDK
+# shellcheck source=deploy/openwrt/sdk.sh
+. "$SCRIPT_DIR/sdk.sh"
+SDK_DIR="$(download_verified_sdk "$SDK_DIR_URL" "$WORK_DIR")" || exit 1
 cd "$WORK_DIR"
-curl -fsSL "$SDK_URL" -o sdk.tar.zst
-tar --zstd -xf sdk.tar.zst
-SDK_DIR="$WORK_DIR/${SDK_NAME%.tar.zst}"
 
 # Package architecture for the .ipk control file. OpenWrt names the staging
 # dir "target-<arch>_musl", so derive it from there instead of hardcoding a

+ 60 - 0
deploy/openwrt/sdk.sh

@@ -0,0 +1,60 @@
+#!/bin/bash
+# Downloads an OpenWrt SDK and verifies it against the target sha256sums
+# before extracting it. Sourced by package*.sh.
+#
+# Usage:
+#   SDK_DIR="$(download_verified_sdk "$SDK_DIR_URL" "$WORK_DIR")" || exit 1
+
+download_verified_sdk() {
+  local dir_url="$1"
+  local work_dir="$2"
+  local name sums line sdk_path
+
+  sums="$work_dir/sha256sums"
+  if ! curl -fsSL "${dir_url}sha256sums" -o "$sums"; then
+    echo "ERROR: no sha256sums under ${dir_url}" >&2
+    return 1
+  fi
+
+  name="$(curl -fsSL "$dir_url" 2>/dev/null | grep -o 'openwrt-sdk-[^"]*\.tar\.zst' | head -1 || true)"
+  if [ -z "$name" ]; then
+    echo "ERROR: SDK not found under ${dir_url}" >&2
+    return 1
+  fi
+
+  line="$(awk -v n="$name" '
+    {
+      candidate = $NF
+      sub(/^\*/, "", candidate)
+      if (candidate == n) {
+        print
+        found = 1
+      }
+    }
+    END { exit found ? 0 : 1 }
+  ' "$sums")" || {
+    echo "ERROR: ${name} is not listed in sha256sums" >&2
+    return 1
+  }
+
+  printf '%s\n' "$line" > "$work_dir/sdk.sha256"
+  echo "  SDK: ${dir_url}${name}" >&2
+
+  sdk_path="$work_dir/$name"
+  if ! curl -fsSL "${dir_url}${name}" -o "$sdk_path"; then
+    echo "ERROR: could not download ${name}" >&2
+    return 1
+  fi
+
+  if ! (cd "$work_dir" && sha256sum -c sdk.sha256) >&2; then
+    echo "ERROR: SDK checksum mismatch for ${name}" >&2
+    return 1
+  fi
+
+  if ! tar --zstd -xf "$sdk_path" -C "$work_dir"; then
+    echo "ERROR: could not extract ${name}" >&2
+    return 1
+  fi
+
+  printf '%s\n' "$work_dir/${name%.tar.zst}"
+}