Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve wildcard handling in authorizer policy resource parser #1797

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading