From 0d1969aa8b9d4f6e8d79f420cec185271ca6e4c3 Mon Sep 17 00:00:00 2001 From: Hironori Takahashi Date: Wed, 2 Dec 2026 20:21:25 +0900 Subject: [PATCH] fix: fail closed on policy resolution errors and normalize the checked manifest --- .github/workflows/cd.yml | 17 +++++++++++++++-- .github/workflows/ci.yml | 15 ++++++++++++++- .gitlab/cd.yml | 2 +- docs/webapp-policy.ja.md | 8 +++++++- docs/webapp-policy.md | 8 +++++++- scripts/check-webapp-policy.sh | 23 ++++++++++++++++++++++- 6 files changed, 66 insertions(+), 7 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 31512df..37041ae 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -60,7 +60,20 @@ jobs: WEBAPP_EXECUTE_AS_POLICY_VAR: ${{ vars.WEBAPP_EXECUTE_AS_POLICY }} WEBAPP_POLICY_HELP_URL: ${{ vars.WEBAPP_POLICY_HELP_URL }} run: | - props=$(gh api "repos/${GITHUB_REPOSITORY}/properties/values" 2>/dev/null || echo "[]") + # Fail closed on resolution errors. HTTP 404 = custom properties + # unavailable (user-owned repo) → fall back to vars. + err=$(mktemp) + if ! props=$(gh api "repos/${GITHUB_REPOSITORY}/properties/values" 2>"$err"); then + if grep -q "HTTP 404" "$err"; then + props="[]" + else + echo "::error::custom property resolution failed:" + cat "$err" + exit 1 + fi + fi + printf '%s' "$props" | node -e "JSON.parse(require('fs').readFileSync(0,'utf8'))" \ + || { echo "::error::custom property payload is not valid JSON"; exit 1; } pick() { printf '%s' "$props" | node -e " const chunks=[]; process.stdin.on('data',c=>chunks.push(c)).on('end',()=>{ @@ -78,7 +91,7 @@ jobs: export WEBAPP_EXECUTE_AS_POLICY="${exec_prop:-$WEBAPP_EXECUTE_AS_POLICY_VAR}" WEBAPP_ACCESS_ALLOW_RESOLVED=$(pick webapp-access-allow) export WEBAPP_ACCESS_ALLOW_RESOLVED - ./.policy-gate/scripts/check-webapp-policy.sh --manifest dist/appsscript.json + ./.policy-gate/scripts/check-webapp-policy.sh --manifest dist/appsscript.json --normalize # Secret Manager mode: direct WIF (no service-account impersonation). # Requires org variables GCP_WIF_PROVIDER + CLASPRC_SECRET (docs/secret-manager.md). diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1b5224e..0d89a1f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,20 @@ jobs: WEBAPP_EXECUTE_AS_POLICY_VAR: ${{ vars.WEBAPP_EXECUTE_AS_POLICY }} WEBAPP_POLICY_HELP_URL: ${{ vars.WEBAPP_POLICY_HELP_URL }} run: | - props=$(gh api "repos/${GITHUB_REPOSITORY}/properties/values" 2>/dev/null || echo "[]") + # Fail closed on resolution errors. HTTP 404 = custom properties + # unavailable (user-owned repo) → fall back to vars. + err=$(mktemp) + if ! props=$(gh api "repos/${GITHUB_REPOSITORY}/properties/values" 2>"$err"); then + if grep -q "HTTP 404" "$err"; then + props="[]" + else + echo "::error::custom property resolution failed:" + cat "$err" + exit 1 + fi + fi + printf '%s' "$props" | node -e "JSON.parse(require('fs').readFileSync(0,'utf8'))" \ + || { echo "::error::custom property payload is not valid JSON"; exit 1; } pick() { printf '%s' "$props" | node -e " const chunks=[]; process.stdin.on('data',c=>chunks.push(c)).on('end',()=>{ diff --git a/.gitlab/cd.yml b/.gitlab/cd.yml index d215d78..dfeccc1 100644 --- a/.gitlab/cd.yml +++ b/.gitlab/cd.yml @@ -21,7 +21,7 @@ WEBAPP_ACCESS_ALLOW_RESOLVED=$(node -e 'const m=JSON.parse(process.argv[1]);const v=m[process.argv[2]];process.stdout.write(v===undefined?"":String(v))' "$WEBAPP_ACCESS_ALLOWLIST" "$CI_PROJECT_PATH") export WEBAPP_ACCESS_ALLOW_RESOLVED fi - ./scripts/check-webapp-policy.sh --manifest dist/appsscript.json + ./scripts/check-webapp-policy.sh --manifest dist/appsscript.json --normalize # Dual-mode credentials: GCP_WIF_PROVIDER set → Secret Manager (no silent # fallback); unset → legacy CLASPRC_JSON; neither → explicit error. # See docs/secret-manager.md. diff --git a/docs/webapp-policy.ja.md b/docs/webapp-policy.ja.md index 5a8de6f..3d339b7 100644 --- a/docs/webapp-policy.ja.md +++ b/docs/webapp-policy.ja.md @@ -72,7 +72,13 @@ repo 変数は**自己申告**です。リポの admin が自分のリポに `AN パイプライン定義で動く。 - **CD のチェックは `dist/appsscript.json`** — clasp が実際に push する manifest — を、build と pre-deploy hook の**後**に検査する。後続ステップが - ゲートの背後で manifest を書き換えることはできない。 + ゲートの背後で manifest を書き換えることはできない。`--normalize`(CD の + デフォルト)ではゲートがパースしたオブジェクトそのもので manifest を書き + 戻すため、JSON の重複キーでゲートと Apps Script API に別の値を見せる + ことはできない。 +- **解決は fail-closed。** custom property API のエラー(404 = property 機能 + なし、を除く)や不正なペイロードは、静かなフォールバックではなくデプロイ + 失敗になる。 - **`ci.yml` の PR チェック — 補助のみ。** 早期フィードバック用。PR は自分の CI 定義を書き換えられるため、統制としては数えない。 - **残存リスク(GitHub)**: デプロイ対象ブランチの build スクリプトと hook は diff --git a/docs/webapp-policy.md b/docs/webapp-policy.md index 4cd0db7..c8d8f96 100644 --- a/docs/webapp-policy.md +++ b/docs/webapp-policy.md @@ -75,7 +75,13 @@ CI definitions, so the script stays platform-neutral and template-synced. definition of the protected branch. - **The CD check inspects `dist/appsscript.json`** — the manifest clasp actually pushes — and runs *after* the build and the pre-deploy hook, so a - later step cannot rewrite the manifest behind the gate. + later step cannot rewrite the manifest behind the gate. With `--normalize` + (CD default) the gate rewrites the manifest as the exact object it parsed, + so duplicate JSON keys cannot show the gate one value and the Apps Script + API another. +- **Resolution fails closed.** A custom-property API error (other than 404 = + properties unavailable) or an invalid payload fails the deploy instead of + silently falling back. - **`ci.yml` PR check — advisory only.** It gives early feedback, but a PR can modify its own CI definition, so it does not count as a control. - **Residual risk (GitHub)**: build scripts and hooks from the deployed branch diff --git a/scripts/check-webapp-policy.sh b/scripts/check-webapp-policy.sh index cc68dc1..dba8ba5 100755 --- a/scripts/check-webapp-policy.sh +++ b/scripts/check-webapp-policy.sh @@ -12,7 +12,11 @@ set -euo pipefail # edit its own CI definition. # # Usage: -# ./scripts/check-webapp-policy.sh [--manifest appsscript.json] +# ./scripts/check-webapp-policy.sh [--manifest appsscript.json] [--normalize] +# +# --normalize (CD use): on pass, rewrite the manifest as the exact JSON object +# this check parsed, so the uploader cannot see different values via duplicate +# keys (parser differential between JSON.parse and the Apps Script API). # # Environment: # WEBAPP_ACCESS_POLICY @@ -31,6 +35,7 @@ set -euo pipefail # Optional URL shown on violation (who to ask for an exception). MANIFEST="appsscript.json" +NORMALIZE=0 while [[ $# -gt 0 ]]; do case $1 in @@ -38,6 +43,10 @@ while [[ $# -gt 0 ]]; do MANIFEST="$2" shift 2 ;; + --normalize) + NORMALIZE=1 + shift + ;; *) echo "Unknown option: $1" >&2 exit 1 @@ -74,6 +83,16 @@ fi command -v node >/dev/null 2>&1 || die "node is required but not installed." [[ -f "$MANIFEST" ]] || die "manifest '${MANIFEST}' not found." +normalize_manifest() { + [[ "$NORMALIZE" -eq 1 ]] || return 0 + node -e " + const fs = require('fs'); + const m = JSON.parse(fs.readFileSync(process.argv[1], 'utf8')); + fs.writeFileSync(process.argv[1], JSON.stringify(m, null, 2) + '\n'); + " "$MANIFEST" || die "failed to normalize ${MANIFEST}." + echo "webapp-policy: normalized ${MANIFEST} to the parsed object." +} + webapp_field() { node -e " const m = JSON.parse(require('fs').readFileSync(process.argv[1], 'utf8')); @@ -84,6 +103,7 @@ webapp_field() { ACCESS=$(webapp_field access) || die "failed to parse ${MANIFEST}." if [[ "$ACCESS" == "__NO_WEBAPP__" ]]; then + normalize_manifest echo "webapp-policy: no webapp section in ${MANIFEST} — nothing to check." exit 0 fi @@ -117,4 +137,5 @@ if [[ "$EXECUTE_AS" == "USER_DEPLOYING" && "$(rank "$ACCESS")" -gt "$(rank DOMAI echo "webapp-policy: WARNING: ${MSG}" >&2 fi +normalize_manifest echo "webapp-policy: OK."