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: ensure resource policy matches the whole arn #1798

Merged
merged 1 commit 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
2 changes: 1 addition & 1 deletion src/events/authMatchPolicyResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function authMatchPolicyResource(policyResource, resource) {
// for the requested resource and the resource defined in the policy
// Need to create a regex replacing ? with one character and * with any number of characters
const regExp = new RegExp(
parsedPolicyResource.path.replaceAll("*", ".*").replaceAll("?", "."),
`${parsedPolicyResource.path.replaceAll("*", ".*").replaceAll("?", ".")}$`,
)

return regExp.test(parsedResource.path)
Expand Down
32 changes: 32 additions & 0 deletions tests/old-unit/authMatchPolicyResource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,38 @@ describe("authMatchPolicyResource", () => {
})
})
})

describe("when the resource has segment wildcards", () => {
const wildcardResource =
"arn:aws:execute-api:*:*:random-api-id/local/GET/organizations"

describe("and it matches", () => {
it("returns true", () => {
const resource =
"arn:aws:execute-api:eu-west-1:random-account-id:random-api-id/local/GET/organizations"

assert.strictEqual(
authMatchPolicyResource(wildcardResource, resource),
true,
)
})
})

describe("and it does not match", () => {
it("returns false", () => {
for (const resource of [
"arn:aws:execute-api:eu-west-1:random-account-id:random-api-id/local/GET/me",
"arn:aws:execute-api:eu-west-1:random-account-id:random-api-id/local/GET/organisations",
"arn:aws:execute-api:eu-west-1:random-account-id:random-api-id/local/GET/organizations/1",
]) {
assert.strictEqual(
authMatchPolicyResource(wildcardResource, resource),
false,
)
}
})
})
})
})

describe("when the resource has single character wildcards", () => {
Expand Down
Loading