Skip to content

Commit 944a551

Browse files
immu4989romanlutz
andauthored
FIX GCG: forward hf_token as None instead of an empty string (#2275)
Co-authored-by: Roman Lutz <romanlutz13@gmail.com>
1 parent 59f2280 commit 944a551

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎pyrit/executor/promptgen/gcg/attack/base/attack_manager.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,7 @@ class ModelWorker:
18191819
def __init__(
18201820
self,
18211821
model_path: str,
1822-
token: str,
1822+
token: str | None,
18231823
model_kwargs: dict[str, Any],
18241824
tokenizer: Any,
18251825
device: str,

‎pyrit/executor/promptgen/gcg/generator.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ def _to_attack_params(self, *, context: GCGContext) -> Any:
410410

411411
all_models = self._models + self._test_models
412412
return SimpleNamespace(
413-
token=self._hf_token or "",
413+
# None means anonymous access. An empty string makes huggingface_hub send
414+
# "Authorization: Bearer " with no credential, which httpx rejects as an
415+
# illegal header value, so public models would fail to load.
416+
token=self._hf_token,
414417
tokenizer_paths=[m.name for m in all_models],
415418
tokenizer_kwargs=[m.tokenizer_kwargs for m in all_models],
416419
model_paths=[m.name for m in all_models],

‎tests/unit/executor/promptgen/gcg/test_generator.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,27 @@ def test_test_length_mismatch_raises(self) -> None:
158158
)
159159

160160

161+
class TestToAttackParamsToken:
162+
"""The HuggingFace token forwarded to model loading."""
163+
164+
def test_token_is_none_when_not_supplied(self) -> None:
165+
# An empty string makes huggingface_hub send "Authorization: Bearer " with no
166+
# credential, which httpx rejects as an illegal header value. None means anonymous,
167+
# which is what loading a public model needs.
168+
gen = GCGGenerator(models=[GCGModelConfig(name=_LLAMA_2)])
169+
170+
params = gen._to_attack_params(context=GCGContext(goals=["g"], targets=["t"]))
171+
172+
assert params.token is None
173+
174+
def test_token_is_forwarded_when_supplied(self) -> None:
175+
gen = GCGGenerator(models=[GCGModelConfig(name=_LLAMA_2)], hf_token="hf_example")
176+
177+
params = gen._to_attack_params(context=GCGContext(goals=["g"], targets=["t"]))
178+
179+
assert params.token == "hf_example"
180+
181+
161182
@pytest.fixture
162183
def patched_get_workers():
163184
"""Patch the heavy worker spawn so lifecycle tests don't try to load real models."""

0 commit comments

Comments
 (0)