Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update path-join-resolve-traversal rule #645

Merged
merged 4 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var path = require('path');

function test1() {
// ruleid: path-join-resolve-traversal
var path = require('path');
function someFunc(entry) {
var extractPath = path.join(opts.path, entry.path);
return extractFile(extractPath);
Expand All @@ -10,10 +11,9 @@ function test1() {

function test2() {
// ruleid: path-join-resolve-traversal
const pth = require('path')
function someFunc(val) {
createFile({
filePath: pth.resolve(opts.path, val)
filePath: path.resolve(opts.path, val)
})
return true
}
Expand All @@ -22,14 +22,12 @@ function test2() {

function test3() {
// ruleid: path-join-resolve-traversal
let path = require('path')
let somePath = req.body.path;
return path.join(opts.path, somePath);
}

function okTest1() {
// ok
const pth = require('path')
function someFunc() {
createFile({
filePath: pth.join(opts.path, 'val')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,172 +1,117 @@
rules:
- id: path-join-resolve-traversal
message: |
Possible writing outside of the destination,
make sure that the target path is nested in the intended destination
metadata:
cwe: "CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')"
owasp: 'A1: Injection'
references:
- https://owasp.org/www-community/attacks/Path_Traversal
patterns:
- pattern-inside: "... $PATH = require('path'); ..."
- pattern-either:
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
$PATH.join(...,$Y,...);
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
var $R = {$SMTH:$PATH.join(...,$Y,...)};
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
$R({$SMTH:$PATH.join(...,$Y,...)});
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
$PATH.join(...,$Y.$P,...);
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
var $R = {$SMTH:$PATH.join(...,$Y.$P,...)};
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
$R({$SMTH:$PATH.join(...,$Y.$P,...)});
...
}
- pattern: |
var $PATH = require('path');
...
var $X = ...;
...
$PATH.join(...,$X,...);
- pattern: |
var $PATH = require('path');
...
$X = ...;
...
$PATH.join(...,$X,...);
- pattern: |
var $PATH = require('path');
...
$PATH.join(...,$X(),...);
- pattern: |
var $PATH = require('path');
...
$PATH.join(...,$X.$P(),...);
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
$PATH.resolve(...,$Y,...);
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
var $R = {$SMTH:$PATH.resolve(...,$Y,...)};
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
$R({$SMTH:$PATH.resolve(...,$Y,...)});
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
$PATH.resolve(...,$Y.$P,...);
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
var $R = {$SMTH:$PATH.resolve(...,$Y.$P,...)};
...
}
- pattern: |
var $PATH = require('path');
...
function $X(...,$Y,...) {
...
$R({$SMTH:$PATH.resolve(...,$Y.$P,...)});
...
}
- pattern: |
var $PATH = require('path');
...
var $X = ...;
...
$PATH.resolve(...,$X,...);
- pattern: |
var $PATH = require('path');
...
$X = ...;
...
$PATH.resolve(...,$X,...);
- pattern: |
var $PATH = require('path');
...
$PATH.resolve(...,$X(),...);
- pattern: |
var $PATH = require('path');
...
$PATH.resolve(...,$X.$P(),...);
- pattern-not: |
var $PATH = require('path');
...
$X = "...";
...
$PATH.join(...,$X,...);
- pattern-not: |
var $PATH = require('path');
...
var $X = "...";
...
$PATH.join(...,$X,...);
- pattern-not: |
var $PATH = require('path');
...
$X = "...";
...
$PATH.resolve(...,$X,...);
- pattern-not: |
var $PATH = require('path');
...
var $X = "...";
...
$PATH.resolve(...,$X,...);
Expand All @@ -182,5 +127,11 @@ rules:
- pattern-not-inside: |
...
if (<... $R.startsWith(...) ...>) {...}
message: |
Possible writing outside of the destination,
make sure that the target path is nested in the intended destination
languages: [javascript]
severity: ERROR
metadata:
owasp: 'A5: Broken Access Control'
cwe: 'CWE-22: Improper Limitation of a Pathname to a Restricted Directory (Path Traversal)'
severity: WARNING
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// example from https://raw.githubusercontent.com/aviadatsnyk/adm-zip/576f7c5a5ccd529c75c0c7cf870a69856d99ca3a/adm-zip.js
var Utils = require("./util");
// ruleid: path-join-resolve-traversal
// ruleid:path-join-resolve-traversal
var fs = Utils.FileSystem.require(),
pth = require("path");

Expand Down Expand Up @@ -247,7 +247,7 @@ module.exports = function(/*String*/input) {
}
// normalize the path first
localPath = pth.normalize(localPath);
localPath = localPath.split("\\").join("/"); //windows fix
localPath = localPath.split("\\").join("/"); //windows fix
if (localPath.charAt(localPath.length - 1) != "/")
localPath += "/";

Expand All @@ -258,7 +258,7 @@ module.exports = function(/*String*/input) {

if (items.length) {
items.forEach(function(path) {
var p = path.split("\\").join("/").replace( new RegExp(localPath.replace(/(\(|\))/g,'\\$1'), 'i'), ""); //windows fix
var p = path.split("\\").join("/").replace( new RegExp(localPath.replace(/(\(|\))/g,'\\$1'), 'i'), ""); //windows fix
if (filter(p)) {
if (p.charAt(p.length - 1) !== "/") {
self.addFile(zipPath+p, fs.readFileSync(path), "", 0)
Expand Down Expand Up @@ -347,14 +347,14 @@ module.exports = function(/*String*/input) {
throw Utils.Errors.NO_ENTRY;
}

// ruleid:path-join-resolve-traversal
var entryName = item.entryName;

if(isWin){
entryName = escapeFileName(entryName)
}



// ruleid:path-join-resolve-traversal
var target = pth.resolve(targetPath, maintainEntryPath ? entryName : pth.basename(entryName));

if (item.isDirectory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module.exports = Extract;
var Parse = require('./parse');
var Writer = require('fstream').Writer;
var util = require('util');
// ruleid: path-join-resolve-traversal
var path = require('path');

util.inherits(Extract, Parse);

// ruleid:path-join-resolve-traversal
function Extract (opts) {
if (!(this instanceof Extract))
return new Extract(opts);
Expand Down