Skip to content

Commit 6e4c3d0

Browse files
correctly validate characters in semantic version identifiers
Semantic Versioning 2.0.0 allows only _ASCII_ alpha-numeric characters and "-" in identifiers.
1 parent 52c852b commit 6e4c3d0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Sources/PackageDescription/Version+StringLiteralConvertible.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ extension Version: LosslessStringConvertible {
7272
if let prereleaseDelimiterIndex = prereleaseDelimiterIndex {
7373
let prereleaseStartIndex = versionString.index(after: prereleaseDelimiterIndex)
7474
let prereleaseIdentifiers = versionString[prereleaseStartIndex..<(metadataDelimiterIndex ?? versionString.endIndex)].split(separator: ".", omittingEmptySubsequences: false)
75-
guard prereleaseIdentifiers.allSatisfy({ $0.allSatisfy({ $0.isLetter || $0.isNumber || $0 == "-" })}) else { return nil }
75+
guard prereleaseIdentifiers.allSatisfy( {
76+
$0.allSatisfy( { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") } )
77+
} ) else { return nil }
7678
self.prereleaseIdentifiers = prereleaseIdentifiers.map { String($0) }
7779
} else {
7880
self.prereleaseIdentifiers = []
@@ -81,7 +83,9 @@ extension Version: LosslessStringConvertible {
8183
if let metadataDelimiterIndex = metadataDelimiterIndex {
8284
let metadataStartIndex = versionString.index(after: metadataDelimiterIndex)
8385
let buildMetadataIdentifiers = versionString[metadataStartIndex...].split(separator: ".", omittingEmptySubsequences: false)
84-
guard buildMetadataIdentifiers.allSatisfy({ $0.allSatisfy({ $0.isLetter || $0.isNumber || $0 == "-" })}) else { return nil }
86+
guard buildMetadataIdentifiers.allSatisfy( {
87+
$0.allSatisfy( { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") } )
88+
} ) else { return nil }
8589
self.buildMetadataIdentifiers = buildMetadataIdentifiers.map { String($0) }
8690
} else {
8791
self.buildMetadataIdentifiers = []

Sources/PackageDescription/Version.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ public struct Version {
6868
buildMetadataIdentifiers: [String] = []
6969
) {
7070
precondition(major >= 0 && minor >= 0 && patch >= 0, "Negative versioning is invalid.")
71+
precondition(
72+
prereleaseIdentifiers.allSatisfy {
73+
$0.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") }
74+
},
75+
#"Pre-release identifiers can contain only ASCII alpha-numeric characters and "-"."#
76+
)
77+
precondition(
78+
buildMetadataIdentifiers.allSatisfy {
79+
$0.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") }
80+
},
81+
#"Build metadata identifiers can contain only ASCII alpha-numeric characters and "-"."#
82+
)
7183
self.major = major
7284
self.minor = minor
7385
self.patch = patch

0 commit comments

Comments
 (0)