Compare commits

Author SHA1 Message Date
nessarandClaude Opus 5.5 263ec0fc5b iam_5: test s3:prefix behavior instead of an exact value set
The ListOwnFolder test required s3:prefix to be exactly
{"home/${aws:username}/", "home/${aws:username}/*"}. In IAM StringLike, "*"
also matches zero characters, so "home/${aws:username}/*" alone already allows
Prefix="home/alice/" (AWS's own home-directory examples use only that value).

The test now resolves ${aws:username} for alice and simulates StringLike with
fnmatchcase: home/alice/ and home/alice/2026/ must be allowed; home/bob/, the
empty prefix, home/alice (no slash, would expose home/alice2/... key names) and
home/alice2/ must be denied. Docstrings updated; the solution uses the single
pattern.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:17:14 +01:00
nessar 25d5c3727e Merge pull request 'Type boto3 clients and tables in exercises and solutions' (#18) from worktree-typed-exercises into main 2026-10-31 20:58:50 +01:00
2 changed files with 44 additions and 9 deletions

No files matched your search

+21 -4
View File
@@ -14,8 +14,10 @@ Building the policy with a Python f-string silently breaks it (try it!).
Your task
---------
Fix HOME_FOLDER_POLICY so that it has two statements:
* "ListOwnFolder": s3:ListBucket on the bucket, only for prefixes
`home/${aws:username}/` and `home/${aws:username}/*`,
* "ListOwnFolder": s3:ListBucket on the bucket, only when the requested
prefix is inside the user's folder (`home/${aws:username}/`,
`home/${aws:username}/2026/`...). Mind the trailing slash: a request
with Prefix="home/alice" would also list the keys of `home/alice2/`,
* "ManageOwnFiles": s3:GetObject, s3:PutObject and s3:DeleteObject on
the objects under `home/${aws:username}/`.
"""
@@ -75,8 +77,23 @@ def test_list_bucket_statement():
s = _by_sid("ListOwnFolder")
assert _as_list(s["Action"]) == ["s3:ListBucket"]
assert _as_list(s["Resource"]) == ["arn:aws:s3:::corp-docs"], "ListBucket is a bucket-level action"
prefixes = set(_as_list(s["Condition"]["StringLike"]["s3:prefix"]))
assert prefixes == {"home/${aws:username}/", "home/${aws:username}/*"}
condition = s.get("Condition", {}).get("StringLike", {})
assert "s3:prefix" in condition, 'narrow ListBucket with "Condition": {"StringLike": {"s3:prefix": [...]}}'
patterns = _as_list(_resolve(condition["s3:prefix"], "alice"))
# IAM StringLike: "*" = any run of characters (even an empty one), "?" = exactly one character
def alice_may_list(prefix):
return any(fnmatch.fnmatchcase(prefix, p) for p in patterns)
for prefix in ("home/alice/", "home/alice/2026/"):
assert alice_may_list(prefix), f"alice must be able to list her own folder with Prefix={prefix!r}"
for prefix in ("home/bob/", ""):
assert not alice_may_list(prefix), f"alice must not be able to list with Prefix={prefix!r}"
for prefix in ("home/alice", "home/alice2/"):
assert not alice_may_list(prefix), (
f"Prefix={prefix!r} must be denied: Prefix='home/alice' (no trailing slash) also returns the key "
"names of home/alice2/..., so keep the slash right after ${aws:username} in every pattern"
)
def test_object_statement():
+23 -5
View File
@@ -14,8 +14,10 @@ Building the policy with a Python f-string silently breaks it (try it!).
Your task
---------
Fix HOME_FOLDER_POLICY so that it has two statements:
* "ListOwnFolder": s3:ListBucket on the bucket, only for prefixes
`home/${aws:username}/` and `home/${aws:username}/*`,
* "ListOwnFolder": s3:ListBucket on the bucket, only when the requested
prefix is inside the user's folder (`home/${aws:username}/`,
`home/${aws:username}/2026/`...). Mind the trailing slash: a request
with Prefix="home/alice" would also list the keys of `home/alice2/`,
* "ManageOwnFiles": s3:GetObject, s3:PutObject and s3:DeleteObject on
the objects under `home/${aws:username}/`.
"""
@@ -30,7 +32,8 @@ HOME_FOLDER_POLICY = {
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": f"arn:aws:s3:::{BUCKET}",
"Condition": {"StringLike": {"s3:prefix": ["home/${aws:username}/", "home/${aws:username}/*"]}},
# "*" also matches zero characters, so this pattern covers Prefix="home/alice/" itself.
"Condition": {"StringLike": {"s3:prefix": ["home/${aws:username}/*"]}},
},
{
"Sid": "ManageOwnFiles",
@@ -71,8 +74,23 @@ def test_list_bucket_statement():
s = _by_sid("ListOwnFolder")
assert _as_list(s["Action"]) == ["s3:ListBucket"]
assert _as_list(s["Resource"]) == ["arn:aws:s3:::corp-docs"], "ListBucket is a bucket-level action"
prefixes = set(_as_list(s["Condition"]["StringLike"]["s3:prefix"]))
assert prefixes == {"home/${aws:username}/", "home/${aws:username}/*"}
condition = s.get("Condition", {}).get("StringLike", {})
assert "s3:prefix" in condition, 'narrow ListBucket with "Condition": {"StringLike": {"s3:prefix": [...]}}'
patterns = _as_list(_resolve(condition["s3:prefix"], "alice"))
# IAM StringLike: "*" = any run of characters (even an empty one), "?" = exactly one character
def alice_may_list(prefix):
return any(fnmatch.fnmatchcase(prefix, p) for p in patterns)
for prefix in ("home/alice/", "home/alice/2026/"):
assert alice_may_list(prefix), f"alice must be able to list her own folder with Prefix={prefix!r}"
for prefix in ("home/bob/", ""):
assert not alice_may_list(prefix), f"alice must not be able to list with Prefix={prefix!r}"
for prefix in ("home/alice", "home/alice2/"):
assert not alice_may_list(prefix), (
f"Prefix={prefix!r} must be denied: Prefix='home/alice' (no trailing slash) also returns the key "
"names of home/alice2/..., so keep the slash right after ${aws:username} in every pattern"
)
def test_object_statement():