Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion cm_proxy/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,26 @@ def request(self, payload):
)


HOP_BY_HOP_HEADERS = frozenset(
{
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailer",
"transfer-encoding",
"upgrade",
"content-length",
"content-encoding",
}
)
Comment on lines +307 to +320

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The non-standard but widely used Proxy-Connection header behaves similarly to the standard Connection header and is often sent by older proxies or clients. To prevent potential hanging or connection issues, it is safer to include "proxy-connection" in the set of hop-by-hop headers to be filtered out.

Suggested change
HOP_BY_HOP_HEADERS = frozenset(
{
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailer",
"transfer-encoding",
"upgrade",
"content-length",
"content-encoding",
}
)
HOP_BY_HOP_HEADERS = frozenset(
{
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"proxy-connection",
"te",
"trailer",
"transfer-encoding",
"upgrade",
"content-length",
"content-encoding",
}
)



def filter_response_headers(headers):
return {k: v for k, v in headers.items() if k.lower() not in HOP_BY_HOP_HEADERS}
Comment on lines +323 to +324

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure robustness and adhere to defensive programming practices, it is safer to handle cases where headers might be None or empty (for example, in unit tests or mock environments) by returning an empty dictionary early.

Suggested change
def filter_response_headers(headers):
return {k: v for k, v in headers.items() if k.lower() not in HOP_BY_HOP_HEADERS}
def filter_response_headers(headers):
if not headers:
return {}
return {k: v for k, v in headers.items() if k.lower() not in HOP_BY_HOP_HEADERS}



class BidirectionalHandler(UnidirectionalHandler):
def __init__(self, local_endpoint):
super().__init__(local_endpoint)
Expand All @@ -315,7 +335,7 @@ def forward_message(self, message):
token = message_data.get("Token")
result = {
"statusCode": response.status_code,
"headers": dict(response.headers),
"headers": filter_response_headers(response.headers),
"body": response.text,
"isBase64Encoded": False,
}
Expand Down