|
| 1 | +/** |
| 2 | + * @name Exposure of private files |
| 3 | + * @description Exposing a node_modules folder, or the project folder to the public, can cause exposure |
| 4 | + * of private information. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity warning |
| 7 | + * @id js/exposure-of-private-files |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-200 |
| 10 | + * @precision high |
| 11 | + */ |
| 12 | + |
| 13 | +import javascript |
| 14 | + |
| 15 | +/** |
| 16 | + * Holds if `folder` is a node_modules folder, and at most 1 subdirectory down. |
| 17 | + */ |
| 18 | +bindingset[folder] |
| 19 | +predicate isNodeModuleFolder(string folder) { |
| 20 | + folder.regexpMatch("(\\.?\\.?/)*node_modules(/|(/[a-zA-Z@_-]+/?))?") |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Get a data-flow node that represents a path to the node_modules folder represented by the string-literal `path`. |
| 25 | + */ |
| 26 | +DataFlow::Node getANodeModulePath(string path) { |
| 27 | + result.getStringValue() = path and |
| 28 | + isNodeModuleFolder(path) |
| 29 | + or |
| 30 | + exists(DataFlow::CallNode resolve | |
| 31 | + resolve = DataFlow::moduleMember("path", ["resolve", "join"]).getACall() |
| 32 | + | |
| 33 | + result = resolve and |
| 34 | + resolve.getLastArgument() = getANodeModulePath(path) |
| 35 | + ) |
| 36 | + or |
| 37 | + exists(StringOps::ConcatenationRoot root | root = result | |
| 38 | + root.getLastLeaf() = getANodeModulePath(path) |
| 39 | + ) |
| 40 | + or |
| 41 | + result.getAPredecessor() = getANodeModulePath(path) // local data-flow |
| 42 | + or |
| 43 | + exists(string base, string folder | |
| 44 | + path = base + folder and |
| 45 | + folder.regexpMatch("(/)?[a-zA-Z@_-]+/?") and |
| 46 | + base.regexpMatch("(\\.?\\.?/)*node_modules(/)?") // node_modules, without any sub-folders. |
| 47 | + | |
| 48 | + exists(StringOps::ConcatenationRoot root | root = result | |
| 49 | + root.getNumOperand() = 2 and |
| 50 | + root.getFirstLeaf() = getANodeModulePath(base) and |
| 51 | + root.getLastLeaf().getStringValue() = folder |
| 52 | + ) |
| 53 | + or |
| 54 | + exists(DataFlow::CallNode resolve | |
| 55 | + resolve = DataFlow::moduleMember("path", ["resolve", "join"]).getACall() |
| 56 | + | |
| 57 | + result = resolve and |
| 58 | + resolve.getNumArgument() = 2 and |
| 59 | + resolve.getArgument(0) = getANodeModulePath(path) and |
| 60 | + resolve.getArgument(1).mayHaveStringValue(folder) |
| 61 | + ) |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Gets a folder that contains a `package.json` file. |
| 67 | + */ |
| 68 | +pragma[noinline] |
| 69 | +Folder getAPackageJSONFolder() { result = any(PackageJSON json).getFile().getParentContainer() } |
| 70 | + |
| 71 | +/** |
| 72 | + * Gets a reference to `dirname` that might cause information to be leaked. |
| 73 | + * That can happen if there is a `package.json` file in the same folder. |
| 74 | + * (It is assumed that the presence of a `package.json` file means that a `node_modules` folder can also exist. |
| 75 | + */ |
| 76 | +DataFlow::Node dirname() { |
| 77 | + exists(ModuleScope ms | result.asExpr() = ms.getVariable("__dirname").getAnAccess()) and |
| 78 | + result.getFile().getParentContainer() = getAPackageJSONFolder() |
| 79 | + or |
| 80 | + result.getAPredecessor() = dirname() |
| 81 | + or |
| 82 | + exists(StringOps::ConcatenationRoot root | root = result | |
| 83 | + root.getNumOperand() = 2 and |
| 84 | + root.getOperand(0) = dirname() and |
| 85 | + root.getOperand(1).getStringValue() = "/" |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Gets a data-flow node that represents a path to the private folder `path`. |
| 91 | + */ |
| 92 | +DataFlow::Node getAPrivateFolderPath(string description) { |
| 93 | + exists(string path | |
| 94 | + result = getANodeModulePath(path) and description = "the folder \"" + path + "\"" |
| 95 | + ) |
| 96 | + or |
| 97 | + result = dirname() and |
| 98 | + description = "the folder " + result.getFile().getParentContainer().getRelativePath() |
| 99 | + or |
| 100 | + result.getStringValue() = [".", "./"] and |
| 101 | + description = "the current working folder" |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Gest a call that serves the folder `path` to the public. |
| 106 | + */ |
| 107 | +DataFlow::CallNode servesAPrivateFolder(string description) { |
| 108 | + result = DataFlow::moduleMember("express", "static").getACall() and |
| 109 | + result.getArgument(0) = getAPrivateFolderPath(description) |
| 110 | +} |
| 111 | + |
| 112 | +from Express::RouteSetup setup, string path |
| 113 | +where |
| 114 | + setup.isUseCall() and |
| 115 | + setup.getArgument([0 .. 1]) = servesAPrivateFolder(path).getEnclosingExpr() |
| 116 | +select setup, "Serves " + path + ", which can contain private information." |
0 commit comments