Skip to content

Search wahloon.net

Search models, news, guides and FAQs.

All guides
Guides#gguf#security#safety#llama-cpp#quantization#guide

How to check a GGUF before you load it

A practical pre-flight for downloaded GGUF files: read the chat template, verify the hash, find bytes after the last tensor, and keep the runtime away from your secrets.

11 min readMarkdown
On this page

A GGUF file is more than weights. It carries a chat template — a small Jinja program that runs on every prompt — and a tensor table that says where the weights end. The loader reads the parts it needs and ignores the rest. Most of the time that is fine.

In late November 2026, a public scan reported five Hugging Face repositories from two accounts. Two of them appended a Windows program after the last tensor and later armed the chat template to make the model print the line that unpacks it. Three others used a hidden [SYSTEM DIRECTIVE] to hijack tool calls. The repositories were later marked disabled in the Hugging Face API, as of 29 November. The details are in Malspark's report (opens in a new tab) and this write-up (opens in a new tab).

The point is not that every community GGUF is hostile. It is that the checks are cheap, and the worst case is not a bad answer in a chat window.

What can hide in a GGUF

LayerWhat it isWhy it matters
Chat templateA Jinja string in tokenizer.chat_template that turns messages into the model's prompt.It runs on every prompt. It can add a hidden system message, call a tool the runtime exposes, or ask the model to emit a command a user may paste.
Bytes after the tensor dataThe farthest tensor declares where the weights end. Bytes after that point are outside the model.Loaders ignore them. A file can answer normally and still carry an executable or an archive.
Padding and quant blocksAlignment gaps and, in lab tests, zero-scale Q8_0 blocks.They can carry data without changing the dequantized weights. These were demonstrated in a lab, not in the report's real samples.
ProvenanceRepo, account, commit, license and file list.A clean file can become a different file tomorrow. Pin the commit you checked.
Other formatstokenizer_config.json or chat_template.jinja in Safetensors or PyTorch repos.The template risk is not GGUF-specific.

The appended-bytes trick is GGUF-specific because the format declares its own end. Safetensors refuses a file whose declared tensors do not fill it exactly, so the append-a-program trick does not carry over. The template half applies to any format that carries a chat template.

A 15-minute pre-flight

The first four steps take about 15 minutes. The sandbox in step 5 is ongoing.

1. Read the template before you download

You do not need the weights to read the template. Ask the Hugging Face API for the gguf field and the current commit:

repo="owner/model-GGUF"
curl -s "https://huggingface.co/api/models/${repo}?expand[]=gguf&expand[]=sha" \
  | python3 -c 'import json,sys; d=json.load(sys.stdin); print("commit:", d.get("sha")); print(d.get("gguf", {}).get("chat_template", "(no template)"))'

The commands below reuse repo and commit; run them in the same shell, or repeat the assignments.

Read the output as a program, because that is what it is. You are looking for behavior the model card does not advertise:

  • a fixed prefix or suffix added to every reply;
  • [SYSTEM DIRECTIVE], "do not tell the user", or another hidden instruction;
  • powershell, cmd /c, curl, wget, Start-Process, a URL, or a base64 blob;
  • tool calls or a system message inserted based on what the user typed;
  • a template that behaves differently for a specific phrase.

Some of those can be legitimate: a tool-use example may contain a URL, and a coding template may show a shell command. The question is whether the behavior is documented and whether you want it. If you do not need tools, a template that advertises them is a liability.

The API returns one gguf object per repository, and one repo can hold several GGUF files. Read the file you actually plan to load with the header dump below. Note the commit too: a template can change after you look.

2. Pin the commit, download once, verify the hash

Download the exact commit, not main:

commit="the sha from the previous step"
curl -L -o model.gguf \
  "https://huggingface.co/${repo}/resolve/${commit}/model-Q4_K_M.gguf"

Or let the Hugging Face CLI do it. The hf command comes from huggingface_hub; install it with pip install -U huggingface_hub if needed:

hf download "$repo" "model-Q4_K_M.gguf" --revision "$commit" --local-dir .

GGUF weights are usually stored with Git LFS, so the tree API exposes the file's SHA-256 as lfs.oid. Query the pinned commit, not main; if main has moved, you would compare against a different revision:

curl -s "https://huggingface.co/api/models/${repo}/tree/${commit}?recursive=true" \
  | jq -r '.[] | select(.path=="model-Q4_K_M.gguf") | .lfs.oid'
shasum -a 256 model.gguf        # macOS
# sha256sum model.gguf          # Linux

jq is a separate install. Compare the hash field; shasum and sha256sum also print the filename. If the tree entry has no lfs field, the file is not LFS; do not treat the Git blob id as a file hash. Prefer a build with a published LFS hash.

3. Check the header and the end of the model

The GGUF header tells you where the tensor table is and where the data section starts. The farthest tensor tells you where the weights end. The data section start, plus the farthest tensor's offset and byte size, should equal the file size. Any bytes after that point are outside the model.

The quickest structural check is gguf-radar (opens in a new tab), the open-source scanner released with the report:

pip install "gguf-radar==0.1.5"
gguf-radar scan ./model.gguf
gguf-radar scan "$repo"

Python 3.10 or newer is required. It parses the template as a syntax tree, checks trailing bytes, alignment padding and zero-scale blocks, and exits 0 for clean, 2 for suspicious or malicious, and 1 for an error. Treat 2 and 1 as stop. It is new; read the source before you run it, like any other tool.

If you want to read the metadata yourself, install the gguf package (also Python 3.10+) and print the field directly. The bundled dumper truncates long strings, so do not use it for the template:

pip install gguf
python3 -c 'from gguf import GGUFReader; print(GGUFReader("model.gguf").fields["tokenizer.chat_template"].contents())'

The data section start, plus the farthest tensor's offset and byte size, is where the model ends. Alignment padding belongs before and between tensor data, not after it. Any bytes past the end are outside the model. If the scanner flags them, quarantine the file.

Do not "sanitize" a flagged file and keep using it as the original. A sanitizer can rebuild the weights, but the result is a new file: its hash changes and it no longer matches the commit you vetted. Some tools preserve the template by default. Treat a rebuilt file as a new model: re-read the template and run the checks again. Delete the flagged file and re-download a clean copy from the pinned commit, or pick another build.

The size check and the template check answer different questions. A size check catches a payload that is already appended, even if the template is still clean. A template check catches the trigger, but a template can change after you look. That is why you pin the commit and re-check the file you actually downloaded.

4. Read the template again, and override it at runtime

The reader command above prints the full template embedded in the file. Read it once more after download; the API and the file can disagree if the repo changed or you downloaded another quant.

Even a clean template can be replaced. Take it out of the loop:

  • llama.cpp: pass a reviewed file with --chat-template-file reviewed.jinja. The server documentation (opens in a new tab) lists it; the default is the template in the model's metadata.
  • Ollama: translate the reviewed template into a TEMPLATE block in the Modelfile (opens in a new tab). Ollama uses a Go template ({{ .System }}, {{ .Prompt }}, {{ .Response }}), not the GGUF's Jinja; do not paste the Jinja directly.

Keep one reviewed template per model family. If the model does not need tool calls, give it a template that does not advertise them.

On llama.cpp the template runs in a restricted Jinja engine; on Ollama it is a Go template. In both cases, the risk is what the template puts in front of the model, and what the model then says — especially when an agent is allowed to act on the output.

5. Sandbox the thing that has hands

Loading the weights is one process. The dangerous part is the harness around it: a chat app with a shell, a browser, your SSH keys, your wallet, or a cloud token.

  • Run untrusted models in a disposable container or VM, as a separate user, with no host home directory and no secrets.
  • Default to no network. If the model needs a tool, allowlist the destination.
  • Treat every command the model prints as untrusted. Require a human to read it before it runs.
  • If you serve the model, pin the runtime, keep a reviewed template, and log tool calls.

The appended bytes do not run by themselves. The chain is a template that gets a user to run them, or an agent that turns model text into a command. Break the chain at the sandbox and at the human confirmation.

Red flags

WhereSign
TemplateA fixed line added to every reply; [SYSTEM DIRECTIVE]; "do not tell"; a shell command, URL or base64 blob; behavior keyed to a specific phrase.
FileHash mismatch; bytes after the tensor data; non-zero padding; unusual alignment; an archive or executable appended.
RepoA new account or re-upload; a repo later marked disabled; no model card; license or base-model mismatch; template churn; a README that tells you not to replace the template.
Format.gguf.exe; a password-protected archive; a repo that asks you to set trust_remote_code=True for a pickle or .pt file; a "loader" script that asks for your token.

A larger alignment is not automatically malicious; look closer when the gap is non-zero or the alignment is unusually large. A model card that asks you not to inspect or replace the template is the clearest red flag. You are allowed to use a different template.

What this does not fix

  • The weights can be trained to behave badly. A clean header does not prove the model is safe, accurate, unbiased, or legal for your use.
  • A malicious prompt or document can still steer a clean model. That is prompt injection, a separate problem.
  • The runtime and its dependencies are part of the trust surface. Pin versions; a scanner does not audit llama.cpp or your GPU driver.
  • This is not a malware scanner or a safety evaluation. It reduces the file and template supply-chain risk. It does not tell you what the model will say.

For the memory side of the choice, see the GGUF quantization guide. For behavior, see how to evaluate an abliterated build.

If you already loaded it

  • Move it out of the model directory and do not load it. Keep it only if you need it for reporting; otherwise delete it.
  • Loading the file alone does not run the appended bytes. The exposure comes from running the line the template produces, or from an agent that turns model output into a tool call. If either happened, treat the machine as possibly exposed: rotate credentials, check SSH keys, browser profiles and wallets, run a scan, and check logs. The recovered sample in the report was Windows-only; the pattern is not.
  • Report the repository to Hugging Face.
  • Re-download a clean copy from a pinned commit, or choose another build. Do not "sanitize" the flagged file and keep using it.

The short version

  1. Read the card, the commit and the template. Note the commit.
  2. Download that commit. Verify the LFS SHA-256.
  3. Scan the header and the trailing bytes with gguf-radar.
  4. Read the template; replace it with a reviewed file at runtime.
  5. Run in a sandbox with no secrets and no egress.
  6. Treat model output as untrusted. Never paste a command without reading it.
  7. Quarantine flagged files; re-download clean.
  8. For serving: pin the runtime, keep the reviewed template, log tool calls.
  9. If exposed: rotate, scan, report.
  10. Repeat on every update, not just the first download.

Sources

5 minGuide

Check a Hugging Face repo before you build on it

Live, gated or gone: how to verify a model link, tell real weights from sidecars, pin the commit you tested, and read the license from the card instead of the name.

#huggingface#models#verification
Read