NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

VISION: Robust and Interpretable Code Vulnerability Detection Leveraging Counterfactual Augmentation

David Egea, Barproda Halder, Sanghamitra Dutta

Abstract

Automated detection of vulnerabilities in source code is an essential cybersecurity challenge, underpinning trust in digital systems and services. Graph Neural Networks (GNNs) have emerged as a promising approach as they can learn the structural and logical code relationships in a data-driven manner. However, the performance of GNNs is severely limited by training data imbalances and label noise. GNNs can often learn “spurious” correlations due to superficial code similarities in the training data, leading to detectors that do not generalize well to unseen real-world data. In this work, we propose a new unified framework for robust and interpretable vulnerability detection—that we call VISION—to mitigate spurious correlations by systematically augmenting a counterfactual training dataset. Counterfactuals are samples with minimal semantic modifications that have opposite prediction labels. Our complete framework includes: (i) generating effective counterfactuals by prompting a Large Language Model (LLM); (ii) targeted GNN model training on synthetically paired code examples with opposite labels; and (iii) graph-based interpretability to identify the truly crucial code statements relevant for vulnerability predictions while ignoring the spurious ones. We find that our framework reduces spurious learning and enables more robust and generalizable vulnerability detection, as demonstrated by improvements in overall accuracy (from 51.8% to 97.8%), pairwise contrast accuracy (from 4.5% to 95.8%), and worst-group accuracy increasing (from 0.7% to 85.5%) on the widely popular Common Weakness Enumeration (CWE)-20 vulnerability. We also demonstrate improvements using our proposed metrics, namely, intra-class attribution variance, inter-class attribution distance, and node score dependency. We provide a new benchmark for vulnerability detection, CWE-20-CFA, comprising 27,556 samples from functions affected by the high-impact and frequently occurring CWE-20 vulnerability, including both real and counterfactual examples. Furthermore, our approach enhances societal objectives of transparent and trustworthy AI-based cybersecurity systems through interactive visualization for human-in-the-loop analysis.

Introduction: The "Clever Hans" Problem in AI Code Security

Deep learning models, particularly Graph Neural Networks (GNNs), are highly effective at identifying patterns in source code. However, they often suffer from the “Clever Hans” effect: they learn spurious correlations rather than actual logic. For example, if a training dataset contains many vulnerable functions that happen to use a specific variable name, a GNN might learn to flag any code containing that name as "vulnerable," regardless of whether a real security flaw exists. This happens because security datasets are often imbalanced or contain superficial patterns that correlate with labels but do not cause them. When these models encounter real-world code that deviates from these patterns, they often fail to generalize.

The VISION Framework

To address these limitations, we propose VISION (Vulnerability Identification and Spuriousness mitigation via counterfactual augmentatION), a unified framework for robust and interpretable vulnerability detection.

The complete VISION framework pipeline, illustrating dataset generation, data processing, and the prediction/explanation stages.

The VISION pipeline consists of four main stages:

  1. Counterfactual Generation: Using an LLM (GPT-4o-mini), the system takes a code snippet and generates a "counterfactual" version. If the original is benign, the LLM introduces a minimal change to make it vulnerable (e.g., removing a boundary check). If it's vulnerable, the LLM fixes it.
  2. Graph Representation: Code is converted into Code Property Graphs (CPGs) using the Joern tool, representing syntax, control flow, and data dependencies as a complex network.
  3. Robust Training: A GNN (based on the Devign architecture) is trained on these "mirrored" pairs. Because the two versions are nearly identical except for the vulnerability, the model is forced to focus on actual semantic differences rather than superficial features.
  4. Interpretable Explanation: The framework integrates the Illuminati explainer and a custom visualization module, allowing humans to see exactly which lines of code the GNN "blamed" for its decision.

Counterfactual Reasoning and Interpretability

The core innovation of VISION lies in its use of counterfactual pairs to "de-bias" the model. By training on pairs that differ by only one or two lines, the model learns that those specific lines are the "causal" reason for the vulnerability.

Comparison of benign and vulnerable counterfactual code pairs with model-generated explanations highlighting causal code statements.

This approach also enhances interpretability. The researchers introduced a "Node Score Dependency" metric to prove that VISION-trained models ignore "spurious" artifacts (like specific error codes) and focus on critical logic (like array indexing). The interactive interface allows developers to verify these decisions in real-time.

Key Results and Impact

The study focuses on CWE-20 (Improper Input Validation), a high-impact vulnerability class. Using the new CWE-20-CFA benchmark (27,556 samples), the framework demonstrated massive improvements over standard training:

  • Overall Accuracy: Jumped from 51.8% to 97.8%.
  • Pairwise Contrast Accuracy: Increased from 4.5% to 95.8%, proving the model can distinguish between an original snippet and its counterfactual.
  • Worst-Group Accuracy (WGA): Increased from 0.7% to 85.5%, demonstrating robustness on difficult data subsets.
  • Recall: Rose to 99.7%, meaning the model almost never misses a vulnerability in the test set.

Conclusion

VISION represents a significant step toward transparent and trustworthy AI-based cybersecurity. By systematically mitigating spurious correlations through counterfactual augmentation, it enables more robust and generalizable vulnerability detection. Future work will expand the framework to a wider variety of vulnerability classes and explore the integration of formal verification to ensure the logical soundness of LLM-generated counterfactuals.

Summary updated

BibTeX
@inproceedings{egea2025vision,
  author = {David Egea and Barproda Halder and Sanghamitra Dutta},
  title = {{VISION: Robust and Interpretable Code Vulnerability Detection Leveraging Counterfactual Augmentation}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2025},
  doi = {10.1609/aies.v8i1.36592},
}
Figure