When constructing a URL for a Munki resource, percent-encode any + characters in the path.

This commit is contained in:
Greg Neagle
2026-04-23 13:17:26 -07:00
parent 6eda31b97f
commit 427a9268ff
2 changed files with 13 additions and 3 deletions
@@ -53,9 +53,15 @@ struct munkiRepoURLTests {
#expect(munkiRepoURL("manifests", resource: "site_default", munkiRepoURL: munkiBaseURL) == "https://munki.example.com/cgi?manifests/site_default")
}
/// Tests building URLs to Munki repo resources when base URL iwhen resource name contains a special character
/// Tests building URLs to Munki repo resources when resource name contains a special character
@Test func urlWithResourceWithSpecialCharacterContructsExpected() async throws {
let munkiBaseURL = "https://munki.example.com/repo"
#expect(munkiRepoURL("manifests", resource: "site default", munkiRepoURL: munkiBaseURL) == "https://munki.example.com/repo/manifests/site%20default")
}
/// Tests building URLs to Munki repo resources when resource name contains a +
@Test func urlWithResourceWithPlusCharacterContructsExpected() async throws {
let munkiBaseURL = "https://munki.example.com/repo"
#expect(munkiRepoURL("manifests", resource: "site+default", munkiRepoURL: munkiBaseURL) == "https://munki.example.com/repo/manifests/site%2Bdefault")
}
}
+6 -2
View File
@@ -58,7 +58,11 @@ func munkiRepoURL(_ type: String = "", resource: String = "", munkiRepoURL: Stri
"manifests": "ManifestURL",
"pkgs": "PackageURL",
]
guard let encodedType = (type as NSString).addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else {
// we're going to remove '+' from the set of characters allowed in the urlPath
// this forces '+' to be precent encoded, which makes some web servers happier
let allowedCharacters = CharacterSet.urlPathAllowed.subtracting(CharacterSet(charactersIn: "+"))
guard let encodedType = (type as NSString).addingPercentEncoding(withAllowedCharacters: allowedCharacters) else {
// encoding failed
return nil
}
@@ -84,7 +88,7 @@ func munkiRepoURL(_ type: String = "", resource: String = "", munkiRepoURL: Stri
if resource.isEmpty {
return typeURL
}
if let encodedResource = (resource as NSString).addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) {
if let encodedResource = (resource as NSString).addingPercentEncoding(withAllowedCharacters: allowedCharacters) {
return typeURL + encodedResource
}
// encoding failed