Skip to content

Commit c122da1

Browse files
committed
ContainerRegistry: Reject other invalid characters in repository names
1 parent 1e0f277 commit c122da1

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
@@ -116,6 +116,7 @@ extension ImageReference {
116116
public enum ValidationError: Error, Equatable {
117117
case emptyString
118118
case containsUppercaseLetters(String)
119+
case invalidReferenceFormat(String)
119120
}
120121

121122
public init(_ rawValue: String) throws {
@@ -129,6 +130,12 @@ extension ImageReference {
129130
throw ValidationError.containsUppercaseLetters(rawValue)
130131
}
131132

133+
// https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-manifests
134+
let regex = /[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(\/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*/
135+
if try regex.wholeMatch(in: rawValue) == nil {
136+
throw ValidationError.invalidReferenceFormat(rawValue)
137+
}
138+
132139
value = rawValue
133140
}
134141

Tests/ContainerRegistryTests/ImageReferenceTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ struct ReferenceTests {
188188
#expect(throws: ImageReference.Repository.ValidationError.containsUppercaseLetters("helloWorld")) {
189189
try ImageReference(fromString: "localhost:5555/helloWorld")
190190
}
191+
192+
#expect(throws: ImageReference.Repository.ValidationError.invalidReferenceFormat("hello^world")) {
193+
try ImageReference(fromString: "localhost:5555/hello^world")
194+
}
191195
}
192196

193197
@Test

0 commit comments

Comments
 (0)