protect: expose uploaded-file data (content/type) for rule-driven inspection - #115
Merged
Merged
Conversation
|
Implements robust file-upload inspection with content signatures and type mismatch detection. 🎯 Quality: 100% Elite · 📦 Size: Medium 📈 This month: Your 52nd PR — above team average · Averaging Excellent |
The engine saw an upload's filename and the raw multipart blob, but not file
content as a first-class thing (file_contains was a no-op), so a polyglot — a
valid image with a malicious payload inside (the ImageMagick "PNG that isn't a
PNG" RCE class) — passed a filename rule.
Expose the DATA, keep the detection in rules. The multipart parser now captures
each file part as { filename, type, content }, and the resolver exposes:
- files.<name>.content the file bytes (contains/regex signature rules)
- files.<name>.type the part's declared content-type
- files.<name>.filename the filename (same as bare files.<name>)
Bare files.<name> still returns the filename, so existing filename rules are
unchanged. No detection heuristics live in the engine — "what's malicious"
(webshell signatures, declared-type-vs-content mismatch) is composed in rules
(see the triage-vpatch-npm upload template): e.g. mismatch = `files.f.type`
matches ^image/ AND `files.f.content` head is markup (`^\s*<`), two inclusive
conditions — which also avoids the false positive of <script> in an image's
EXIF/XMP metadata. Content rides inside the already-capped body.
Adds tests/protect/upload-inspection.test.ts; updates multipart shape assertions.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
patchstackdave
force-pushed
the
protect/upload-content-inspection
branch
from
August 13, 2026 07:57
26f7b38 to
b65137c
Compare
Contributor
Author
|
/review |
daniloradovic
approved these changes
Aug 13, 2026
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.
What
The engine could see an upload's filename and the raw multipart blob, but not file content as a first-class source (
file_containswas a no-op). So a polyglot — a valid image with a malicious payload inside (the ImageMagick "PNG that isn't a PNG" RCE class, webshell-in-image) — slipped past a filename rule.Expose the data, keep detection in rules. The multipart parser now captures each file part as
{ filename, type, content }, and the resolver exposes three data sources:files.<name>.content— the file's bytes, for acontains/regexsignature rule.files.<name>.type— the part's declared content-type.files.<name>.filename— the filename (same as barefiles.<name>).No detection heuristics live in the engine — what counts as malicious is composed in rules (SaaS-delivered, versionable), matching the "detection is fully rule-driven, nothing hardcoded" principle. For example a declared-type-vs-content mismatch is just two
inclusiveconditions:{ "parameter": "rules", "rules": [ { "parameter": "files.f.type", "match": { "type": "regex", "value": "/^image\\//i" }, "inclusive": true }, { "parameter": "files.f.content", "match": { "type": "regex", "value": "/^\\s*<[a-z!?]/i" }, "inclusive": true } ] }That
^\s*<head anchor + the type gate catch svg-as-png / php-as-png with very low false-positive — and deliberately do not match a bare<script>in a real image's EXIF/XMP metadata (a genuine binary image head never starts with<). Content signatures (webshell / ImageMagick MSL) are a second OR'd condition. Full template ships in thetriage-vpatch-npmskill.Backward-compatible: bare
files.<name>still returns the filename.Notes / limits
^<head check rather than raw magic bytes.Tests
tests/protect/upload-inspection.test.tscovers content signatures, the rule-composed type mismatch (php-as-png / svg-as-png flagged; real image with<script>in metadata NOT flagged), the.type/.filenamesources, and filename backward-compat. Full suite green (651), typecheck clean.