Skip to content

Instantly share code, notes, and snippets.

@jtyjty99999
Created June 15, 2017 02:09
Show Gist options
  • Select an option

  • Save jtyjty99999/44475bfbb3a74481a8834225d2db037c to your computer and use it in GitHub Desktop.

Select an option

Save jtyjty99999/44475bfbb3a74481a8834225d2db037c to your computer and use it in GitHub Desktop.

Revisions

  1. jtyjty99999 created this gist Jun 15, 2017.
    43 changes: 43 additions & 0 deletions 1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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 });