|
| 1 | +# CAP CDS Utils used with user-controlled sources |
| 2 | + |
| 3 | +If a path is constructed from user-provided input without sufficient sanitization, a malicious user may be able to manipulate the contents of the filesystem without proper authorization. |
| 4 | + |
| 5 | +Additionally if user-provided input is used to create file contents this can also result in a malicious user manipulating the filesystem in an unchecked way. |
| 6 | + |
| 7 | +## Recommendation |
| 8 | + |
| 9 | +CAP applications using CDS Utils should not use user-provided input without sanitization. |
| 10 | + |
| 11 | +The sanitization stragety can vary depending on what types of paths are satisfactory as user-provided input. A simple approach to sanitization is to check user-provided input against an allow list. Other potential approaches include checking components of paths or normalizing them to make sure that the path does not escape the expected root folder. |
| 12 | + |
| 13 | +Normalization techniques should be carefully considered and simple naive replacement strategies will not be sufficient, for example replacing any match of a parent directory reference (`../`) in the sample `.../...//` will still result in the path `../` being used which could escape the intended directory. |
| 14 | + |
| 15 | +## Examples |
| 16 | + |
| 17 | +This CAP service directly uses user-provided input to construct a path. |
| 18 | + |
| 19 | +``` javascript |
| 20 | +const cds = require("@sap/cds"); |
| 21 | +const { rm } = cds.utils |
| 22 | + |
| 23 | +module.exports = class Service1 extends cds.ApplicationService { |
| 24 | + |
| 25 | + init() { |
| 26 | + this.on("send1", async (req) => { |
| 27 | + let userinput = req.data |
| 28 | + await rm(userinput, 'db', 'data') // Path injection alert |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | +
|
| 34 | +This CAP service directly uses user-provided input to add content to a file. |
| 35 | +
|
| 36 | +``` javascript |
| 37 | +const cds = require("@sap/cds"); |
| 38 | +const { rm } = cds.utils |
| 39 | + |
| 40 | +module.exports = class Service1 extends cds.ApplicationService { |
| 41 | + init() { |
| 42 | + this.on("send1", async (req) => { |
| 43 | + let userinput = req.data |
| 44 | + await write(userinput).to('db/data') // Path injection alert |
| 45 | + |
| 46 | + // GOOD: the path can not be controlled by an attacker |
| 47 | + let allowedDirectories = [ |
| 48 | + 'this-is-a-safe-directory' |
| 49 | + ]; |
| 50 | + if (allowedDirectories.includes(userinput)) { |
| 51 | + await rm(userinput) // sanitized - No Path injection alert |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | +
|
| 58 | +## References |
| 59 | +
|
| 60 | +- OWASP 2021: [Injection](https://owasp.org/Top10/A03_2021-Injection/). |
| 61 | +- SAP CAP CDS Utils : [Documentation](https://cap.cloud.sap/docs/node.js/cds-utils). |
| 62 | +- Common Weakness Enumeration: [CWE-020](https://cwe.mitre.org/data/definitions/20.html). |
| 63 | +- Common Weakness Enumeration: [CWE-022](https://cwe.mitre.org/data/definitions/22.html). |
0 commit comments