agentleFS
kgrizz-gitkgrizz-git/AgentsConfigHelper/.cursor/rules/codeguard-0-file-handling-and-uploads.mdc

AgentsConfigHelper / rules

Cursor rule2 starsChanged 3 months agoMITView on GitHub

Secure file handling & uploads (validation, storage isolation, scanning, safe delivery)

We split this codeguard-0-file-handling-and-uploads.mdc into the rules it gives an agent

15 rules. About 0 are common, 14 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈14

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Ensure input validation is applied before validating extensions.

    line 15
  2. Very specific:

    Avoid double extensions (e.g., .jpg.php) and null byte injection (e.g., .php%00.jpg).

    line 16
  3. Distinctive:

    Use allowlist approach rather than denylist for file extensions.

    line 17
  4. Distinctive:

    Never trust client-supplied Content-Type headers as they can be spoofed.

    line 21
  5. Distinctive:

    Use file signature validation but not as a standalone security measure.

    line 24
  6. Distinctive:

    If user filenames required, implement maximum length limits.

    line 28
  7. Distinctive:

    Avoid leading hyphens or spaces for safer shell script processing.

    line 31
  8. Distinctive:

    Avoid ZIP files due to numerous attack vectors.

    line 36
  9. Distinctive:

    If storing in webroot, set write-only permissions with proper access controls.

    line 43
  10. Distinctive:

    Use application handlers that map IDs to filenames for public access.

    line 44
  11. Distinctive:

    Consider database storage for specific use cases with DBA expertise.

    line 45
  12. Distinctive:

    Consider post-decompression size limits for compressed files.

    line 55
  13. Distinctive:

    Use secure methods to calculate ZIP file sizes safely.

    line 57
  14. Distinctive:

    Keep all file processing libraries securely configured and updated.

    line 61
  15. Distinctive:

    Use secure extraction methods for compressed files.

    line 64

≈ “Common” is an estimate: at least 1 in 5 of the 6,350 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
codeguard-0-file-handling-and-uploads.mdc
---
description: Secure file handling & uploads (validation, storage isolation, scanning, safe delivery)
globs: **/*.c,**/*.go,**/*.h,**/*.java,**/*.js,**/*.jsx,**/*.mjs,**/*.php,**/*.py,**/*.pyi,**/*.pyx,**/*.rb,**/*.ts,**/*.tsx
version: 1.0.1
---

rule_id: codeguard-0-file-handling-and-uploads

## File Upload Security Guidelines

This rule advises on secure file upload practices to prevent malicious file attacks and protect system integrity:

- Extension Validation
  - List allowed extensions only for business-critical functionality.
  - Ensure input validation is applied before validating extensions.
  - Avoid double extensions (e.g., `.jpg.php`) and null byte injection (e.g., `.php%00.jpg`).
  - Use allowlist approach rather than denylist for file extensions.
  - Validate extensions after decoding filename to prevent bypass attempts.

- Content Type and File Signature Validation
  - Never trust client-supplied Content-Type headers as they can be spoofed.
  - Validate file signatures (magic numbers) in conjunction with Content-Type checking.
  - Implement allowlist approach for MIME types as a quick protection layer.
  - Use file signature validation but not as a standalone security measure.

- Filename Security
  - Generate random filenames (UUID/GUID) instead of using user-supplied names.
  - If user filenames required, implement maximum length limits.
  - Restrict characters to alphanumeric, hyphens, spaces, and periods only.
  - Prevent leading periods (hidden files) and sequential periods (directory traversal).
  - Avoid leading hyphens or spaces for safer shell script processing.

- File Content Validation
  - For images, apply image rewriting techniques to destroy malicious content.
  - For Microsoft documents, use Apache POI for validation.
  - Avoid ZIP files due to numerous attack vectors.
  - Implement manual file review in sandboxed environments when resources allow.
  - Integrate antivirus scanning and Content Disarm & Reconstruct (CDR) for applicable file types.

- Storage Security
  - Store files on different servers for complete segregation when possible.
  - Store files outside webroot with administrative access only.
  - If storing in webroot, set write-only permissions with proper access controls.
  - Use application handlers that map IDs to filenames for public access.
  - Consider database storage for specific use cases with DBA expertise.

- Access Control and Authentication
  - Require user authentication before allowing file uploads.
  - Implement proper authorization levels for file access and modification.
  - Set filesystem permissions on principle of least privilege.
  - Scan files before execution if execution permission is required.

- Upload and Download Limits
  - Set proper file size limits for upload protection.
  - Consider post-decompression size limits for compressed files.
  - Implement request limits for download services to prevent DoS attacks.
  - Use secure methods to calculate ZIP file sizes safely.

- Additional Security Measures
  - Protect file upload endpoints from CSRF attacks.
  - Keep all file processing libraries securely configured and updated.
  - Implement logging and monitoring for upload activities.
  - Provide user reporting mechanisms for illegal content.
  - Use secure extraction methods for compressed files.

Summary:
Implement defense-in-depth for file uploads through multi-layered validation, secure storage practices, proper access controls, and comprehensive monitoring. Never rely on single validation methods and always generate safe filenames to prevent attacks.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.