Skip to content

Commit 0022759

Browse files
committed
ContainerRegistry: Reject other invalid characters in repository names
1 parent 3668508 commit 0022759

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Sources/ContainerRegistry/ImageReference.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import RegexBuilder
1717
public enum ReferenceError: Error, Equatable {
1818
case emptyString
1919
case containsUppercaseLetters(String)
20+
case invalidReferenceFormat(String)
2021
case unexpected(String)
2122
}
2223

@@ -66,6 +67,12 @@ public struct Repository: Sendable, Equatable, CustomStringConvertible, CustomDe
6667
throw ReferenceError.containsUppercaseLetters(rawValue)
6768
}
6869

70+
// https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-manifests
71+
let regex = /[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(\/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*/
72+
if try regex.wholeMatch(in: rawValue) == nil {
73+
throw ReferenceError.invalidReferenceFormat(rawValue)
74+
}
75+
6976
value = rawValue
7077
}
7178

Tests/ContainerRegistryTests/ImageReferenceTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ struct ReferenceTests {
146146
#expect(throws: ReferenceError.containsUppercaseLetters("helloWorld")) {
147147
try ImageReference(fromString: "localhost:5555/helloWorld")
148148
}
149+
150+
#expect(throws: ReferenceError.invalidReferenceFormat("hello^world")) {
151+
try ImageReference(fromString: "localhost:5555/hello^world")
152+
}
149153
}
150154

151155
@Test

0 commit comments

Comments
 (0)