From b18c2eadd450912e8475a4c9f2c5546d96307b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=80=E5=8D=93=E7=96=8C?= <55120045+WowbaggersLiquidLunch@users.noreply.github.com> Date: Sat, 6 Nov 2021 01:31:51 +0800 Subject: [PATCH] correctly validate characters in semantic version identifiers Semantic Versioning 2.0.0 allows only _ASCII_ alpha-numeric characters and "-" in identifiers. --- Sources/PackageDescription/Version.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Sources/PackageDescription/Version.swift b/Sources/PackageDescription/Version.swift index 4fcacb921cf..b470670f6dc 100644 --- a/Sources/PackageDescription/Version.swift +++ b/Sources/PackageDescription/Version.swift @@ -60,6 +60,10 @@ public struct Version { /// - patch: The patch version number. /// - prereleaseIdentifiers: The pre-release identifier. /// - buildMetaDataIdentifiers: Build metadata that identifies a build. + /// + /// - Precondition: `major >= 0 && minor >= 0 && patch >= 0`. + /// - Precondition: `prereleaseIdentifiers` can conatin only ASCII alpha-numeric characters and "-". + /// - Precondition: `buildMetaDataIdentifiers` can conatin only ASCII alpha-numeric characters and "-". public init( _ major: Int, _ minor: Int, @@ -68,6 +72,18 @@ public struct Version { buildMetadataIdentifiers: [String] = [] ) { precondition(major >= 0 && minor >= 0 && patch >= 0, "Negative versioning is invalid.") + precondition( + prereleaseIdentifiers.allSatisfy { + $0.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") } + }, + #"Pre-release identifiers can contain only ASCII alpha-numeric characters and "-"."# + ) + precondition( + buildMetadataIdentifiers.allSatisfy { + $0.allSatisfy { $0.isASCII && ($0.isLetter || $0.isNumber || $0 == "-") } + }, + #"Build metadata identifiers can contain only ASCII alpha-numeric characters and "-"."# + ) self.major = major self.minor = minor self.patch = patch