Skip to content

Commit

Permalink
Use define to switch between short and full hsts generation
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Dec 26, 2024
1 parent d4f060e commit 31fba93
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ partial class HstsDomainPolicyCollection
{
partial void LoadPreloadDomains(TimeProvider timeProvider)
{
// HSTS preload data source: https://raw.githubusercontent.com/chromium/chromium/ba720cd91299fe45b6345be1971ee628af9bc3f5/net/http/transport_security_state_static.json
// HSTS preload data source: https://raw.githubusercontent.com/chromium/chromium/ba720cd91299fe45b6345be1971ee628af9bc3f5/net/http/transport_security_state_static.json
// Commit date: 2024-12-18T23:38:47.0000000Z
var expires126Days = timeProvider.GetUtcNow().Add(TimeSpan.FromDays(126));
var expires365Days = timeProvider.GetUtcNow().Add(TimeSpan.FromDays(365));
Expand All @@ -23,6 +23,7 @@ partial void LoadPreloadDomains(TimeProvider timeProvider)
_policies.Add(dict4);
var dict5 = new ConcurrentDictionary<string, HstsDomainPolicy>(concurrencyLevel: -1, capacity: 11, comparer: StringComparer.OrdinalIgnoreCase);
_policies.Add(dict5);
#if HSTS_PRELOAD_FULL
// Segment size: 1
_ = dict1.TryAdd("amazon", new HstsDomainPolicy("amazon", expires365Days, true));
_ = dict1.TryAdd("android", new HstsDomainPolicy("android", expires365Days, true));
Expand Down Expand Up @@ -161047,5 +161048,53 @@ partial void LoadPreloadDomains(TimeProvider timeProvider)
_ = dict4.TryAdd("zhyz.nl.eu.org", new HstsDomainPolicy("zhyz.nl.eu.org", expires365Days, true));
// Segment size: 5
_ = dict5.TryAdd("wnc-frontend-alb-1765173526.ap-northeast-2.elb.amazonaws.com", new HstsDomainPolicy("wnc-frontend-alb-1765173526.ap-northeast-2.elb.amazonaws.com", expires365Days, true));
#else
// Segment size: 1
_ = dict1.TryAdd("amazon", new HstsDomainPolicy("amazon", expires365Days, true));
_ = dict1.TryAdd("android", new HstsDomainPolicy("android", expires365Days, true));
_ = dict1.TryAdd("app", new HstsDomainPolicy("app", expires365Days, true));
_ = dict1.TryAdd("audible", new HstsDomainPolicy("audible", expires365Days, true));
_ = dict1.TryAdd("azure", new HstsDomainPolicy("azure", expires365Days, true));
_ = dict1.TryAdd("bank", new HstsDomainPolicy("bank", expires365Days, true));
_ = dict1.TryAdd("bing", new HstsDomainPolicy("bing", expires365Days, true));
_ = dict1.TryAdd("boo", new HstsDomainPolicy("boo", expires365Days, true));
_ = dict1.TryAdd("channel", new HstsDomainPolicy("channel", expires365Days, true));
_ = dict1.TryAdd("chrome", new HstsDomainPolicy("chrome", expires365Days, true));
// Segment size: 2
_ = dict2.TryAdd("0--1.de", new HstsDomainPolicy("0--1.de", expires365Days, true));
_ = dict2.TryAdd("0-0.io", new HstsDomainPolicy("0-0.io", expires365Days, true));
_ = dict2.TryAdd("0-0.lt", new HstsDomainPolicy("0-0.lt", expires365Days, true));
_ = dict2.TryAdd("0-24.com", new HstsDomainPolicy("0-24.com", expires365Days, true));
_ = dict2.TryAdd("0-24.net", new HstsDomainPolicy("0-24.net", expires365Days, true));
_ = dict2.TryAdd("0-9.com", new HstsDomainPolicy("0-9.com", expires365Days, true));
_ = dict2.TryAdd("0.sb", new HstsDomainPolicy("0.sb", expires365Days, true));
_ = dict2.TryAdd("00.eco", new HstsDomainPolicy("00.eco", expires365Days, true));
_ = dict2.TryAdd("00010110.nl", new HstsDomainPolicy("00010110.nl", expires365Days, true));
_ = dict2.TryAdd("0008.life", new HstsDomainPolicy("0008.life", expires365Days, true));
// Segment size: 3
_ = dict3.TryAdd("0.com.ms", new HstsDomainPolicy("0.com.ms", expires365Days, true));
_ = dict3.TryAdd("0ii0.eu.org", new HstsDomainPolicy("0ii0.eu.org", expires365Days, true));
_ = dict3.TryAdd("1-2-3bounce.co.uk", new HstsDomainPolicy("1-2-3bounce.co.uk", expires365Days, true));
_ = dict3.TryAdd("100plus.com.my", new HstsDomainPolicy("100plus.com.my", expires365Days, true));
_ = dict3.TryAdd("100plus.com.sg", new HstsDomainPolicy("100plus.com.sg", expires365Days, true));
_ = dict3.TryAdd("101warehousing.com.au", new HstsDomainPolicy("101warehousing.com.au", expires365Days, true));
_ = dict3.TryAdd("106.hi.cn", new HstsDomainPolicy("106.hi.cn", expires365Days, true));
_ = dict3.TryAdd("11tv.dp.ua", new HstsDomainPolicy("11tv.dp.ua", expires365Days, true));
_ = dict3.TryAdd("123host.com.au", new HstsDomainPolicy("123host.com.au", expires365Days, true));
_ = dict3.TryAdd("123noticias.com.br", new HstsDomainPolicy("123noticias.com.br", expires365Days, true));
// Segment size: 4
_ = dict4.TryAdd("1.0.0.1", new HstsDomainPolicy("1.0.0.1", expires365Days, false));
_ = dict4.TryAdd("1022996493.rsc.cdn77.org", new HstsDomainPolicy("1022996493.rsc.cdn77.org", expires126Days, true));
_ = dict4.TryAdd("1464424382.rsc.cdn77.org", new HstsDomainPolicy("1464424382.rsc.cdn77.org", expires126Days, true));
_ = dict4.TryAdd("1844329061.rsc.cdn77.org", new HstsDomainPolicy("1844329061.rsc.cdn77.org", expires126Days, true));
_ = dict4.TryAdd("1972969867.rsc.cdn77.org", new HstsDomainPolicy("1972969867.rsc.cdn77.org", expires126Days, true));
_ = dict4.TryAdd("agriculture.vic.gov.au", new HstsDomainPolicy("agriculture.vic.gov.au", expires365Days, true));
_ = dict4.TryAdd("alanburr.us.eu.org", new HstsDomainPolicy("alanburr.us.eu.org", expires365Days, true));
_ = dict4.TryAdd("allamakee.k12.ia.us", new HstsDomainPolicy("allamakee.k12.ia.us", expires365Days, true));
_ = dict4.TryAdd("api.mega.co.nz", new HstsDomainPolicy("api.mega.co.nz", expires365Days, true));
_ = dict4.TryAdd("armadale.wa.gov.au", new HstsDomainPolicy("armadale.wa.gov.au", expires365Days, true));
// Segment size: 5
_ = dict5.TryAdd("wnc-frontend-alb-1765173526.ap-northeast-2.elb.amazonaws.com", new HstsDomainPolicy("wnc-frontend-alb-1765173526.ap-northeast-2.elb.amazonaws.com", expires365Days, true));
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Version>1.0.0</Version>
<IsTrimmable>true</IsTrimmable>
<Description>Provide an HttpClientHandler to upgrade request from http to https if a policy is set</Description>
<DefineConstants Condition="'$(IsOfficialBuild)' == 'true'">$(DefineConstants);HSTS_PRELOAD_FULL</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
34 changes: 20 additions & 14 deletions tools/Meziantou.Framework.Http.Hsts.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
using Meziantou.Framework;
using Meziantou.Framework.Versioning;

// Enable generating a subset of the data. Otherwise the IDE is not responsive because of the large file.
var fullGeneration = true;

var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN");
if (token is null)
{
Expand Down Expand Up @@ -83,22 +80,31 @@
sb.Append($" _policies.Add(dict{i.ToString(CultureInfo.InvariantCulture)});\n");
}

foreach (var entryGroup in entries.GroupBy(e => e.SegmentCount).OrderBy(group => group.Key))
void AddPreloadData(bool fullGeneration)
{
sb.Append(CultureInfo.InvariantCulture, $" // Segment size: {entryGroup.Key}\n");
foreach (var entry in entryGroup.OrderBy(entry => entry.Name, StringComparer.Ordinal).Take(fullGeneration ? int.MaxValue : 10))
foreach (var entryGroup in entries.GroupBy(e => e.SegmentCount).OrderBy(group => group.Key))
{
var expiresIn = entry.Policy switch
sb.Append(CultureInfo.InvariantCulture, $" // Segment size: {entryGroup.Key}\n");
foreach (var entry in entryGroup.OrderBy(entry => entry.Name, StringComparer.Ordinal).Take(fullGeneration ? int.MaxValue : 10))
{
"bulk-18-weeks" => "expires126Days",
"bulk-1-year" => "expires365Days",
_ => "expires365Days",
};

sb.Append($""" _ = dict{entry.SegmentCount.ToString(CultureInfo.InvariantCulture)}.TryAdd("{entry.Name}", new HstsDomainPolicy("{entry.Name}", {expiresIn}, {(entry.IncludeSubdomains ? "true" : "false")}));""" + "\n");
var expiresIn = entry.Policy switch
{
"bulk-18-weeks" => "expires126Days",
"bulk-1-year" => "expires365Days",
_ => "expires365Days",
};

sb.Append($""" _ = dict{entry.SegmentCount.ToString(CultureInfo.InvariantCulture)}.TryAdd("{entry.Name}", new HstsDomainPolicy("{entry.Name}", {expiresIn}, {(entry.IncludeSubdomains ? "true" : "false")}));""" + "\n");
}
}
}

sb.Append("#if HSTS_PRELOAD_FULL\n");
AddPreloadData(fullGeneration: true);
sb.Append("#else\n");
AddPreloadData(fullGeneration: false);
sb.Append("#endif\n");

var result = $$"""
// <auto-generated />
#nullable disable
Expand All @@ -111,7 +117,7 @@ partial class HstsDomainPolicyCollection
{
partial void LoadPreloadDomains(TimeProvider timeProvider)
{
// HSTS preload data source: {{fileUrl}}
// HSTS preload data source: {{fileUrl}}
// Commit date: {{commitDate.ToString("O", CultureInfo.InvariantCulture)}}
{{sb.ToString().TrimEnd('\n')}}
}
Expand Down

0 comments on commit 31fba93

Please sign in to comment.