Skip to content

Commit f9f859f

Browse files
authored
fix: remove prohibited characters from error response (#62)
1 parent 60524f4 commit f9f859f

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/sagemaker_inference/transformer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ def handle_error(context, inference_exception, trace):
8585
Returns:
8686
str: The error message and stacktrace from the exception.
8787
"""
88-
context.set_response_status(
89-
code=inference_exception.status_code, phrase=inference_exception.phrase
90-
)
91-
return ["{}\n{}".format(inference_exception.message, trace)]
88+
phrase = utils.remove_crlf(inference_exception.phrase)
89+
message = utils.remove_crlf(inference_exception.message)
90+
stack_trace = utils.remove_crlf(trace)
91+
92+
context.set_response_status(code=inference_exception.status_code, phrase=phrase)
93+
return ["{} {}".format(message, stack_trace)]
9294

9395
def transform(self, data, context):
9496
"""Take a request with input data, deserialize it, make a prediction, and return a

src/sagemaker_inference/utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,24 @@ def parse_accept(accept):
7979
understand.
8080
"""
8181
return accept.replace(" ", "").split(",")
82+
83+
84+
def remove_crlf(illegal_string):
85+
"""Removes characters prohibited by the MMS dependency Netty.
86+
87+
https://github.com/netty/netty/issues/8312
88+
89+
Args:
90+
illegal_string: The string containing prohibited characters.
91+
92+
Returns:
93+
str: The input string with the prohibited characters removed.
94+
"""
95+
prohibited = ("\r", "\n")
96+
97+
sanitized_string = illegal_string
98+
99+
for character in prohibited:
100+
sanitized_string = sanitized_string.replace(character, " ")
101+
102+
return sanitized_string

test/unit/test_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from sagemaker_inference.utils import (
1717
parse_accept,
1818
read_file,
19+
remove_crlf,
1920
retrieve_content_type_header,
2021
write_file,
2122
)
@@ -92,3 +93,10 @@ def test_content_type_header(content_type_key):
9293
def test_parse_accept(input, expected):
9394
actual = parse_accept(input)
9495
assert actual == expected
96+
97+
98+
def test_remove_crlf():
99+
illegal_string = "test:\r\nstring"
100+
sanitized_string = "test: string"
101+
102+
assert sanitized_string == remove_crlf(illegal_string)

0 commit comments

Comments
 (0)