Fix line break handling in Cloudflare template

Fix line break handling in Cloudflare template

Cloudflare’s IP list has gone back and forth between including a trailing line break and omitting it.

When a trailing line break was first added in 2015, it resulted in a bug: Issue with Cloudflare template - bug - Discourse Meta

The trailing line break was removed again in 2021: Cloudflare template broken again - bug - Discourse Meta

This fixes the template so that it will work regardless of extra line breaks. It will also safely ignore any empty lines that may appear in the files.

diff --git a/templates/cloudflare.template.yml b/templates/cloudflare.template.yml
index 45f29e2..2f5a089 100644
--- a/templates/cloudflare.template.yml
+++ b/templates/cloudflare.template.yml
@@ -6,9 +6,10 @@ run:
         #!/bin/bash -e
         # Download list of CloudFlare ips
         wget https://www.cloudflare.com/ips-v4/ -O - > /tmp/cloudflare-ips
+        echo >> /tmp/cloudflare-ips
         wget https://www.cloudflare.com/ips-v6/ -O - >> /tmp/cloudflare-ips
         # Make into nginx commands and escape for inclusion into sed append command
-        CONTENTS=$(</tmp/cloudflare-ips sed 's/^/set_real_ip_from /' | sed 's/$/;/' | tr '\n' '\\' | sed 's/\\/\\n/g')
+        CONTENTS=$(</tmp/cloudflare-ips sed '/^$/d; s/^.*/set_real_ip_from &;/' | tr '\n' '\\' | sed 's/\\/\\n/g')
         
         echo CloudFlare IPs:
         echo $(echo | sed "/^/a $CONTENTS")

GitHub sha: ae5a223074388795e4ea073a162cbf1dc174d392

This commit appears in #558 which was approved by Falco. It was merged by Falco.