fetch-secret.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env bash
  2. # Retrieves the secret for an existing Turnstile widget via the Cloudflare API.
  3. # Used by the recovery flow when binding the secret to a freshly deployed Worker.
  4. #
  5. # Reads:
  6. # $CLOUDFLARE_API_TOKEN (required)
  7. #
  8. # Args:
  9. # --account-id <id> Cloudflare account ID
  10. # --sitekey <key> Widget sitekey to look up
  11. #
  12. # Outputs JSON. Exit 0 on success, 1 on failure.
  13. # ok: {"status":"ok","secret":"<secret>","clearance_level":"<level>","domains":[<list>]}
  14. # no_scope: {"status":"missing_read_scope","detail":"token lacks Account.Turnstile:Read"}
  15. # not_found: {"status":"error","reason":"widget_not_found","http_code":<code>}
  16. #
  17. # The agent uses clearance_level to enforce the pre-clearance scope boundary
  18. # (Spin only applies to widgets where clearance_level == "no_clearance"; for
  19. # other levels siteverify is optional and the recovery flow should exit).
  20. #
  21. # Never propose recreating the widget to get a fresh secret; that breaks
  22. # the existing sitekey everywhere the user has it deployed in their frontend.
  23. set -uo pipefail
  24. while [[ $# -gt 0 ]]; do
  25. case $1 in
  26. --account-id) ACCOUNT_ID="$2"; shift 2 ;;
  27. --sitekey) SITEKEY="$2"; shift 2 ;;
  28. *) echo "fetch-secret: unknown arg $1" >&2; exit 2 ;;
  29. esac
  30. done
  31. : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN must be set}"
  32. : "${ACCOUNT_ID:?--account-id required}"
  33. : "${SITEKEY:?--sitekey required}"
  34. tmp=$(mktemp)
  35. http_code=$(curl -sS -w "%{http_code}" -o "$tmp" \
  36. "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/challenges/widgets/$SITEKEY" \
  37. -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" 2>/dev/null || echo "000")
  38. body=$(cat "$tmp"); rm -f "$tmp"
  39. if [ "$http_code" = "200" ]; then
  40. secret=$(echo "$body" | (jq -r '.result.secret' 2>/dev/null || python3 -c "import sys,json; print(json.load(sys.stdin)['result']['secret'])"))
  41. clearance=$(echo "$body" | (jq -r '.result.clearance_level // "no_clearance"' 2>/dev/null || python3 -c "import sys,json; print(json.load(sys.stdin)['result'].get('clearance_level','no_clearance'))"))
  42. domains=$(echo "$body" | (jq -c '.result.domains // []' 2>/dev/null || python3 -c "import sys,json; print(json.dumps(json.load(sys.stdin)['result'].get('domains',[])))"))
  43. if [ -n "$secret" ] && [ "$secret" != "null" ]; then
  44. echo "{\"status\":\"ok\",\"secret\":\"$secret\",\"clearance_level\":\"$clearance\",\"domains\":$domains}"
  45. exit 0
  46. fi
  47. fi
  48. if [ "$http_code" = "403" ]; then
  49. code=$(echo "$body" | (jq -r '.errors[0].code // 0' 2>/dev/null || echo "0"))
  50. if [ "$code" = "10000" ]; then
  51. echo "fetch-secret: token can edit Turnstile widgets but cannot read this one's secret." >&2
  52. echo "fetch-secret: add Account.Turnstile:Read to the token, or fall back to user paste." >&2
  53. echo "{\"status\":\"missing_read_scope\",\"detail\":\"token lacks Account.Turnstile:Read\"}"
  54. exit 1
  55. fi
  56. fi
  57. echo "fetch-secret: widget lookup failed (HTTP $http_code)." >&2
  58. echo "{\"status\":\"error\",\"reason\":\"widget_not_found\",\"http_code\":$http_code}"
  59. exit 1