Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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',()=>{
Expand All @@ -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).
Expand Down
15 changes: 14 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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',()=>{
Expand Down
2 changes: 1 addition & 1 deletion .gitlab/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion docs/webapp-policy.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 は
Expand Down
8 changes: 7 additions & 1 deletion docs/webapp-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion scripts/check-webapp-policy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,13 +35,18 @@ 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
--manifest)
MANIFEST="$2"
shift 2
;;
--normalize)
NORMALIZE=1
shift
;;
*)
echo "Unknown option: $1" >&2
exit 1
Expand Down Expand Up @@ -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'));
Expand All @@ -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
Expand Down Expand Up @@ -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."
Loading