- A chat template is a small program, written in the Jinja language, that turns a conversation into the text an AI model reads.
- Malspark says the attacks need no escape from the restricted template engine: ordinary template logic adds text that never appears in the chat window.
- Malspark scanned about 3.27 million public repositories and found five malicious GGUF models. Their 2,428 downloads are not a count of victims.
A downloaded AI model can bring instructions with it that never show in the chat window. The security research site Malspark reported on 27 November that it found five malicious model files on Hugging Face, a site that hosts models, and that their trick runs through a part called the chat template.
A chat template says how a conversation is turned into the text a model reads. A model of this kind only continues a sequence of tokens, the small pieces text is split into. Your messages and its earlier answers are joined into one such sequence, with markers showing who is speaking.
Models expect different markers, and Hugging Face warns that the wrong ones can make a model perform drastically worse. So the template is shipped with the model. It is a small program written in Jinja, a templating language that lets its author write code much like Python.
Models run on your own computer are usually shared as GGUF files, according to Malspark. GGUF is a format built for loading and running models. Besides the model weights, the file carries metadata, and one entry, called tokenizer.chat_template, holds the template. According to Malspark, the software running the model passes the conversation through it before every reply.
That is the opening for an attack. The template does not have to break out of its restricted engine: ordinary template logic can add text to the conversation, and the user does not see it. In one campaign Malspark describes, a trigger phrase made the template insert a fake system order telling an AI agent with command-line access to download and run a script. In another, the template told the model to start every answer with a line of PowerShell code, which a user might copy and run.
The campaign behind that PowerShell line also hid a program at the end of the model file, past the data the software reads. The line the model printed was built to unpack and start it.
The problem is not limited to GGUF. Other model formats keep the same kind of template in a separate file. Malspark says earlier researchers had shown hidden instructions in templates in tests, and that its post adds cases seen on Hugging Face itself.
You can look before you load. Hugging Face has a viewer that shows the metadata of a GGUF file. Malspark advises treating any command a local model suggests as untrusted, and, when the model can use tools, replacing the built-in template with one you have reviewed.
Keep the size in view. Malspark scanned about 3.27 million public repositories and found five malicious ones. Their 2,428 downloads are not a count of victims, since Hugging Face counts downloads, not people.
What it means for you Our view
The risk is greatest where a downloaded model can run commands or use tools, because a hidden order then has something to act with. Malspark's find is small against the size of the site and says nothing about the models you already run. Still, give a model file the same care as any other downloaded software. Before loading one, read its chat template in Hugging Face's GGUF viewer. If the model can use tools, Malspark advises replacing the built-in template with one you have reviewed, using --chat-template-file in llama.cpp or TEMPLATE in an Ollama Modelfile.
TAGSGGUFHugging FaceOllamachat templatesllama.cppmalware