-
Notifications
You must be signed in to change notification settings - Fork 456
feat(parser): add support for sourceIp with port #7315
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
Changes from 1 commit
028ed38
3a0a8a1
c65bed6
8e1f331
2b15fc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| import logging | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from pydantic import TypeAdapter | ||
| from pydantic import IPvAnyNetwork, TypeAdapter, ValidationError | ||
|
|
||
| from aws_lambda_powertools.shared.cache_dict import LRUDict | ||
|
|
||
|
|
@@ -82,3 +82,28 @@ def _parse_and_validate_event(data: dict[str, Any] | Any, adapter: TypeAdapter): | |
| data = json.loads(data) | ||
|
|
||
| return adapter.validate_python(data) | ||
|
|
||
|
|
||
| def _validate_source_ip(value): | ||
| """ | ||
| Handle sourceIp that may come with port (e.g., "10.1.15.242:39870") | ||
| in certain network configurations like Cloudflare + CloudFront + API Gateway. | ||
| Validates the IP part while preserving the original format. | ||
| See: https://github.com/aws-powertools/powertools-lambda-python/issues/7288 | ||
| """ | ||
|
|
||
| if value == "test-invoke-source-ip": | ||
| return value | ||
|
|
||
| try: | ||
| # The value is always an instance of str before Pydantic validation occurs. | ||
| # So the first thing to do is try to convert it. | ||
| IPvAnyNetwork(value) | ||
| except (ValidationError, ValueError): | ||
| try: | ||
| ip_part = value.split(":")[0] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has "IPv6-with-port" been taken into account? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I understand, the IPvAnyAddress model we're using supports both v4 and v6 - source There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I think this can break ipv6 yes in some situations. I need to run some tests before confirming this. Thanks @iBug There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I think the way we split the port might actually not work with IPv6 which contains multiple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's what I meant. I don't think it would parse when ip_part = value.rsplit(":", 1)[0]There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And probably with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reoepned this issue: #7288 |
||
| IPvAnyNetwork(ip_part) | ||
| except (ValidationError, ValueError, IndexError) as e: | ||
| raise ValueError(f"Invalid IP address in sourceIp: {ip_part}") from e | ||
|
|
||
| return value | ||
Uh oh!
There was an error while loading. Please reload this page.