Skip to content

Commit d9c034d

Browse files
committed
ech-rotate: validate get dns_records before proceeding with rotation
1 parent 43476ee commit d9c034d

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

ech-rotate.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,27 @@ rotate_ech() {
6262
reload_nginx
6363

6464
# 5. Backup DNS records for rollback
65+
log "Backing up current HTTPS DNS records..."
6566
backup_file=$(mktemp)
66-
curl -s -X GET "$CF_ZONE_URL/$CF_ZONE_ID/dns_records?type=HTTPS" \
67+
68+
BACKUP_RESP=$(curl -s --fail-with-body -X GET \
69+
"$CF_ZONE_URL/$CF_ZONE_ID/dns_records?type=HTTPS" \
6770
-H "Authorization: Bearer $CF_API_TOKEN" \
68-
-H "Content-Type: application/json" \
69-
> "$backup_file"
71+
-H "Content-Type: application/json" ) || {
72+
log "Failed to contact Cloudflare for backup"
73+
return 1
74+
}
75+
76+
# Validate JSON and success flag
77+
if ! jq -e '.success == true and (.result | type=="array")' >/dev/null 2>&1 <<<"$BACKUP_RESP"; then
78+
log "Backup failed: invalid or unsuccessful Cloudflare response"
79+
echo "$BACKUP_RESP" | jq -C . >&2 || echo "$BACKUP_RESP" >&2
80+
return 1
81+
fi
82+
83+
# Write only validated data
84+
echo "$BACKUP_RESP" > "$backup_file"
85+
log "Backup saved to $backup_file (entries: $(jq '.result | length' "$backup_file"))"
7086

7187
# 5-6. Update DNS Records
7288
source /usr/local/bin/update-https-records.sh

update-https-records.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ update_https_records() {
2525
# 3. Publish HTTPS DNS record to Cloudflare (update only ech field)
2626
# Common curl options
2727
# use the DNS batch API schema (posts, patches, puts, deletes)
28-
CURL_OPTS=(-s --retry 5 --retry-delay 2 --retry-connrefused)
28+
CURL_OPTS=(-s --fail-with-body --retry 5 --retry-delay 2 --retry-connrefused)
2929

3030
POSTS=()
3131
PATCHES=()
@@ -35,7 +35,17 @@ update_https_records() {
3535
--data-urlencode "type=HTTPS" \
3636
-H "Authorization: Bearer $CF_API_TOKEN" \
3737
-H "Content-Type: application/json" \
38-
"$CF_ZONE_URL/$CF_ZONE_ID/dns_records")
38+
"$CF_ZONE_URL/$CF_ZONE_ID/dns_records") || {
39+
log "Curl request failed (transport or HTTP error) when fetching existing DNS records"
40+
return 1
41+
}
42+
43+
# Validate JSON + success flag
44+
if ! jq -e '.success == true' >/dev/null 2>&1 <<<"$ALL_RECORDS"; then
45+
log "Cloudflare API returned failure or invalid JSON"
46+
echo "$ALL_RECORDS" | jq -C . >&2 || echo "$ALL_RECORDS" >&2
47+
return 1
48+
fi
3949

4050
for d in "${SUBDOMAINS_ARR[@]}"; do
4151
RECORD_RAW=$(jq --arg name "$d" '.result[] | select(.name==$name)' <<<"$ALL_RECORDS")

0 commit comments

Comments
 (0)