Sparse Autoencoders as Plug-and-Play Firewalls for Adversarial Attack Detection in VLMs

Hao Wang, Yiqun Sun, Pengfei Wei, Lawrence B. Hsieh, Daisuke KawaharaarXiv 2026 · arXiv

TL;DR

SAEgis is a lightweight, plug-and-play firewall that detects adversarial attacks on Vision-Language Models (VLMs) without requiring any expensive adversarial training. By embedding Sparse Autoencoders (SAEs) into a pretrained VLM, the framework exploits the sparse latent space to naturally isolate and flag attack-relevant signals. It demonstrates exceptional generalization across unseen image domains and novel attack methods, providing a highly practical safeguard for real-world VLM deployments.

What Problem Does This Paper Address?

Vision-Language Models (VLMs) are rapidly expanding into real-world applications like autonomous agents and embodied AI. However, their visual inputs introduce severe security vulnerabilities. Attackers can craft adversarial images—adding imperceptible pixel perturbations—that completely hijack the VLM's behavior, forcing it to ignore actual visual semantics and output malicious or nonsensical text.

While several adversarial detection methods exist, they suffer from two major flaws: they are rarely tested against the latest, strongest attacks, and they fail to generalize. A defense trained to detect attacks on natural images often completely falls apart when deployed on a new domain (like medical imagery) or when faced with a novel attack algorithm. SAEgis addresses this gap by providing a detection mechanism that generalizes robustly without requiring a massive dataset of attacks to train on.

Methods

The core intuition behind SAEgis is that an SAE trained purely on clean data learns to efficiently represent normal visual features. When an adversarial image is processed, it deviates from these clean patterns and abnormally activates a specific subset of sparse latent features, which the authors term attack-relevant features.

Figure 1: Overview of SAEgis. An SAE is inserted into the VLM and trained with reconstruction. Top-k attack-relevant features are identified from a set of adversarial samples. At inference, inputs activating many such features are flagged as adversarial, while those with few are classified as clean.

The SAEgis pipeline operates in three main phases:

  1. SAE Insertion & Training: A standard SAE is inserted into a pretrained VLM (such as the vision encoder or the projection layer) and trained using a basic reconstruction objective on clean images.
  2. Feature Selection: Using a very small set of adversarial images, the framework calculates an "attack score" for each latent feature based on its peak activation strength and spatial frequency. The top-$K$ features with the highest scores are selected as the attack-relevant feature set.
  3. Adversarial Detection: At inference time, the model counts how many of these top-$K$ features fire. If the activation count exceeds a specific threshold $\tau$—calibrated on a clean development set to maintain a strict false positive rate (e.g., $\alpha = 0.02$)—the input is flagged as adversarial.

To maximize robustness, the authors introduce Multi-Layer SAE Ensembling. By averaging the activation counts from SAEs placed at different depths (e.g., early vision blocks and the projection layer), the firewall can catch both low-level pixel noise and high-level semantic manipulations.

Datasets

The authors evaluated their approach using three distinct image datasets to rigorously test cross-domain generalization:

  • NIPS17: A standard benchmark dataset of natural images used for non-targeted adversarial attacks.
  • LLaVA-Instruct-150K (LLaVA): A widely used dataset featuring diverse natural images paired with instructions.
  • Medical Multimodal Evaluation Data (Medical): A specialized dataset containing medical imagery, utilized specifically as a challenging out-of-domain evaluation target.

Key Insights and Ideas

  • Anomalies Stand Out in Sparse Spaces: Unlike dense hidden states where adversarial perturbations blend into high-dimensional vectors, the sparse latent features of an SAE act like discrete semantic switches. Attacks light up specific "alarm" features that rarely fire during clean inference.
  • Local vs. Global Complementarity: Early layers in the vision encoder are highly sensitive to local, high-frequency texture modifications (typical of pixel-space attacks). Deeper layers, like the projection layer, capture global semantic shifts. Ensembling them eliminates structural blind spots.
  • Inherent Feature Transferability: Venn diagram analyses revealed a massive overlap in the specific attack-relevant features activated across entirely different datasets and attack types. This explains why a firewall profiled on natural images remains highly effective when processing medical scans.

Key Results

SAEgis was evaluated against state-of-the-art attacks like M-Attack and FOA-Attack, outperforming both dense representation baselines and prior attention-based detection methods (like PIP):

  • In-Domain Perfection: In standard settings where training and testing occurred on the same dataset, the SAEgis Ensemble achieved near-perfect detection, hitting a 100% F1-score on NIPS17 against SSA-CWA attacks.
  • Superior Cross-Domain Generalization: When transferring from LLaVA to the Medical dataset under the FOA-Attack, the dense baseline's precision plummeted to 76%. Meanwhile, the SAEgis Ensemble maintained an exceptional 97.8% precision and 95.3% F1-score.
  • Data Efficiency: The framework is remarkably practical; using as few as 10 adversarial samples during the feature selection phase is enough to achieve an 80% F1-score in cross-domain detection.

Limitations and Future Work

Despite its strengths, the authors note a few limitations stemming from its clean-only calibration strategy:

  • Clean Distribution Shifts: Because the detection threshold $\tau$ is calibrated strictly on a clean development set, if the clean data at deployment shifts drastically from the calibration data, it can skew the threshold—leading to higher false positives or lower recall.
  • Cross-Attack Distribution Mixing: In rare cross-attack scenarios, the global feature shifts induced by a novel attack can cause the clean and adversarial feature activation distributions to overlap significantly, making clean separation difficult.

Future work will likely focus on developing adaptive thresholding techniques to dynamically adjust to clean distribution shifts in production environments.

Summary updated

Abstract

Vision-language models (VLMs) have advanced rapidly and are increasingly deployed in real-world applications, especially with the rise of agent-based systems. However, their safety has received relatively limited attention. Even the latest proprietary and open-weight VLMs remain highly vulnerable to adversarial attacks, leaving downstream applications exposed to significant risks. In this work, we propose a novel and lightweight adversarial attack detection framework based on sparse autoencoders (SAEs), termed SAEgis. By inserting an SAE module into a pretrained VLM and training it with standard reconstruction objectives, we find that the learned sparse latent features naturally capture attack-relevant signals. These features enable reliable classification of whether an input image has been adversarially perturbed, even for previously unseen samples. Extensive experiments show that SAEgis achieves strong performance across in-domain, cross-domain, and cross-attack settings, with particularly large improvements in cross-domain generalization compared to existing baselines. In addition, combining signals from multiple layers further improves robustness and stability. To the best of our knowledge, this is the first work to explore SAE as a plug-and-play mechanism for adversarial attack detection in VLMs. Our method requires no additional adversarial training, introduces minimal overhead, and provides a practical approach for improving the safety of real-world VLM systems.
BibTeX
@misc{wang2026sparse,
  author = {Hao Wang and Yiqun Sun and Pengfei Wei and Lawrence B. Hsieh and Daisuke Kawahara},
  title = {{Sparse Autoencoders as Plug-and-Play Firewalls for Adversarial Attack Detection in VLMs}},
  year = {2026},
  eprint = {2605.07447},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2605.07447},
}
Figure