Skip to content

Commit cdfe765

Browse files
committed
Caddyfile.generate: Make HSTS preloading optional and disabled by default
1 parent def88ec commit cdfe765

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

.github/workflows/expected-output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
+example.org {
1313
+ import common
1414
+ reverse_proxy example-org:80 {
15-
+ header_down +Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
15+
+ header_down +Strict-Transport-Security "max-age=63072000; includeSubDomains"
1616
+ }
1717
+}
1818
+

Caddyfile.generate

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class CaddyfileGenerator:
2020
"alias_domains",
2121
"backend_authority",
2222
"domain",
23+
"hsts_preload",
2324
],
2425
)
2526

@@ -48,15 +49,19 @@ class CaddyfileGenerator:
4849
sites_with_alias_domains: list[self.Site] = [s for s in self._sites if s.alias_domains]
4950

5051
for site in sorted(sites_with_backends, key=attrgetter("domain")):
52+
hsts_header_value = "max-age=63072000; includeSubDomains"
53+
if site.hsts_preload:
54+
hsts_header_value += "; preload"
55+
5156
print(
5257
dedent("""
5358
%s {
5459
import common
5560
reverse_proxy %s {
56-
header_down +Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
61+
header_down +Strict-Transport-Security "%s"
5762
}
58-
}""") # noqa: E501
59-
% (site.domain, site.backend_authority),
63+
}""")
64+
% (site.domain, site.backend_authority, hsts_header_value),
6065
file=fp,
6166
)
6267

@@ -90,7 +95,9 @@ def run(options):
9095
except NoOptionError:
9196
backend_authority = None
9297

93-
site = CaddyfileGenerator.Site(alias_domains, backend_authority, domain)
98+
hsts_preload = config.getboolean(domain, "hsts_preload", fallback=False)
99+
100+
site = CaddyfileGenerator.Site(alias_domains, backend_authority, domain, hsts_preload)
94101
caddyfile.add(site)
95102

96103
with NamedTemporaryFile() as temp_file:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ The `Caddyfile` generated from that very `sites.cfg` would read:
7979
example.org {
8080
import common
8181
reverse_proxy example-org:80 {
82-
header_down +Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
82+
header_down +Strict-Transport-Security "max-age=63072000; includeSubDomains"
8383
}
8484
}
8585

0 commit comments

Comments
 (0)