-
-
Save jtyjty99999/44475bfbb3a74481a8834225d2db037c to your computer and use it in GitHub Desktop.
join with normalize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const benchmark = require('benchmark'); | |
const suite = new benchmark.Suite; | |
const join = require('path').join; | |
const normalize = require('path').normalize; | |
const p = '/ddd/../../../aaa/bbb/ccc'; | |
function getRandomStr() { | |
return '__' + Math.floor(Math.random() * 16777215).toString(16) + '__'; | |
} | |
const judgeString = getRandomStr(); | |
function isSafePath(path, ctx) { | |
if (join(judgeString, path).indexOf(judgeString) === -1) { | |
return false; | |
} | |
return true; | |
}; | |
function isSafePath2(path, ctx) { | |
path = path.slice(1); | |
return normalize(path).startsWith('../'); | |
}; | |
// add tests | |
suite.add('join#test', function() { | |
isSafePath(p); | |
}) | |
.add('normalize#test', function() { | |
isSafePath2(p); | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
// run async | |
.run({ 'async': true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment