Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions pyrit/prompt_target/http_target/http_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ def parse_raw_http_request(self, http_request: str) -> tuple[dict[str, str], Req

body = ""

# Split the request into headers and body by finding the double newlines (\n\n)
request_parts = http_request.strip().split("\n\n", 1)
# Split the request into headers and body by finding the double newlines (\n\n).
# Preserve body whitespace exactly as provided in the raw request.
request_parts = http_request.split("\n\n", 1)

# Parse out the header components
header_lines = request_parts[0].strip().split("\n")
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/target/test_http_target_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ def test_parse_raw_http_request_preserves_relative_url_case(sqlite_instance):
assert version == "HTTP/1.1"


def test_parse_raw_http_request_preserves_body_trailing_whitespace(sqlite_instance):
request = "POST /submit HTTP/1.1\nHost: example.com\nContent-Type: text/plain\n\nhello \n"
target = HTTPTarget(http_request=request)

headers, body, url, method, version = target.parse_raw_http_request(request)

assert url == "https://example.com/submit"
assert method == "POST"
assert headers == {"host": "example.com", "content-type": "text/plain"}
assert body == "hello \n"
assert version == "HTTP/1.1"


def test_parse_regex_response_no_match():
mock_response = MagicMock()
mock_response.content = b"<html><body>No match here</body></html>"
Expand Down