Skip to content

Commit

Permalink
fix: improve wildcard handling in authorizer policy resource parser (#…
Browse files Browse the repository at this point in the history
…1797)

* fix: improve wildcard handling in authorizer policy resource parser

* refactor: remove unneeded condition as it's handled by the new parsing logic

* refactor: remove unneeded escape

* refactor: optimize regexp
  • Loading branch information
G-Rath committed Jun 18, 2024
1 parent 600c93e commit 0203d04
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/events/authMatchPolicyResource.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function parseResource(resource) {
const [, region, accountId, restApiId, path] = resource.match(
/arn:aws:execute-api:(.*?):(.*?):(.*?)\/(.*)/,
)
const [, region = "*", accountId = "*", restApiId = "*", path = "*"] =
resource.match(
/arn:aws:execute-api:([^\s:]+)(?::([^\s:]+))?(?::([^\s/:]+))?(?:\/(.*))?/,
)

return {
accountId,
Expand All @@ -26,10 +27,6 @@ export default function authMatchPolicyResource(policyResource, resource) {
return true
}

if (policyResource === "arn:aws:execute-api:*:*:*") {
return true
}

if (policyResource.includes("*") || policyResource.includes("?")) {
// Policy contains a wildcard resource

Expand Down
75 changes: 75 additions & 0 deletions tests/old-unit/authMatchPolicyResource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,81 @@ describe("authMatchPolicyResource", () => {
})
})

describe("when the resource defines all segments with a wildcard", () => {
const wildcardResource = "arn:aws:execute-api:*:*:*"

it("matches anything", () => {
for (const resource of [
"arn:aws:execute-api:eu-west-1:random-account-id:random-api-id/development/GET/dinosaurs",
"arn:aws:execute-api:us-west-1:123456:random-api-id/development/GET/diinosaurs",
"arn:aws:execute-api:eu-west-2:123abc:random-api-id/development/PUT/dinosaurs",
"arn:aws:execute-api:eu-west-1:random-account-id:123abc/development/GET/dinosaurs:extinct",
"arn:aws:execute-api:ap-southeast-1:random-account-id:random-api-id/development/GET/diinosaurs",
]) {
assert.strictEqual(
authMatchPolicyResource(wildcardResource, resource),
true,
)
}
})
})

describe("when the resource ends with a wildcarded region segment", () => {
const wildcardResource = "arn:aws:execute-api:*"

it("matches anything", () => {
for (const resource of [
"arn:aws:execute-api:eu-west-1:random-account-id:random-api-id/development/GET/dinosaurs",
"arn:aws:execute-api:us-west-1:123456:random-api-id/development/GET/diinosaurs",
"arn:aws:execute-api:eu-west-2:123abc:random-api-id/development/PUT/dinosaurs",
"arn:aws:execute-api:eu-west-1:random-account-id:123abc/development/GET/dinosaurs:extinct",
"arn:aws:execute-api:ap-southeast-1:random-account-id:random-api-id/development/GET/diinosaurs",
]) {
assert.strictEqual(
authMatchPolicyResource(wildcardResource, resource),
true,
)
}
})
})

describe("when the resource ends with a wildcarded account-id segment", () => {
const wildcardResource = "arn:aws:execute-api:eu-west-1:*"

describe("and the resource is in the same region", () => {
it("matches regardless of what comes afterwards", () => {
for (const resource of [
"arn:aws:execute-api:eu-west-1:random-account-id:random-api-id/development/GET/dinosaurs",
"arn:aws:execute-api:eu-west-1:123456:random-api-id/development/GET/diinosaurs",
"arn:aws:execute-api:eu-west-1:123abc:random-api-id/development/PUT/dinosaurs",
"arn:aws:execute-api:eu-west-1:random-account-id:123abc/development/GET/dinosaurs:extinct",
]) {
assert.strictEqual(
authMatchPolicyResource(wildcardResource, resource),
true,
)
}
})
})

describe("and the resource is in a different region", () => {
it("does not match regardless of what comes afterwards", () => {
for (const resource of [
"arn:aws:execute-api:eu-west-2:random-account-id:random-api-id/development/GET/dinosaurs",
"arn:aws:execute-api:us-west-1:123456:random-api-id/development/GET/diinosaurs",
"arn:aws:execute-api:eu-west-2:123abc:random-api-id/development/PUT/dinosaurs",
"arn:aws:execute-api:eu-west-2:random-account-id:123abc/development/GET/dinosaurs:extinct",
"arn:aws:execute-api:ap-southeast-1:random-account-id:random-api-id/development/GET/diinosaurs",
]) {
assert.strictEqual(
authMatchPolicyResource(wildcardResource, resource),
false,
)
}
})
})
})

describe("when the resource has wildcards", () => {
describe("and it matches", () => {
const wildcardResource =
Expand Down

0 comments on commit 0203d04

Please sign in to comment.