Skip to content

Commit

Permalink
update get+set of http library agent values/methods
Browse files Browse the repository at this point in the history
  • Loading branch information
leondz committed Oct 29, 2024
1 parent 68c8991 commit 36319c3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
33 changes: 32 additions & 1 deletion garak/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,40 @@ def _store_config(settings_files) -> None:
run.user_agent = run.user_agent.replace("{version}", version)
plugins = _set_settings(plugins, settings["plugins"])
reporting = _set_settings(reporting, settings["reporting"])


def set_all_http_lib_agents(agent_string):
set_http_lib_agents(
{"requests": agent_string, "httpx": agent_string, "aiohttp": agent_string}
)


def set_http_lib_agents(agent_strings: dict):
if "requests" in agent_strings:
from requests import utils

utils.default_user_agent = lambda x=None: agent_strings["requests"]
if "httpx" in agent_strings:
import httpx

httpx._client.USER_AGENT = agent_strings["httpx"]
if "aiohttp" in agent_strings:
import aiohttp

aiohttp.client_reqrep.SERVER_SOFTWARE = agent_strings["aiohttp"]


def get_http_lib_agents():
from requests import utils
import httpx
import aiohttp

agent_strings = {}
agent_strings["requests"] = utils.default_user_agent
agent_strings["httpx"] = httpx._client.USER_AGENT
agent_strings["aiohttp"] = aiohttp.client_reqrep.SERVER_SOFTWARE

utils.default_user_agent = run.user_agent
return agent_strings


def load_base_config() -> None:
Expand Down
11 changes: 11 additions & 0 deletions garak/harnesses/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def _load_buffs(self, buff_names: List) -> None:
logging.warning(err_msg)
continue

def _start_run_hook(self):
self._http_lib_user_agents = _config.get_http_lib_agents()
_config.set_all_http_lib_agents(_config.run.user_agent)

def _end_run_hook(self):
_config.set_http_lib_agents(self._http_lib_user_agents)

def run(self, model, probes, detectors, evaluator, announce_probe=True) -> None:
"""Core harness method
Expand Down Expand Up @@ -92,6 +99,8 @@ def run(self, model, probes, detectors, evaluator, announce_probe=True) -> None:
print(msg)
raise ValueError(msg)

self._start_run_hook()

for probe in probes:
logging.debug("harness: probe start for %s", probe.probename)
if not probe:
Expand Down Expand Up @@ -135,4 +144,6 @@ def run(self, model, probes, detectors, evaluator, announce_probe=True) -> None:
else:
evaluator.evaluate(attempt_results)

self._end_run_hook()

logging.debug("harness: probe list iteration completed")
19 changes: 19 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
import tempfile

import aiohttp.client_reqrep
import pytest

from pathlib import Path
Expand Down Expand Up @@ -764,3 +765,21 @@ def test_nested():

_config.plugins.generators["a"]["b"]["c"]["d"] = "e"
assert _config.plugins.generators["a"]["b"]["c"]["d"] == "e"


def test_get_user_agents():
agents = _config.get_http_lib_agents()
assert isinstance(agents, dict)


def test_set_agents():
from requests import utils
import httpx
import aiohttp

agent_test = "garak/9 - only simple tailors edition"
_config.set_all_http_lib_agents(agent_test)

assert str(utils.default_user_agent()) == agent_test
assert httpx._client.USER_AGENT == agent_test
assert aiohttp.client_reqrep.SERVER_SOFTWARE == agent_test

0 comments on commit 36319c3

Please sign in to comment.