| 1 | ## Threat Model for qs (querystring parsing library)
|
|---|
| 2 |
|
|---|
| 3 | ### 1. Library Overview
|
|---|
| 4 |
|
|---|
| 5 | - **Library Name:** qs
|
|---|
| 6 | - **Brief Description:** A JavaScript library for parsing and stringifying URL query strings, supporting nested objects and arrays. It is widely used in Node.js and web applications for processing query parameters[2][6][8].
|
|---|
| 7 | - **Key Public APIs/Functions:** `qs.parse()`, `qs.stringify()`
|
|---|
| 8 |
|
|---|
| 9 | ### 2. Define Scope
|
|---|
| 10 |
|
|---|
| 11 | This threat model focuses on the core parsing and stringifying functionality, specifically the handling of nested objects and arrays, option validation, and cycle management in stringification.
|
|---|
| 12 |
|
|---|
| 13 | ### 3. Conceptual System Diagram
|
|---|
| 14 |
|
|---|
| 15 | ```
|
|---|
| 16 | Caller Application → qs.parse(input, options) → Parsing Engine → Output Object
|
|---|
| 17 | │
|
|---|
| 18 | └→ Options Handling
|
|---|
| 19 |
|
|---|
| 20 | Caller Application → qs.stringify(obj, options) → Stringifying Engine → Output String
|
|---|
| 21 | │
|
|---|
| 22 | └→ Options Handling
|
|---|
| 23 | └→ Cycle Tracking
|
|---|
| 24 | ```
|
|---|
| 25 |
|
|---|
| 26 | **Trust Boundaries:**
|
|---|
| 27 | - **Input string (parse):** May come from untrusted sources (e.g., user input, network requests)
|
|---|
| 28 | - **Input object (stringify):** May contain cycles, which can lead to infinite loops during stringification
|
|---|
| 29 | - **Options:** Provided by the caller
|
|---|
| 30 | - **Cycle Tracking:** Used only during stringification to detect and handle circular references
|
|---|
| 31 |
|
|---|
| 32 | ### 4. Identify Assets
|
|---|
| 33 |
|
|---|
| 34 | - **Integrity of parsed output:** Prevent malicious manipulation of the output object structure, especially ensuring builtins/globals are not modified as a result of parse[3][4][8].
|
|---|
| 35 | - **Confidentiality of processed data:** Avoid leaking sensitive information through errors or output.
|
|---|
| 36 | - **Availability/performance for host application:** Prevent crashes or resource exhaustion in the consuming application.
|
|---|
| 37 | - **Security of host application:** Prevent the library from being a vector for attacks (e.g., prototype pollution, DoS).
|
|---|
| 38 | - **Reputation of library:** Maintain trust by avoiding supply chain attacks and vulnerabilities[1].
|
|---|
| 39 |
|
|---|
| 40 | ### 5. Identify Threats
|
|---|
| 41 |
|
|---|
| 42 | | Component / API / Interaction | S | T | R | I | D | E |
|
|---|
| 43 | |---------------------------------------|----|----|----|----|----|----|
|
|---|
| 44 | | Public API Call (`parse`) | – | ✓ | – | ✓ | ✓ | ✓ |
|
|---|
| 45 | | Public API Call (`stringify`) | – | ✓ | – | ✓ | ✓ | – |
|
|---|
| 46 | | Options Handling | ✓ | ✓ | – | ✓ | – | ✓ |
|
|---|
| 47 | | Dependency Interaction | – | – | – | – | ✓ | – |
|
|---|
| 48 |
|
|---|
| 49 | **Key Threats:**
|
|---|
| 50 | - **Tampering:** Malicious input can, if not prevented, alter parsed output (e.g., prototype pollution via `__proto__`, modification of builtins/globals)[3][4][8].
|
|---|
| 51 | - **Information Disclosure:** Error messages may expose internal details or sensitive data.
|
|---|
| 52 | - **Denial of Service:** Large or malformed input can exhaust memory or CPU.
|
|---|
| 53 | - **Elevation of Privilege:** Prototype pollution can lead to unintended privilege escalation in the host application[3][4][8].
|
|---|
| 54 |
|
|---|
| 55 | ### 6. Mitigation/Countermeasures
|
|---|
| 56 |
|
|---|
| 57 | | Threat Identified | Proposed Mitigation |
|
|---|
| 58 | |---------------------------------------------------|---------------------|
|
|---|
| 59 | | Tampering (malicious input, prototype pollution) | Strict input validation; keep `allowPrototypes: false` by default; use `plainObjects` for output; ensure builtins/globals are never modified by parse[4][8]. |
|
|---|
| 60 | | Information Disclosure (error messages) | Generic error messages without stack traces or internal paths. |
|
|---|
| 61 | | Denial of Service (memory/CPU exhaustion) | Enforce `arrayLimit` and `parameterLimit` with safe defaults; enable `throwOnLimitExceeded`; limit nesting depth[7]. |
|
|---|
| 62 | | Elevation of Privilege (prototype pollution) | Keep `allowPrototypes: false`; validate options against allowlist; use `plainObjects` to avoid prototype pollution[4][8]. |
|
|---|
| 63 |
|
|---|
| 64 | ### 7. Risk Ranking
|
|---|
| 65 |
|
|---|
| 66 | - **High:** Denial of Service via array parsing or malformed input (historical vulnerability)
|
|---|
| 67 | - **Medium:** Prototype pollution via options or input (if `allowPrototypes` enabled)
|
|---|
| 68 | - **Low:** Information disclosure in errors
|
|---|
| 69 |
|
|---|
| 70 | ### 8. Next Steps & Review
|
|---|
| 71 |
|
|---|
| 72 | 1. **Audit option validation logic.**
|
|---|
| 73 | 2. **Add depth limiting to nested parsing and stringification.**
|
|---|
| 74 | 3. **Implement fuzz testing for parser and stringifier edge cases.**
|
|---|
| 75 | 4. **Regularly review dependencies for vulnerabilities.**
|
|---|
| 76 | 5. **Keep documentation and threat model up to date.**
|
|---|
| 77 | 6. **Ensure builtins/globals are never modified as a result of parse.**
|
|---|
| 78 | 7. **Support round-trip consistency between parse and stringify as a non-security goal, with the right options[5][9].**
|
|---|