Skip to content

Commit c31ffc4

Browse files
devantlerclaude
andauthored
feat(zone): stop Cloudflare rewriting pages and overriding robots.txt (#81)
* feat(zone): stop Cloudflare rewriting proxied pages Since devantler.tech is proxied, Cloudflare changes 76 of its HTML pages: it obfuscates mailto links behind a JavaScript decoder, injects the Bot Fight Mode JavaScript Detections script, and adds AI Labyrinth honeypot links. Declare those settings off in zone.tf so visitors get exactly what the origin serves. On the Free plan JavaScript Detections can only be turned off with Bot Fight Mode; "Block AI bots" is left as it is. The DNS workflow now also imports the zone's single-instance settings (bot management by zone, the listed zone settings by zone and name) and admits only imports and updates of them, never creates or forgets. Part of #21. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(zone): let each site's robots.txt decide on AI crawlers "Block AI bots" answered GPTBot with 403 on devantler.tech although the site's robots.txt allows every crawler, keeping the business site out of AI search. Set ai_bots_protection to disabled in the same apply; every other bot setting keeps its live value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(zone): rewrap the zone.tf header comment Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(zone): keep the bot settings the provider would otherwise reset Provider 5.28.1 gives ai_bots_migration_opt_out a static false default and sends it on update, and bot_preference_sync_enabled is optional without a computed value. Ignore both, so the apply changes only the declared bot settings and leaves these at their live values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f66684a commit c31ffc4

7 files changed

Lines changed: 96 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ workflow defaults to checking independent encrypted recovery publication. Its
8787
apply mode accepts only the frozen, reviewed first proposal and requires a
8888
successful custody check from that same main revision.
8989

90-
DNS records are declared in [`dns.tf`](dns.tf) and change only through the
90+
DNS records are declared in [`dns.tf`](dns.tf), and the zone settings that would
91+
rewrite proxied pages in [`zone.tf`](zone.tf). Both change only through the
9192
manual **DNS plan and guarded apply** workflow: plan first, then apply the same
9293
reviewed revision. It imports declared records that already exist, never
9394
deletes or replaces anything, and keeps an encrypted state copy from before and

‎docs/runbooks/bootstrap-and-recovery.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ match, a record already managed here, or a record that external-dns owns
364364
refuses the run.
365365

366366
The saved plan must then pass `scripts/assert-no-destructive-plan.sh` and
367-
`scripts/check-dns-plan.jq`. Only DNS records may change. Imports must leave
367+
`scripts/check-dns-plan.jq`. Only DNS records and the zone settings in `zone.tf`
368+
may change. Those settings, the bot management configuration and the listed
369+
zone settings, always exist, so they are imported by zone, never created or
370+
forgotten, and may be updated by the change that imports them. Imports must leave
368371
their record exactly as it is. Creates, updates and `removed` blocks with
369372
`destroy = false` are admitted; deletes and replacements are not. A created or
370373
updated record must have a known name and must not be owned by external-dns.

‎scripts/apply-dns.sh‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,10 @@ jq -r '.[]|"import {\n to = \(.address)\n id = \(.id|tojson)\n}\n"' "$scratch/
180180
stage=plan
181181
plan_json change || stop
182182
# Public summary: actions and changed attribute names, never values. Only the
183-
# addresses written in dns.tf are printed; resources keyed by the private
183+
# addresses written in dns.tf and zone.tf are printed; resources keyed by the private
184184
# configuration (tokens, buckets, configured records) appear only as a count.
185-
jq -r 'def public: .address|startswith("cloudflare_dns_record.") and (startswith("cloudflare_dns_record.records[")|not);
185+
jq -r 'def public: .address | (startswith("cloudflare_dns_record.") and (startswith("cloudflare_dns_record.records[")|not)) or
186+
startswith("cloudflare_zone_setting.") or startswith("cloudflare_bot_management.");
186187
(.resource_changes[] | select(public) |
187188
(if .change.importing then "import" else (.change.actions|join("-")) end) as $action |
188189
select($action!="no-op") |

‎scripts/check-dns-plan.jq‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
# names. Otherwise it could hand a mail record, or any other record, to
1212
# external-dns, which would delete it. The typeless apex registry name is always refused,
1313
# because it claims every apex record, the Email MX and TXT records included.
14+
#
15+
# Besides DNS records, only the zone settings declared in zone.tf may change:
16+
# the bot management configuration and the zone settings listed below. They
17+
# always exist, so they are imported, never created, and may be updated by the
18+
# reviewed change that imports them.
19+
def zone_settings: ["email_obfuscation"];
20+
def setting: .type=="cloudflare_bot_management" or
21+
(.type=="cloudflare_zone_setting" and (((.change.after // .change.before // {}).setting_id // "")|IN(zone_settings[])));
1422
def fqdn: ascii_downcase | rtrimstr(".") |
1523
if .=="@" then "devantler.tech"
1624
elif .=="devantler.tech" or endswith(".devantler.tech") then .
@@ -51,9 +59,11 @@ all(.resource_changes[];
5159
.mode=="managed" and
5260
(.change.actions|IN(["no-op"],["create"],["update"],["forget"])) and
5361
(if .change.importing then
54-
any($wanted[]; .address==$resource.address) and .change.actions==["no-op"] and (changed|not)
62+
any($wanted[]; .address==$resource.address) and
63+
(if setting then .change.actions|IN(["no-op"],["update"]) else .change.actions==["no-op"] and (changed|not) end)
5564
else true end) and
56-
(if .type!="cloudflare_dns_record" then .change.actions==["no-op"]
65+
(if setting then .change.actions|IN(["no-op"],["update"])
66+
elif .type!="cloudflare_dns_record" then .change.actions==["no-op"]
5767
elif writes then
5868
.change.after as $after |
5969
($after.name|type=="string") and ($after.type|type=="string") and

‎scripts/dns-imports.jq‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
# no external-dns ownership record claims it. Anything else is refused, so a
66
# typo or drift can never add a second mail record, adopt a record twice, or
77
# take over an external-dns record.
8+
#
9+
# The zone's single-instance settings in zone.tf always exist, so a planned
10+
# create of one of them is always an import of the live setting: the bot
11+
# management configuration by zone, and an allowed zone setting by zone and
12+
# setting name. Any other setting is left to fail at the plan check.
13+
def zone_settings: ["email_obfuscation"];
814
def fqdn: ascii_downcase | rtrimstr(".") |
915
if .=="@" then "devantler.tech"
1016
elif .=="devantler.tech" or endswith(".devantler.tech") then .
@@ -33,7 +39,15 @@ $snapshot[0] as $live |
3339
elif ($exact|length)>1 then {address, refused:"ambiguous"}
3440
elif ($same|length)>0 then {address, refused:"content-differs"}
3541
else empty end
36-
] as $resolved |
42+
] as $records |
43+
[.resource_changes[] |
44+
select(.mode=="managed" and .change.actions==["create"]) |
45+
if .type=="cloudflare_bot_management" then {address, id:$live.zone.id}
46+
elif .type=="cloudflare_zone_setting" and (.change.after.setting_id|IN(zone_settings[])) then
47+
{address, id:($live.zone.id+"/"+.change.after.setting_id)}
48+
else empty end
49+
] as $settings |
50+
($records + $settings) as $resolved |
3751
([$resolved[]|select(has("id"))|.id]) as $ids |
3852
{
3953
imports:[$resolved[]|select(has("id"))]|sort_by(.address),

‎scripts/tests/test-dns-apply.sh‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,31 @@ printf '%s\n' "$noop_import" | jq -s '{errored:true,resource_changes:.}' | if ch
173173
printf '%s\n' "$noop_import" | jq -s '{deferred_changes:[{}],resource_changes:.}' | if check; then exit 1; fi
174174
printf '%s\n' 'PASS: DNS plans admit unchanged imports, creates, updates and forgets only'
175175

176+
# 3b. The zone's single-instance settings are always imported, may then be updated,
177+
# and are never created, forgotten or widened to other zone settings.
178+
setting() { jq -n --arg address "$1" --arg type "$2" --arg setting "$3" '{address:$address,mode:"managed",type:$type,change:{actions:["create"],after:({zone_id:null} + (if $setting=="" then {} else {setting_id:$setting,value:"off"} end))}}'; }
179+
result=$( { setting cloudflare_bot_management.primary cloudflare_bot_management ""
180+
setting cloudflare_zone_setting.email_obfuscation cloudflare_zone_setting email_obfuscation
181+
setting cloudflare_zone_setting.rocket_loader cloudflare_zone_setting rocket_loader; } | resolve)
182+
jq -e --arg zone "$zone" '.refused==[] and .imports==[
183+
{address:"cloudflare_bot_management.primary",id:$zone},
184+
{address:"cloudflare_zone_setting.email_obfuscation",id:($zone+"/email_obfuscation")}]' <<<"$result" >/dev/null
185+
printf '%s\n' "[{\"address\":\"cloudflare_bot_management.primary\",\"id\":\"$zone\"},{\"address\":\"cloudflare_zone_setting.email_obfuscation\",\"id\":\"$zone/email_obfuscation\"}]" > "$scratch/imports.json"
186+
bot_import=$(change cloudflare_bot_management.primary cloudflare_bot_management '["update"]' '{"fight_mode":true,"enable_js":true}' '{"fight_mode":false,"enable_js":false}' "{\"id\":\"$zone\"}")
187+
obfuscation_import=$(change cloudflare_zone_setting.email_obfuscation cloudflare_zone_setting '["update"]' '{"setting_id":"email_obfuscation","value":"on"}' '{"setting_id":"email_obfuscation","value":"off"}' "{\"id\":\"$zone/email_obfuscation\"}")
188+
printf '%s\n' "$bot_import" "$obfuscation_import" | plan | check
189+
# The import list names only the other resource, so only the action rule can refuse these.
190+
printf '%s\n' "[{\"address\":\"cloudflare_zone_setting.email_obfuscation\",\"id\":\"$zone/email_obfuscation\"}]" > "$scratch/imports.json"
191+
{ printf '%s\n' "$obfuscation_import"; change cloudflare_bot_management.primary cloudflare_bot_management '["create"]' null '{"fight_mode":false}' null; } | reject "created bot management"
192+
printf '%s\n' "[{\"address\":\"cloudflare_bot_management.primary\",\"id\":\"$zone\"}]" > "$scratch/imports.json"
193+
{ printf '%s\n' "$bot_import"; change cloudflare_zone_setting.email_obfuscation cloudflare_zone_setting '["forget"]' '{"setting_id":"email_obfuscation","value":"on"}' null null; } | reject "forgotten zone setting"
194+
printf '%s\n' "[{\"address\":\"cloudflare_bot_management.primary\",\"id\":\"$zone\"},{\"address\":\"cloudflare_zone_setting.email_obfuscation\",\"id\":\"$zone/email_obfuscation\"}]" > "$scratch/imports.json"
195+
{ printf '%s\n' "$bot_import" "$obfuscation_import"
196+
change cloudflare_zone_setting.rocket_loader cloudflare_zone_setting '["update"]' '{"setting_id":"rocket_loader","value":"off"}' '{"setting_id":"rocket_loader","value":"on"}' null; } | reject "update of an unlisted zone setting"
197+
printf '%s\n' "$bot_import" | reject "missing zone-setting import"
198+
printf '%s\n' "[{\"address\":\"cloudflare_dns_record.mail_mx_01\",\"id\":\"$import_id\"}]" > "$scratch/imports.json"
199+
printf '%s\n' 'PASS: zone settings are imported, may be updated, and never created, forgotten or widened'
200+
176201
# 4. The declared mail records are the ones the company Email depends on.
177202
grep -q 'content = "mx01.mail.icloud.com"' "$root/dns.tf"
178203
grep -q 'content = "mx02.mail.icloud.com"' "$root/dns.tf"

‎zone.tf‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Zone features that rewrite the HTML of proxied pages, or override a site's own
2+
# crawler policy, stay off, so visitors get exactly the bytes the origin serves.
3+
# The business site forbids injected scripts, and a strict
4+
# Content-Security-Policy would block them anyway.
5+
#
6+
# - Email Address Obfuscation replaces mailto links with a JavaScript decoder,
7+
# which hides the published contact address from visitors without JS.
8+
# - Bot Fight Mode injects the JavaScript Detections script on every page; on
9+
# the Free plan it cannot be turned off separately. It can also challenge the
10+
# platform's API and sign-in traffic, which sign-in protects instead.
11+
# - AI Labyrinth (`crawler_protection`) adds hidden honeypot links to pages.
12+
# - "Block AI bots" (`ai_bots_protection`) answered AI crawlers with 403 although
13+
# the site's robots.txt allows every crawler, keeping the business site out of
14+
# AI search. Each site states its crawler policy in its own robots.txt.
15+
#
16+
# Every other bot setting keeps its live value. Most unset attributes are
17+
# computed; the two the provider would otherwise reset (a static default and an
18+
# optional, non-computed flag) are ignored so this resource never changes them.
19+
resource "cloudflare_zone_setting" "email_obfuscation" {
20+
zone_id = cloudflare_zone.primary.id
21+
setting_id = "email_obfuscation"
22+
value = "off"
23+
}
24+
25+
resource "cloudflare_bot_management" "primary" {
26+
zone_id = cloudflare_zone.primary.id
27+
fight_mode = false
28+
enable_js = false
29+
crawler_protection = "disabled"
30+
ai_bots_protection = "disabled"
31+
32+
lifecycle {
33+
ignore_changes = [ai_bots_migration_opt_out, bot_preference_sync_enabled]
34+
}
35+
}

0 commit comments

Comments
 (0)