<commit_before><commit_msg>Add new post<commit_after>+++
categories = ["webdev"]
date = 2020-05-08T21:42:38+05:30
draft = true
slug = "how-cloudflare-proxies-cname-records"
tags = ["#100DaysToOffload", "cloudflare"]
title = "How Cloudflare proxies CNAME records"
description = "Everybody probably understands how Cloudflare proxies A/AAAA records, but how it proxies CNAME records is also pretty interesting. Let's dive into how that happens and why it can often break other products that need you to set CNAME records."
+++

As people here might realize, I recently started using [Purelymail](https://purelymail.com/) for my email needs (the how and why of it can be found [here](/posts/switching-my-email-to-purelymail/)). I also mentioned there, that Cloudflare's proxy-by-default nature caused Purelymail to not detect my CNAME settings and disabling the proxy did the job. I contacted Purelymail's Scott about this and he eventually pushed a fix out that \*should\* have fixed it, but since he did not have a Cloudflare account, he couldn't verify this exact case.

Well, the fix didn't work.

This made me wonder, _why?_ I trust that Scott is more aware of what he's doing than I am so the fix must have been legitimate and that something is special about Cloudflare's handling of this. So I did some testing! (yes I still use [dnscontrol](https://stackexchange.github.io/dnscontrol/))

```diff
diff --git a/dnsconfig.js b/dnsconfig.js
index f5fd1836f8ec..a53bab70de84 100644
--- a/dnsconfig.js
+++ b/dnsconfig.js
@@ -24,6 +24,8 @@ var DEV_RECORDS = [
     CNAME('purelymail2._domainkey', 'key2._dkimroot.purelymail.com.', CF_PROXY_OFF),
     CNAME('purelymail3._domainkey', 'key3._dkimroot.purelymail.com.', CF_PROXY_OFF),
     CNAME('status', 'stats.uptimerobot.com.', CF_PROXY_OFF),
+    CNAME('test_domain_no_proxy', 'msfjarvis.github.io.', CF_PROXY_OFF),
+    CNAME('test_domain_with_proxy', 'msfjarvis.github.io.'),
     MX('@', 50, 'mailserver.purelymail.com.'),
     TXT('@', 'v=spf1 include:_spf.purelymail.com ~all'),
     TXT('@', 'purelymail_ownership_proof=0xd34db33f'),
```

Running [dig](https://linux.die.net/man/1/dig) on both the subdomains, I spotted something interesting.

```bash
$ dig @1.1.1.1 test_domain_no_proxy.msfjarvis.dev
...
;; ANSWER SECTION:
test_domain_no_proxy.msfjarvis.dev. 289	IN CNAME msfjarvis.github.io.
msfjarvis.github.io.	3589	IN	A	172.16.17.32
msfjarvis.github.io.	3589	IN	A	172.16.17.32
msfjarvis.github.io.	3589	IN	A	192.168.127.12
msfjarvis.github.io.	3589	IN	A	192.168.127.12
...
```

```bash
$ dig @1.1.1.1 test_domain_with_proxy.msfjarvis.dev
...
;; ANSWER SECTION:
test_domain_with_proxy.msfjarvis.dev. 300 IN A	192.168.127.12
test_domain_with_proxy.msfjarvis.dev. 300 IN A	172.16.31.10
...
```

The proxied CNAME record isn't actually a CNAME after all! Cloudflare creates an A record for it and handles the redirection internally. This makes the CNAME aspect of the record opaque to DNS lookups which in turn trips software like Purelymail's backend. I've reported my findings to Scott and am awaiting his response.

And that's it! Nothing too fancy, just something I found kinda weird.
{'fraction_non_alphanumeric': 0.11411315956770503, 'fraction_numerical': 0.04799745708836618, 'mean_word_length': 4.492146596858639, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7585083', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1069, 'n_words': 350}