# Bypassing File Upload Restrictions To Exploit Client-Side Path Traversal

DevFeed: [Bypassing File Upload Restrictions To Exploit Client-Side Path Traversal](<https://devfeed.tech/articles/bypassing-file-upload-restrictions-to-exploit-client-side-path-traversal-92057.md>)

Original publisher: [Read original article](<https://blog.doyensec.com/2025/01/09/cspt-file-upload.html>)

Published: 2025-01-08T23:00:00Z

Content type: tutorial

Language: en

Sources: [Doyensec](<https://devfeed.tech/sources/doyensec.md>)

Topics: [Data validation](<https://devfeed.tech/topics/data-validation.md>), [pdf](<https://devfeed.tech/topics/pdf.md>), [Front end](<https://devfeed.tech/topics/frontend.md>)

Tags: [auditing](<https://devfeed.tech/tags/auditing.md>), [automation](<https://devfeed.tech/tags/automation.md>), [bypass](<https://devfeed.tech/tags/bypass.md>), [content-type](<https://devfeed.tech/tags/content-type.md>), [csrf](<https://devfeed.tech/tags/csrf.md>), [doyensec](<https://devfeed.tech/tags/doyensec.md>), [doyensecurity](<https://devfeed.tech/tags/doyensecurity.md>), [exploit](<https://devfeed.tech/tags/exploit.md>), [file](<https://devfeed.tech/tags/file.md>), [front-end](<https://devfeed.tech/tags/front-end.md>), [hacking](<https://devfeed.tech/tags/hacking.md>), [images](<https://devfeed.tech/tags/images.md>), [infosec](<https://devfeed.tech/tags/infosec.md>), [json](<https://devfeed.tech/tags/json.md>), [json-object](<https://devfeed.tech/tags/json-object.md>), [libraries](<https://devfeed.tech/tags/libraries.md>), [library](<https://devfeed.tech/tags/library.md>), [magic](<https://devfeed.tech/tags/magic.md>), [mime](<https://devfeed.tech/tags/mime.md>), [node-js](<https://devfeed.tech/tags/node-js.md>), [offensive](<https://devfeed.tech/tags/offensive.md>), [path-traversal](<https://devfeed.tech/tags/path-traversal.md>), [pdf](<https://devfeed.tech/tags/pdf.md>), [pdfs](<https://devfeed.tech/tags/pdfs.md>), [pentest](<https://devfeed.tech/tags/pentest.md>), [pentester](<https://devfeed.tech/tags/pentester.md>), [reverse-engineering](<https://devfeed.tech/tags/reverse-engineering.md>), [security](<https://devfeed.tech/tags/security.md>), [upload](<https://devfeed.tech/tags/upload.md>), [v8](<https://devfeed.tech/tags/v8.md>), [vulnerability](<https://devfeed.tech/tags/vulnerability.md>)

## AI overview

The article shows how a JSON gadget used for client-side path traversal can be crafted to pass several file-upload checks, enabling uses such as CSPT-to-CSRF. For `mmmagic`, it places `%PDF` within the first 1,024 bytes of valid JSON, matching the library's PDF signature check. For `pdflib`, it embeds a PDF structure in a JSON string and replaces line feeds between PDF objects with spaces; the resulting file passes the described validation but may not render in recent PDF viewers. The `file` command first tries JSON parsing, so the article instead pads the payload with whitespace beyond its 1 MiB read limit, causing fallback file detection. For image validation, it puts `WEBP` at the offset checked by `file-type` while preserving valid JSON. The methods depend on the validator's specific detection behavior and do not establish that every implementation is bypassable.

## Source excerpt

In my previous blog post, I demonstrated how a JSON file could be used as a gadget for Client-Side Path Traversal (CSPT) to perform Cross-Site Request Forgery (CSRF). That example was straightforward because no file upload restriction was enforced. However, real-world applications often impose restrictions on file uploads to ensure security. In this post, we'll explore how to bypass some of these mechanisms to achieve the same goal. We'll cover common file validation methods and how they can be subverted. Constraint In most scenarios, the gadget file will be parsed in the front-end using JSON.parse. It means that our file must be a valid input for JSON.parse. If we look at the V8 implementation. A valid JSON input is : a string a number true false null an array an object The parser skips starting WHITESPACE characters such as : ' ' '\t' '\r' '\n' Also, control characters and double quotes inside a JSON object (key or value) will break the JSON structure and must be escaped. Our gadget file must follow these restrictions to be parsed as JSON. Different applications validate files using libraries or tools designed to detect the file's MIME type, file structure or magic bytes. By creatively crafting files that meet these conditions, we can fool these validations and bypass the restrictions. Let's explore how various file upload mechanisms can be bypassed to maintain valid JSON payloads for CSPT while satisfying file format requirements, such as PDFs or images. Bypassing PDF Checks To Upload a JSON File A basic check in many upload mechanisms involves verifying the file's MIME type. This is often done using the Content-Type header or by inspecting the file itself. However, these checks can often be bypassed by manipulating the file's structure or headers. Bypassing mmmagic Validation The mmmagic library is commonly used in Node.js applications to detect file types based on the Magic database. A PDF file can be verified with the following code: async function checkMMMagi