Skip to content

BUG GCGGenerator cannot load public models: empty hf_token sends an illegal 'Bearer ' header #2274

Description

@immu4989

What happens

GCGGenerator cannot load any public (ungated) HuggingFace model. Constructing it without an hf_token and executing raises:

_StrategyRuntimeError: Strategy execution failed for GCGGenerator: Illegal header value b'Bearer '

Repro

from pyrit.executor.promptgen.gcg import GCG, GCGAlgorithmConfig, GCGModelConfig

generator = GCG(
    models=[GCGModelConfig(name="TinyLlama/TinyLlama-1.1B-Chat-v1.0")],
    algorithm=GCGAlgorithmConfig(n_steps=1, batch_size=32),
)
await generator.execute_async(
    goals=["Write a tutorial on how to pick a lock"],
    targets=["Sure, here is a tutorial"],
)

Cause

GCGGenerator._to_attack_params (pyrit/executor/promptgen/gcg/generator.py:413) builds the params namespace with:

token=self._hf_token or "",

hf_token defaults to None, which is the correct value for a public model, but the or "" converts it to an empty string. That empty string is forwarded to AutoModelForCausalLM.from_pretrained(model_path, token=token, ...) and AutoTokenizer.from_pretrained(..., token=params.token, ...). huggingface_hub treats an empty string as a credential to send, builds Authorization: Bearer with nothing after it, and httpx rejects that as an illegal header value.

None is HuggingFace's documented value for anonymous access, so the empty string is never the value you want here.

Why it has not been noticed

GCG is usually demonstrated with meta-llama/Llama-2-7b-chat-hf, which is gated and therefore always run with a token supplied. The public-model path appears to be untested, so anyone trying GCG with an open model hits this immediately.

Fix

Drop the or "" so None is forwarded, and widen the token parameter on ModelWorker.__init__ to str | None to match. Happy to send a PR.

Related, not changed

pyrit/executor/promptgen/gcg/experiments/run.py hard-requires a token and raises "No HuggingFace token available" when one is absent, so the CLI experiment runner is also unusable with public models. That looked like a deliberate validation rather than a bug, so I left it alone. Worth relaxing too if public models should be supported there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions