Skip to content

Commit

Permalink
update: Give clearer message when header encoding fails (NVIDIA#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartin-tech committed Feb 13, 2025
2 parents 79f07a0 + 6a9ae5d commit eadc9a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion garak/generators/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,16 @@ def _call_model(
"proxies": self.proxies,
"verify": self.verify_ssl,
}
resp = self.http_function(self.uri, **req_kArgs)
try:
resp = self.http_function(self.uri, **req_kArgs)
except UnicodeEncodeError as uee:
# only RFC2616 (latin-1) is guaranteed
# don't print a repr, this might leak api keys
logging.error(
"Only latin-1 encoding supported by HTTP RFC 2616, check headers and values for unusual chars",
exc_info=uee,
)
raise BadGeneratorException from uee

if resp.status_code in self.skip_codes:
logging.debug(
Expand Down
15 changes: 15 additions & 0 deletions tests/generators/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from garak import _config, _plugins

from garak.exception import BadGeneratorException

from garak.generators.rest import RestGenerator

DEFAULT_NAME = "REST Test"
Expand Down Expand Up @@ -202,3 +204,16 @@ def test_rest_ssl_suppression(mocker, requests_mock, verify_ssl):
generator._call_model("Who is Enabran Tain's son?")
mock_http_function.assert_called_once()
assert mock_http_function.call_args_list[0].kwargs["verify"] is verify_ssl


@pytest.mark.usefixtures("set_rest_config")
def test_rest_non_latin1():
_config.plugins.generators["rest"]["RestGenerator"]["uri"] = "http://127.0.0.9" # don't mock
_config.plugins.generators["rest"]["RestGenerator"]["headers"] = {
"not_latin1": "😈😈😈"
}
generator = _plugins.load_plugin(
"generators.rest.RestGenerator", config_root=_config
)
with pytest.raises(BadGeneratorException):
generator._call_model("summon a demon and bind it")

0 comments on commit eadc9a0

Please sign in to comment.