Skip to content

Commit 143e8b8

Browse files
authored
Merge pull request #5 from aanklewicz/main
Percent encoding support for ( and )
2 parents b1bc0bb + a32ab7f commit 143e8b8

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
> **Maintainer needed**
2-
> This middleware was ported from Python to Swift for Munki 7 by Greg Neagle. However, Greg does not actually use this middleware and is not particularly motivated to support it. If you or your organization rely on this middleware, please consider taking over the responsibility for maintaining it.
1+
Middleware maintained by @aysiu & @aanklewicz
32

43
This is a project that builds an s3 middleware plugin for Munki 7.
54

S3Middleware/S3Middleware.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// S3Middleware
44
//
55
// Created by Greg Neagle on 5/11/25.
6+
// Modified by Adam Anklewicz on 2026-01-05 to allow percent encoding of perenthesis (using ISO formatted date as 1/5/26 is possibly January 5 or 1 May depending on locale).
67
//
78
// A proof-of-concept port of Wade Robson's s3 auth middleware
89
// https://github.com/waderobson/s3-auth
@@ -87,12 +88,29 @@ class S3RequestHeadersBuilder {
8788
// populate hashedRequest
8889
createCanonicalRequestHash()
8990
}
91+
92+
/* Function to allow () to be percentage encoded.
93+
Function tells it what characters to not encode, then encodes the rest.
94+
Documentation can be found here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html#sig-v4-examples-get-auth-header
95+
As per this documentation, one must URI encode every character
96+
URI encode every byte except the unreserved characters: 'A'-'Z', 'a'-'z', '0'-'9', '-', '.', '_', and '~'.
97+
The space character is a reserved character and must be encoded as "%20" (and not as "+").
98+
Encode the forward slash character, '/', everywhere except in the object key name. For example, if the object key name is photos/Jan/sample.jpg, the forward slash in the key name is not encoded. */
99+
private func awsUriEncode(_ string: String) -> String {
100+
var allowed = CharacterSet.alphanumerics // A-Z, a-z, 0-9 must not be encoded, so it's added to allowed.
101+
allowed.insert(charactersIn: "-._~") // Per the documentation, - . _ and ~ must not be encoded, so they are added to the allowed list.
102+
allowed.insert(charactersIn: "/") // Slashes must not be encoded, so add it to the allowed list of characters.
103+
return string.addingPercentEncoding(withAllowedCharacters: allowed) ?? string // addingPercentEncoding is built into Swift and will percent encode while passing it a list of allowed characters.
104+
}
90105

91106
/// build a canonical request string
92107
func createCanonicalRequestHash() {
93108
let method = "GET"
94109
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return }
95-
let canonicalURI = components.percentEncodedPath
110+
111+
// Call the function to handle () in the path
112+
let canonicalURI = awsUriEncode(url.path)
113+
96114
let host = components.percentEncodedHost ?? ""
97115
let canonicalizedQueryString = components.percentEncodedQuery ?? ""
98116
let canonicalHeaders = "host:\(host)\nx-amz-date:\(amzDate)\n"

0 commit comments

Comments
 (0)