Benchmarking Prompt Engineering Techniques for Secure Code Generation with GPT Models

Prompt engineering reduces reasoning mistakes in Large Language Models (LLMs). However, its effectiveness in mitigating vulnerabilities in LLM-generated code remains underexplored. To address this gap, we implemented a benchmark to automatically assess the impact of various prompt engineering strategies on code security. Our benchmark leverages two peer-reviewed prompt datasets and employs static scanners to evaluate code security at scale. We tested multiple prompt engineering techniques on GPT-3.5-turbo, GPT-4o, and GPT-4o-mini. Our results show that for GPT-4o and GPT-4o-mini, a security-focused prompt prefix can reduce the occurrence of security vulnerabilities by up to 56%. Additionally, all tested models demonstrated the ability to detect and repair between 41.9% and 68.7% of vulnerabilities in previously generated code when using iterative prompting techniques. Finally, we introduce a "prompt agent" that demonstrates how the most effective techniques can be applied in real-world development workflows.

Authors

Institutions

Publication Details

Published
2025-04-27
DOI
https://doi.org/10.1109/forge66646.2025.00018
Citations
7
Primary Topic
Security and Verification in Computing
Type
article
Field-Weighted Citation Impact
17.37
Controls
|||
ALL TIME
JAN
FEB
MAR
APR
MAY
JUN
JUL
AUG
SEP
OCT
article

Benchmarking Prompt Engineering Techniques for Secure Code Generation with GPT Models

Martin Kropp, Mohammad Ghafari, F. Gabrielli, Marc Bruni
7 citations
Security and Verification in Computing
17.37
article

Benchmarking Prompt Engineering Techniques for Secure Code Generation with GPT Models

Martin Kropp, Mohammad Ghafari, F. Gabrielli, Marc Bruni
article en
7 citations

Abstract

Prompt engineering reduces reasoning mistakes in Large Language Models (LLMs). However, its effectiveness in mitigating vulnerabilities in LLM-generated code remains underexplored. To address this gap, we implemented a benchmark to automatically assess the impact of various prompt engineering strategies on code security. Our benchmark leverages two peer-reviewed prompt datasets and employs static scanners to evaluate code security at scale. We tested multiple prompt engineering techniques on GPT-3.5-turbo, GPT-4o, and GPT-4o-mini. Our results show that for GPT-4o and GPT-4o-mini, a security-focused prompt prefix can reduce the occurrence of security vulnerabilities by up to 56%. Additionally, all tested models demonstrated the ability to detect and repair between 41.9% and 68.7% of vulnerabilities in previously generated code when using iterative prompting techniques. Finally, we introduce a "prompt agent" that demonstrates how the most effective techniques can be applied in real-world development workflows.

FHNW University of Applied Sciences and Arts Northwestern Switzerland (CH), Clausthal University of Technology (DE)
Industry, innovation and infrastructure
Openalex Percentile: Top 1%
Security and Verification in Computing
17.37
AI Navigator

Ask Laika to Summarize, Analyze, and Connect papers live on the map.

Summarize Papers & Methodologies

Extract key findings, datasets, and comparative methods across publications.

Benchmark Rankings & Visual Analytics

Rank top research institutions, authors, funders, topics, and journals by Field-Weighted Citation Impact (FWCI) and paper volume with instant charts.

Connect Distant Disciplines

Bridge topological clusters on the map to find hidden collaborative intersections.

Benchmarking Prompt Engineering Techniques for Secure Code Generation with GPT Models — Martin Kropp, Mohammad Ghafari, et al. · (2025) | TGRS Research Map | TGRS