Files
floci/tools/aws/test_fetch_lambda_response_permissions.py
T
Priamo Germosen 1dc6478108 fix(iam): return GetFunction's tags only to a caller allowed lambda:ListTags (#9782)
The Lambda API reference says of GetFunction's Tags "Lambda returns tag data only if you have explicit allow
permissions for lambda:ListTags", and the Service Reference lists lambda:ListTags among the actions GetFunction is
authorized by. Floci returned a function's tags to every caller allowed lambda:GetFunction.

tools/aws/fetch_lambda_response_permissions.py vendors, from the API reference page of each operation the Service
Reference lists as authorized by an action beside its own and the reference's operations page documents, each
response element's sentence saying it is returned only with a permission, with the element and the action. It
refuses a sentence naming a permission and a lambda: action in any other wording, and a page whose response elements
section is missing or lists no element without saying the response is empty, so a reworded or relaid page fails
rather than dropping the check. The authorized-actions generator reads it into a returnedOnlyIf
section, where botocore's response of the operation has a member of the element's name and the Service Reference
lists the action among the operation's: GetFunction returns Tags only with lambda:ListTags. IamAuthorizedActions
reads it, and LambdaController leaves the member out of a GetFunction response to a caller not allowed the action on
the function, checked with the function's tags through IamEnforcementFilter.authorizeEntryResource. TagsError is not
set, as no recording of AWS's answer for that case gives its code and message.

IamEnforcementFilter.requestAuthorization, which hands a handler the caller the filter evaluated, now treats a blank
Authorization header as no credential, as the filter itself does, and falls back to the presigned X-Amz-Credential;
before, a request with a blank header and a query credential passed the filter as that credential and the handler's
check as unsigned, so GetFunction returned Tags to a caller not allowed lambda:ListTags.
2026-11-29 22:09:26 -04:00

76 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""Tests for fetch_lambda_response_permissions.py, on the Service Reference and pages in the AWS Lambda API
reference's markup."""
from __future__ import annotations
import pytest
import fetch_lambda_response_permissions as f
def operation(name: str, *actions: str) -> dict:
return {"Name": name, "AuthorizedActions": [{"Service": action.split(":")[0], "Name": action.split(":")[1]}
for action in (f"lambda:{name}", *actions)]}
REFERENCE = {"Operations": [
operation("GetFunction", "lambda:ListTags"),
# An operation authorized by its own action alone has no page read, nor one the API reference does not list.
operation("GetAlias"),
operation("RunMicrovm", "lambda:GetMicrovmImage"),
]}
INDEX = ('<div id="main-col-body"><ul><li><a href="./API_GetFunction.html">GetFunction</a></li>'
'<li><a href="./API_GetAlias.html">GetAlias</a></li></ul></div><div class="prev-next"></div>')
TAGS = ('<dt id="lambda-GetFunction-response-Tags"><span class="term"><b><a href="#x">Tags</a></b></span></dt>\n'
'<dd><p>The function\'s <a href="#t">tags</a>. Lambda\n returns tag data only if you have explicit allow '
'permissions for <a href="#l">lambda:ListTags</a>.</p><p>Type: String to string map</p></dd>')
CONFIGURATION = ('<dt id="lambda-GetFunction-response-Configuration"><span class="term"><b>Configuration</b></span>'
'</dt><dd><p>The configuration of the function or version.</p></dd>')
def page(*elements: str, operation: str = "GetFunction") -> str:
section = (f"<dl>{''.join(elements)}</dl>" if elements
else "<p>If the action is successful, the service sends back an HTTP 204 response with an empty HTTP "
"body.</p>")
return (f'<div id="main-col-body"><h1>{operation}</h1><h2 id="API_{operation}_ResponseElements">Response '
f'Elements</h2>{section}<h2 id="API_{operation}_Errors">Errors</h2><p>Errors.</p></div>'
f'<div class="prev-next"></div>')
def test_a_response_element_returned_only_with_a_permission_is_read_with_its_action():
built = f.build(REFERENCE, INDEX, {"GetFunction": page(CONFIGURATION, TAGS)})
assert built["returnedOnlyIf"] == [{
"operation": "GetFunction", "element": "Tags",
"sentence": "Lambda returns tag data only if you have explicit allow permissions for lambda:ListTags.",
"action": "lambda:ListTags"}]
assert f.operations(REFERENCE, INDEX) == ["GetFunction"]
def test_a_page_whose_layout_or_wording_changed_is_refused():
with pytest.raises(ValueError, match="links to no operation"):
f.operations(REFERENCE, "<div id=\"main-col-body\"></div>")
# A sentence AWS rewords is refused rather than dropping its action, however it says it.
for reworded in (TAGS.replace("Lambda\n returns tag data", "Tags are returned"),
TAGS.replace("only if you have", "only when you have"),
TAGS.replace("Lambda\n returns tag data only if you have explicit allow permissions for",
"Reading tags requires the permission")):
with pytest.raises(ValueError, match="in no wording it used"):
f.build(REFERENCE, INDEX, {"GetFunction": page(reworded)})
# A page whose response elements cannot be found is refused, not read as returning everything.
with pytest.raises(ValueError, match="no response elements section"):
f.build(REFERENCE, INDEX, {"GetFunction": page(TAGS).replace("_ResponseElements", "_Output")})
with pytest.raises(ValueError, match="lists no response element and does not say its response is empty"):
f.build(REFERENCE, INDEX, {"GetFunction": page(TAGS).replace('<dt id="lambda-GetFunction-response-Tags">',
"<dt>")})
def test_a_page_saying_its_response_is_empty_returns_nothing_only_with_a_permission():
assert f.read_returned("GetFunction", page()) == []
def test_the_file_is_stale_when_the_reference_or_a_page_says_something_else():
pages = {"GetFunction": page(TAGS)}
vendored = f.build(REFERENCE, INDEX, pages)
assert not f.stale(vendored, REFERENCE, INDEX, pages)
assert f.stale(vendored, REFERENCE, INDEX, {"GetFunction": page(TAGS.replace("lambda:ListTags", "lambda:GetTags"))})