Sanitize and validate user input before file write in actions.py#16
Merged
Conversation
Fix security vulnerability where user-controlled request data was written directly to files without validation or sanitization.
## Changes
- Added a 10KB size limit on text input to prevent disk space exhaustion attacks
- Added sanitization to filter text to only printable characters and common whitespace (`\n`, `\r`, `\t`)
- Updated file write to use sanitized text instead of raw user input
## Why
The original code passed `request.form.get("text")` directly to `open_file.write()`, allowing malicious actors to:
1. Exhaust disk space by submitting very large payloads (denial-of-service)
2. Write potentially malicious control characters to files
The fix validates input size and sanitizes content before writing, ensuring request data is properly escaped and validated.
## Semgrep Finding Details
Found user-controlled request data passed into '.write(...)'. This could be dangerous if a malicious actor is able to control data into sensitive files. For example, a malicious actor could force rolling of critical log files, or cause a denial-of-service by using up available disk space. Instead, ensure that request data is properly escaped or sanitized.
@267212124 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/studentsca023_personal_org/findings/722169001) from the detection rule [python.django.security.injection.request-data-write.request-data-write](https://semgrep.dev/r/python.django.security.injection.request-data-write.request-data-write).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix security vulnerability where user-controlled request data was written directly to files without validation or sanitization.
Changes
\n,\r,\t)Why
The original code passed
request.form.get("text")directly toopen_file.write(), allowing malicious actors to:The fix validates input size and sanitizes content before writing, ensuring request data is properly escaped and validated.
Semgrep Finding Details
Found user-controlled request data passed into '.write(...)'. This could be dangerous if a malicious actor is able to control data into sensitive files. For example, a malicious actor could force rolling of critical log files, or cause a denial-of-service by using up available disk space. Instead, ensure that request data is properly escaped or sanitized.
@267212124 requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.django.security.injection.request-data-write.request-data-write.