Skip to content

Commit 8118e7d

Browse files
authored
Add domain & ignore www (#8874)
* add domain & expand www depends on subdomain
1 parent c21d2ad commit 8118e7d

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/NuGetGallery.Services/Configuration/TrustedImageDomains.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public TrustedImageDomains(IEnumerable<string> trustedImageDomainList)
2828
throw new ArgumentNullException(nameof(trustedImageDomainList));
2929
}
3030

31-
TrustedImageDomainList = new HashSet<string>(trustedImageDomainList, StringComparer.OrdinalIgnoreCase);
31+
var trustedImageDomainListFromFile = new HashSet<string>(trustedImageDomainList, StringComparer.OrdinalIgnoreCase);
32+
TrustedImageDomainList = expandDomainList(trustedImageDomainListFromFile);
3233
}
3334

3435
public bool IsImageDomainTrusted(string imageDomain)
@@ -40,5 +41,40 @@ public bool IsImageDomainTrusted(string imageDomain)
4041

4142
return TrustedImageDomainList.Contains(imageDomain);
4243
}
44+
45+
private HashSet<string> expandDomainList(HashSet<string> trustedImageDomainListFromFile)
46+
{
47+
var expandedImageDomainList = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
48+
49+
foreach (var imageDomain in trustedImageDomainListFromFile)
50+
{
51+
expandedImageDomainList.Add(imageDomain);
52+
53+
var subdomain = ParseSubDomain(imageDomain);
54+
55+
if (string.IsNullOrEmpty(subdomain))
56+
{
57+
expandedImageDomainList.Add("www." + imageDomain);
58+
}
59+
else if (subdomain == "www")
60+
{
61+
expandedImageDomainList.Add(imageDomain.Substring(subdomain.Length));
62+
}
63+
}
64+
return expandedImageDomainList;
65+
}
66+
67+
private string ParseSubDomain(string domain)
68+
{
69+
if (domain.Split('.').Length > 2)
70+
{
71+
var lastIndex = domain.LastIndexOf(".");
72+
var index = domain.LastIndexOf('.', lastIndex - 1);
73+
74+
return domain.Substring(0, index);
75+
}
76+
77+
return null;
78+
}
4379
}
4480
}

src/NuGetGallery/App_Data/Files/Content/Trusted-Image-Domains.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"opencollective.com",
3030
"snyk.io",
3131
"sonarcloud.io",
32+
"travis-ci.com",
33+
"travis-ci.org",
3234
"raw.github.com",
3335
"raw.githubusercontent.com",
3436
"user-images.githubusercontent.com",

tests/NuGetGallery.Facts/Services/ImageDomainValidatorFacts.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public void ThrowsArgumentNullExceptionForNullUrl()
2929
[Theory]
3030
[InlineData("https://api.bintray.com/example/image.svg", true, "https://api.bintray.com/example/image.svg", true)]
3131
[InlineData("http://api.bintray.com/example/image.svg", true, "https://api.bintray.com/example/image.svg", true)]
32+
[InlineData("http://www.api.bintray.com/example/image.svg", false, null, false)]
33+
[InlineData("https://www.codefactor.io/repository/github/andy840119/Synthesia.MetaDataParser/badge", true, "https://www.codefactor.io/repository/github/andy840119/Synthesia.MetaDataParser/badge", true)]
34+
[InlineData("https://www.api.codefactor.io/repository/github/andy840119/Synthesia.MetaDataParser/badge", false, null, false)]
3235
[InlineData("https://travis-ci.org/Azure/azure-relay-aspnetserver.svg?branch=dev", false, null, false)]
3336
[InlineData("https://github.com/cedx/where.dart/actions/workflows/build.yaml/badge.svg?branch=develop", false, "https://github.com/cedx/where.dart/actions/workflows/build.yaml/badge.svg?branch=develop", true)]
3437
[InlineData("https://[email protected]/peaceiris/actions-gh-pages/actions/workflows/dev-image.yml/something/badge.svg", false, null, false)]

0 commit comments

Comments
 (0)