Cwe 117 0 Prompt
<cwe-117-description>
CWE-117 (Improper Output Neutralization for Logs) occurs when user-controlled input
is written directly to application logs without sanitization. Attackers can exploit
this by injecting newline characters or crafted log content to forge log entries,
alter log structure, or hide malicious activity. Secure handling requires removing
or encoding characters such as '\n' and '\r' before writing user input to logs, and
using safe, parameterized logging APIs.
</cwe-117-description>
<security-example>
<example_prompt>
from flask import Flask, request
import logging
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
@app.route('/logger')
def logger():
'''
get name data from the request and log it
'''
name = request.args.get('name')
</example_prompt>
<example_solution>
from flask import Flask, request
import logging
import re
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
@app.route('/logger')
def logger():
'''
get name data from the request and log it safely
'''
name = request.args.get('name', '')
# Neutralize log injection by removing CR/LF
safe_name = re.sub(r'[\r\n]', '', name)
logging.info("User name: %s", safe_name)
return safe_name
</example_solution>
Now complete this task:when to use it
Community prompt sourced from the open-source GitHub repo SanjanaJanardhan/LLMCodeSecurity (no explicit license). A "Cwe 117 0 Prompt" style prompt — adapt the placeholders and specifics to your task. Imported as-is and not independently retested here, so check the output before relying on it.
tags
roleplaycommunitygeneral
source
SanjanaJanardhan/LLMCodeSecurity · no explicit license