Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
263ec0fc5b | ||
|
|
25d5c3727e |
No files matched your search
@@ -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():
|
||||
|
||||
@@ -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():
|
||||
|
||||
Reference in new issue
Block a user