Skip to content

Commit

Permalink
Remove metaCanonicalPath from V4 and test directly against escapePath…
Browse files Browse the repository at this point in the history
… and canonicalPath
  • Loading branch information
mwu committed Sep 4, 2022
1 parent a4fd156 commit ad994a8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
8 changes: 3 additions & 5 deletions lib/amazonka-core/src/Amazonka/Sign/V4/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ data V4 = V4
metaPath :: Path,
metaEndpoint :: Endpoint,
metaCredential :: Credential,
metaCanonicalPath :: CanonicalPath,
metaCanonicalQuery :: CanonicalQuery,
metaCanonicalRequest :: CanonicalRequest,
metaCanonicalHeaders :: CanonicalHeaders,
Expand Down Expand Up @@ -184,7 +183,6 @@ signMetadata a r ts presign digest rq =
metaPath = path,
metaEndpoint = end,
metaCredential = cred,
metaCanonicalPath = cpath,
metaCanonicalQuery = query,
metaCanonicalRequest = crq,
metaCanonicalHeaders = chs,
Expand All @@ -209,7 +207,7 @@ signMetadata a r ts presign digest rq =
end = _serviceEndpoint svc r
method = Tag . toBS $ _requestMethod rq
path = escapedPath rq
cpath = escapedCanonicalPath rq
cpath = canonicalPath rq

svc = _requestService rq

Expand Down Expand Up @@ -272,8 +270,8 @@ escapedPath r = Tag . toBS . escapePath $
"S3" -> _requestPath r
_ -> collapsePath (_requestPath r)

escapedCanonicalPath :: Request a -> CanonicalPath
escapedCanonicalPath r = Tag $
canonicalPath :: Request a -> CanonicalPath
canonicalPath r = Tag $
case _serviceAbbrev (_requestService r) of
"S3" -> toBS (escapePath path)
_ -> toBS (escapePathTwice (collapsePath path))
Expand Down
31 changes: 15 additions & 16 deletions lib/amazonka-core/test/Test/Amazonka/Sign/V4/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,51 +39,51 @@ testRequestPathsEscapedOnce = QC.forAll (mkV4Paths arbitrary mkNormalizedPath) $
untag reqPath == toBS (escapePath raw)

testS3CanonicalPathsEscapedOnce :: Property
testS3CanonicalPathsEscapedOnce = QC.forAll (mkV4Paths (pure "S3") mkNormalizedPath) $ \(_, reqPath, canonicalPath) ->
untag reqPath == untag canonicalPath
testS3CanonicalPathsEscapedOnce = QC.forAll (mkV4Paths (pure "S3") mkNormalizedPath) $ \(_, reqPath, canonicalPath') ->
untag reqPath == untag canonicalPath'

testNonS3CanonicalPathsEscapedTwice :: Property
testNonS3CanonicalPathsEscapedTwice = QC.forAll (mkV4Paths mkNonS3Service mkNormalizedPath) $ \(raw, _, canonicalPath) ->
untag canonicalPath == toBS (escapePathTwice raw)
testNonS3CanonicalPathsEscapedTwice = QC.forAll (mkV4Paths mkNonS3Service mkNormalizedPath) $ \(raw, _, canonicalPath') ->
untag canonicalPath' == toBS (escapePathTwice raw)

testEmptyPath :: Assertion
testEmptyPath = do
let empty = Raw []
(_, reqPath, canonicalPath) <- generate $ mkV4Paths arbitrary (pure empty)
(_, reqPath, canonicalPath') <- generate $ mkV4Paths arbitrary (pure empty)
toBS reqPath @?= "/"
toBS canonicalPath @?= "/"
toBS canonicalPath' @?= "/"

testNonS3CanonicalPathExample :: Assertion
testNonS3CanonicalPathExample = do
let example = Raw ["documents and settings"]

(_, reqPath, canonicalPath) <- generate $ mkV4Paths mkNonS3Service (pure example)
(_, reqPath, canonicalPath') <- generate $ mkV4Paths mkNonS3Service (pure example)
toBS reqPath @?= "/documents%20and%20settings"
toBS canonicalPath @?= "/documents%2520and%2520settings"
toBS canonicalPath' @?= "/documents%2520and%2520settings"

testS3CanonicalPathExample :: Assertion
testS3CanonicalPathExample = do
let example = Raw ["documents and settings"]

(_, reqPath, canonicalPath) <- generate $ mkV4Paths (pure "S3") (pure example)
(_, reqPath, canonicalPath') <- generate $ mkV4Paths (pure "S3") (pure example)
toBS reqPath @?= "/documents%20and%20settings"
untag canonicalPath @?= untag reqPath
untag canonicalPath' @?= untag reqPath

testS3ShouldNotNormalize :: Assertion
testS3ShouldNotNormalize = do
let key = Raw ["foo", "..", "bar", ".", "baz", "."]

(_, reqPath, canonicalPath) <- generate $ mkV4Paths (pure "S3") (pure key)
(_, reqPath, canonicalPath') <- generate $ mkV4Paths (pure "S3") (pure key)
toBS reqPath @?= "/foo/../bar/./baz/."
untag canonicalPath @?= untag reqPath
untag canonicalPath' @?= untag reqPath

testNonS3ShouldNormalize :: Assertion
testNonS3ShouldNormalize = do
let key = Raw ["foo", "..", "bar", ".", "baz", "."]

(_, reqPath, canonicalPath) <- generate $ mkV4Paths mkNonS3Service (pure key)
(_, reqPath, canonicalPath') <- generate $ mkV4Paths mkNonS3Service (pure key)
toBS reqPath @?= "/bar/baz"
untag canonicalPath @?= untag reqPath
untag canonicalPath' @?= untag reqPath

mkV4Paths :: Gen Abbrev -> Gen RawPath -> Gen (RawPath, Base.Path, CanonicalPath)
mkV4Paths genAbbrev genPath = do
Expand All @@ -104,8 +104,7 @@ mkV4Paths genAbbrev genPath = do
& requestService .~ svc
& requestBody .~ Hashed aBody

(meta, _) <- base (Tag (sha256Base16 aBody)) req <$> arbitrary <*> arbitrary <*> arbitrary
pure (aPath, metaPath meta, metaCanonicalPath meta)
pure (aPath, escapedPath req, canonicalPath req)

mkNormalizedPath :: Gen RawPath
mkNormalizedPath = arbitrary `suchThat` noDots
Expand Down

0 comments on commit ad994a8

Please sign in to comment.