BSM: A Browser-Resident Framework for Real-Time Detection of JavaScript API Abuse and Prompt Injection Attacks
Abstract
As web applications embed both dynamic JavaScript and AI services, they expose browser APIs to two runtime threats that are rarely addressed together: JavaScript code injection and prompt injection. This paper presents Browser Security Monitor (BSM), a browser-resident framework that instruments sensitive window object APIs to intercept and analyze dynamic JavaScript execution in real time. BSM integrates weighted behavioral pattern matching using Deterministic Finite Automata (DFA) with empirically derived risk scores and a lightweight keyword and linguistic feature filter for direct prompt injection. We evaluate BSM across production deployment, static malware analysis, and prompt injection detection. Field deployment across 47 production websites monitored 50,732 API events (6,559 fetch and 44,173 eval() invocations) with zero false positives. Static evaluation on 1,109 samples (1,061 malicious from the Petrak collection and 48 benign production libraries) reports 98.4% precision, 59.6% recall, and 79.2% specificity at the browser-deployment threshold $T=40$ , with an AUROC of 0.60 reflecting a discrete weighted scoring design that trades ranking quality for sub-millisecond latency; a Random Forest baseline on the same features reaches AUC-ROC 0.98 but a 52.0% false-positive rate unusable for browser deployment. Applied to the static evaluation, a SHA-256 content-hash access list over canonical CDN library versions raises specificity to 100.0% with no measurable change in malicious-sample recall. On 1,695 labeled prompts, the prompt injection module yields 45.9% recall, 92.5% precision, 96.8% specificity, and F1 = 0.614, while detecting 0 of 20 paraphrased attacks, an explicit keyword evasion boundary. An adversarial robustness evaluation across five transformation families shows 90% evasion once the literal eval() token is stripped, quantifying the rule-based detection ceiling. BSM does not address indirect prompt injection, binary-payload malware, or polymorphic JavaScript outside the nine-pattern set. Detection latency stays within 0.3 to 0.8 ms and memory within 25 to 35 MB. BSM’s contribution is integrative rather than algorithmic: no individual technique is novel in isolation, and the value lies in combining JavaScript API-abuse detection and direct prompt injection detection in a single browser-resident, sub-millisecond, low-memory extension validated on 47 live production sites. BSM is a precision-first, low-overhead client-side filter that complements rather than replaces server-side and model-internal defenses.