Skip to content

Commit b18c2ea

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 b18c2ea

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Sources/PackageDescription/Version.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public struct Version {
6060
/// - patch: The patch version number.
6161
/// - prereleaseIdentifiers: The pre-release identifier.
6262
/// - buildMetaDataIdentifiers: Build metadata that identifies a build.
63+
///
64+
/// - Precondition: `major >= 0 && minor >= 0 && patch >= 0`.
65+
/// - Precondition: `prereleaseIdentifiers` can conatin only ASCII alpha-numeric characters and "-".
66+
/// - Precondition: `buildMetaDataIdentifiers` can conatin only ASCII alpha-numeric characters and "-".
6367
public init(
6468
_ major: Int,
6569
_ minor: Int,
@@ -68,6 +72,18 @@ public struct Version {
6872
buildMetadataIdentifiers: [String] = []
6973
) {
7074
precondition(major >= 0 && minor >= 0 && patch >= 0, "Negative versioning is invalid.")
75+
precondition(
76+
prereleaseIdentifiers.allSatisfy {
77+
$0.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") }
78+
},
79+
#"Pre-release identifiers can contain only ASCII alpha-numeric characters and "-"."#
80+
)
81+
precondition(
82+
buildMetadataIdentifiers.allSatisfy {
83+
$0.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") }
84+
},
85+
#"Build metadata identifiers can contain only ASCII alpha-numeric characters and "-"."#
86+
)
7187
self.major = major
7288
self.minor = minor
7389
self.patch = patch

0 commit comments

Comments
 (0)