Skip to content

Commit c255230

Browse files
authored
Merge pull request #1688 from ungrav/security/pin-hf-model-revisions
Pin executable Hugging Face model downloads to revisions
2 parents c871fde + 1acab76 commit c255230

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

StabilityMatrix.Core/Helper/RemoteModels.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private static RemoteResource ControlNetCommon(string path, string sha256)
199199
[
200200
new()
201201
{
202-
Url = new Uri("https://huggingface.co/Bingsu/adetailer/resolve/main/face_yolov8m.pt"),
202+
Url = new Uri("https://huggingface.co/Bingsu/adetailer/resolve/b0a075fd35454c86bb453a1ca06b29ffee704c20/face_yolov8m.pt"),
203203
HashSha256 = "f02b8a23e6f12bd2c1b1f6714f66f984c728fa41ed749d033e7d6dea511ef70c",
204204
InfoUrl = new Uri("https://huggingface.co/Bingsu/adetailer"),
205205
Author = "Bingsu",
@@ -212,7 +212,7 @@ private static RemoteResource ControlNetCommon(string path, string sha256)
212212
},
213213
new()
214214
{
215-
Url = new Uri("https://huggingface.co/Bingsu/adetailer/resolve/main/hand_yolov8s.pt"),
215+
Url = new Uri("https://huggingface.co/Bingsu/adetailer/resolve/b0a075fd35454c86bb453a1ca06b29ffee704c20/hand_yolov8s.pt"),
216216
HashSha256 = "5c4faf8d17286ace2c3d3346c6d0d4a0c8d62404955263a7ae95c1dd7eb877af",
217217
InfoUrl = new Uri("https://huggingface.co/Bingsu/adetailer"),
218218
Author = "Bingsu",
@@ -225,7 +225,7 @@ private static RemoteResource ControlNetCommon(string path, string sha256)
225225
},
226226
new()
227227
{
228-
Url = new Uri("https://huggingface.co/Bingsu/adetailer/resolve/main/person_yolov8m-seg.pt"),
228+
Url = new Uri("https://huggingface.co/Bingsu/adetailer/resolve/b0a075fd35454c86bb453a1ca06b29ffee704c20/person_yolov8m-seg.pt"),
229229
HashSha256 = "9d881ec50b831f546e37977081b18f4e3bf65664aec163f97a311b0955499795",
230230
InfoUrl = new Uri("https://huggingface.co/Bingsu/adetailer"),
231231
Author = "Bingsu",
@@ -238,7 +238,7 @@ private static RemoteResource ControlNetCommon(string path, string sha256)
238238
},
239239
new()
240240
{
241-
Url = new Uri("https://huggingface.co/Bingsu/adetailer/resolve/main/person_yolov8s-seg.pt"),
241+
Url = new Uri("https://huggingface.co/Bingsu/adetailer/resolve/b0a075fd35454c86bb453a1ca06b29ffee704c20/person_yolov8s-seg.pt"),
242242
HashSha256 = "b5684835e79fd8b805459e0f7a0f9daa437e421cb4a214fff45ec4ac61767ef9",
243243
InfoUrl = new Uri("https://huggingface.co/Bingsu/adetailer"),
244244
Author = "Bingsu",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Reflection;
2+
using StabilityMatrix.Core.Helper;
3+
using StabilityMatrix.Core.Models;
4+
5+
namespace StabilityMatrix.Tests.Core;
6+
7+
[TestClass]
8+
public class RemoteModelsTests
9+
{
10+
[TestMethod]
11+
public void ExecutableHuggingFaceModelsUsePinnedRevisions()
12+
{
13+
var executableExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
14+
{
15+
".bin",
16+
".ckpt",
17+
".pt",
18+
".pth",
19+
};
20+
21+
var resources = typeof(RemoteModels)
22+
.GetProperties(BindingFlags.Public | BindingFlags.Static)
23+
.Select(property => property.GetValue(null))
24+
.SelectMany(
25+
value =>
26+
value switch
27+
{
28+
IEnumerable<RemoteResource> remoteResources => remoteResources,
29+
IEnumerable<HybridModelFile> hybridModels => hybridModels
30+
.Where(model => model.DownloadableResource.HasValue)
31+
.Select(model => model.DownloadableResource!.Value),
32+
HybridModelFile { DownloadableResource: { } resource } => [resource],
33+
_ => [],
34+
}
35+
);
36+
37+
var unpinnedModels = resources
38+
.Where(resource => resource.Url.Host == "huggingface.co")
39+
.Where(resource => executableExtensions.Contains(Path.GetExtension(resource.Url.AbsolutePath)))
40+
.Where(resource => resource.Url.AbsolutePath.Contains("/resolve/main/", StringComparison.Ordinal))
41+
.Select(resource => resource.Url)
42+
.ToArray();
43+
44+
Assert.AreEqual(
45+
0,
46+
unpinnedModels.Length,
47+
$"Executable Hugging Face model URLs must use pinned revisions: {string.Join(", ", unpinnedModels.Select(url => url.AbsoluteUri))}"
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)