Enhancing Cross-Site Scripting (XSS) Attacks Detection through Modern Transformer Architecture Optimizations
- 1 School of Computational and Communication Science and Engineering, Nelson Mandela African Institution of Science and Technology (NM-AIST), Arusha, Tanzania
- 2 School of Computational and Communication Science and Engineering, Nelson Mandela African Institution of Science and Technology (NM-AIST), Arusha, Tanzania
- 3 School of Computational and Communication Science and Engineering, Nelson Mandela African Institution of Science and Technology (NM-AIST), Arusha, Tanzania
Abstract
Cross-Site Scripting (XSS) remains a widespread and damaging threats to web applications, as highlighted by the OWASP Top 10. While various detection methods exist, they often struggle to keep pace with the sophistication of attack vectors and obfuscation techniques. This paper implements an approach for enhancing XSS detection by leveraging modern optimizations within the transformer architecture. Our methodology uses a custom transformer encoder model trained on an aggregated dataset of nearly 100,000 samples, including newly collected XSS payloads. To enhance performance and efficiency, we integrate two key architectural improvements: Rotary Positional Embeddings (RoPE) to achieve a superior contextual understanding of HTTP payloads, and Flash Attention to significantly accelerate training and inference speeds while reducing memory consumption. Experimental results show good performance of our model that achieves an accuracy of 99.38 with high recall and precision. An ablation study demonstrates that the integrated optimizations improve detection accuracy by 0.11 percentage points, while reducing training time by approximately 32% and peak GPU memory usage by approximately 30% relative to standard Transformer configurations. Comparative evaluation against a Random Forest baseline further reveals a clear contextual understanding advantage over traditional frequency-based approaches, justifying the architectural complexity. The proposed model effectively captures structural patterns in sophisticated payloads that typically evade classical methods. This work presents an efficient, and accurate solution in real-time XSS threat detection.
- OWASP (2025) Introduction-OWASP Top 10. https://owasp.org/Top10/2025/0x00_2025-Introduction
- Stency, V.S. and Mohanasundaram, N. (2021) A Study on XSS Attacks: Intelligent Detection Methods. Journal of Physics : Conference Series , 1767, Article ID: 012047. https://doi.org/10.1088/1742-6596/1767/1/012047
- Fang, Y., Li, Y., Liu, L. and Huang, C. (2018) DeepXSS: Cross Site Scripting Detection Based on Deep Learning. Proceedings of the 2018 International Conference on Computing and Artificial Intelligence , Chengdu, 12-14 March 2018, 47-51. https://doi.org/10.1145/3194452.3194469
- Yan, H., Feng, L., Yu, Y., Liao, W., Feng, L., Zhang, J., et al . (2022) Cross-Site Scripting Attack Detection Based on a Modified Convolution Neural Network. Frontiers in Computational Neuroscience , 16, Article 981739. https://doi.org/10.3389/fncom.2022.981739
- Kascheev, S. and Olenchikova, T. (2020) The Detecting Cross-Site Scripting (XSS) Using Machine Learning Methods. 2020 Global Smart Industry Conference ( GloSIC ), Chelyabinsk, 17-19 November 2020, 265-270. https://doi.org/10.1109/glosic50886.2020.9267866
- Yang, W., Zuo, W. and Cui, B. (2019) Detecting Malicious URLs via a Keyword-Based Convolutional Gated-Recurrent-Unit Neural Network. IEEE Access , 7, 29891-29900. https://doi.org/10.1109/access.2019.2895751
- Zhou, Y. and Wang, P. (2019) An Ensemble Learning Approach for XSS Attack Detection with Domain Knowledge and Threat Intelligence. Computers & Security , 82, 261-269. https://doi.org/10.1016/j.cose.2018.12.016
- Kaggle (2020) Cross Site Scripting XSS Dataset for Deep Learning. https://www.kaggle.com/datasets/syedsaqlainhussain/cross-site-scripting-xss-dataset-for-deep-learning
- OWASP (2025) Cross Site Scripting Prevention Cheat Sheet. OWASP Cheat Sheet Series. https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.md
- XSSED (2015) XSSed | Cross Site Scripting (XSS) Attacks Information and Archive. http://www.xssed.com
- Huyen, C. (2022) Designing Machine Learning Systems. O’Reilly Media, Inc.
- PortSwigger (2024) Input Data Encoding and Obfuscation. https://portswigger.net/web-security/essential-skills/obfuscating-attacks-using-encodings
- Sennrich, R., Haddow, B. and Birch, A. (2016) Neural Machine Translation of Rare Words with Subword Units. Proceedings of the 54 th Annual Meeting of the Association for Computational Linguistics ( Volume 1: Long Papers ), Berlin, 7-12 August 2016, 1715-1725. https://doi.org/10.18653/v1/p16-1162