Robust Detection of XSS and SQL Injection in JSON-API Traffic via JSON-Aware Canonicalization and Few-Shot Adversarial Fine-Tuning
Deep-learning detectors for web attacks perform strongly on clean data yet remain brittle to semantics-preserving obfuscations, especially in JSON centric API traffic where identical malicious intent can appear under varied encodings, Unicode confusables, whitespace/comment patterns, or JSON carrier forms. We present a deployment-minded pipeline that integrates: (i) JSON-aware canonicalization that standardizes benign representational variance while preserving attack-relevant semantics; (ii) multi-seed training of BiLSTM-Attention and CharCNN detectors; (iii) black-box robustness evaluation using a bounded reinforcement-learning (RL) adversary and per-mutator Attack Evasion Rate (AER); (iv) few-shot adversarial fine-tuning (FSAT) on curated hard cases; and (v) temperature scaling with per-class thresholds, together with cloud-style latency profiling. On a three-class corpus stratified into Train/Validation/Test = 173,872 / 37,258 / 37,259 and augmented with JSON-native payloads, an RL set (6,821 train / 2,882 holdout), and a few-shot set (n = 1,499; 48.3% JSON), the BiLSTM-Attention model with canonicalization achieves clean-test macro-F1 = 0.9879 ± 0.0007, precision = 0.9889 ± 0.0007, recall = 0.9871 ± 0.0007, and accuracy = 0.9875 ± 0.0007 across three seeds. For the selected robust configuration, per-class threshold calibration (0.4 / 0.6 / 0.6) yields validation macro-F1 = 0.9881. Canonicalization reduces AER across common mutator families, with the largest reductions on JSON wrapping and on multi-step chains such as homoglyph ? URL-encode ? whitespace. After FSAT, the calibrated canon_on model attains clean-test precision = 0.9891, recall = 0.9870, macro-F1 = 0.9880, and accuracy = 0.9877, while reaching RL-holdout precision = 1.0000, recall = 1.0000, F1 = 1.0000, and accuracy = 1.0000 against the evaluated bounded black-box RL attacker. Median latency at 8 KB is 79.6 ms (P95 95.8 ms), indicating practical feasibility for API-gateway deployment.