One PUBLIC method's input sources, gathered during the single traversal.
| 625 | |
| 626 | |
| 627 | class _InputCtx: |
| 628 | """One PUBLIC method's input sources, gathered during the single traversal.""" |
| 629 | |
| 630 | def __init__(self, declared_params: set[str], declared_body: set[str]) -> None: |
| 631 | self.declared_params = declared_params |
| 632 | self.declared_body = declared_body |
| 633 | self.query_locals: set[str] = set() |
| 634 | self.body_locals: set[str] = set() |
| 635 | # (line, col, serializer) for each serializer built from that source |
| 636 | self.query_validators: list[tuple[int, int, str]] = [] |
| 637 | self.body_validators: list[tuple[int, int, str]] = [] |
| 638 | # (line, col, key, source) reads with a literal key |
| 639 | self.literal_reads: list[tuple[int, int, str, str]] = [] |
| 640 | # (line, col, rendered key, source) reads whose key is computed |
| 641 | self.computed_reads: list[tuple[int, int, str, str]] = [] |
| 642 | # (line, col, callee, source) the whole dict passed somewhere |
| 643 | self.hand_offs: list[tuple[int, int, str, str]] = [] |
| 644 | |
| 645 | def _is(self, node: ast.expr, attrs: frozenset[str], locals_: set[str]) -> bool: |
| 646 | node = _unwrap_copy(node) |
| 647 | if isinstance(node, ast.Name): |
| 648 | return node.id in locals_ |
| 649 | # The attribute has to hang off the request. `serializer.data` and |
| 650 | # `response.data` are outputs, not parameters a client sent. |
| 651 | return isinstance(node, ast.Attribute) and node.attr in attrs and _is_request(node.value) |
| 652 | |
| 653 | def is_query(self, node: ast.expr) -> bool: |
| 654 | return self._is(node, _QUERY_ATTRS, self.query_locals) |
| 655 | |
| 656 | def is_body(self, node: ast.expr) -> bool: |
| 657 | return self._is(node, frozenset(("data",)), self.body_locals) |
| 658 | |
| 659 | def source_of(self, node: ast.expr) -> str | None: |
| 660 | """ "query string" / "request body" for an input source, else None.""" |
| 661 | if self.is_query(node): |
| 662 | return "query string" |
| 663 | if self.is_body(node): |
| 664 | return "request body" |
| 665 | return None |
| 666 | |
| 667 | def record_read(self, key: ast.expr, source: str, line: int, col: int) -> None: |
| 668 | if isinstance(key, ast.Constant) and isinstance(key.value, str): |
| 669 | self.literal_reads.append((line, col, key.value, source)) |
| 670 | else: |
| 671 | self.computed_reads.append((line, col, ast.unparse(key), source)) |
| 672 | |
| 673 | |
| 674 | class SentryVisitor(ast.NodeVisitor): |