te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>security - Is this use of Javascript eval() 100% safe? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

security - Is this use of Javascript eval() 100% safe? - Stack Overflow

programmeradmin1浏览0评论

I'm writing a PHP library which generates Javascript code.

The Javascript code has a number of ponents named ponent001, ponent002, etc.

Pages are loaded dynamically via AJAX.

I need to pass the name of the ponent via URL variable which is then evaled() by the script.

The only way I am protecting what is being evaled is with the regular expression ^ponent[0-9]{3}$: if it passes it gets evaled, otherwise it does not.

To me this is 100% safe since nothing will get executed unless it is simply the name of one of my known ponents, or is there something about the eval() mand that could be exploited in this code sample, e.g. regex injection, some kind of cross site scripting etc.?

window.onload = function() {

    // *** DEFINED IN ANOTHER JAVASCRIPT FILE:
    var ponent001 = 'testing111';
    var ponent002 = 'testing222';
    var ponent003 = 'testing333';

    var APP = {};

    APP.getUrlVars = function() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }

    APP.getUrlVar = function(name, defaultValue) {
        defaultValue = (typeof defaultValue == 'undefined') ? '' : defaultValue;
        var vars = APP.getUrlVars();
        if(vars[name] === undefined)
        {
            return defaultValue;
        } else {
            return vars[name];
        }
    }

    APP.safeEval = function(nameOfComponent) {
        var REGEX_VALID_NAME = /^ponent[0-9]{3}$/;
        if(REGEX_VALID_NAME.test(nameOfComponent)) {
            return eval(nameOfComponent);
        } else {
            return 'ERROR';
        }

    }

    // *** JAVASCRIPT FILE LOADED VIA AJAX:

    var nameOfComponentToDisplay = APP.getUrlVar('pname', 'ponent001');
    var ponent = APP.safeEval(nameOfComponentToDisplay);
    document.write(ponent);

}

I'm writing a PHP library which generates Javascript code.

The Javascript code has a number of ponents named ponent001, ponent002, etc.

Pages are loaded dynamically via AJAX.

I need to pass the name of the ponent via URL variable which is then evaled() by the script.

The only way I am protecting what is being evaled is with the regular expression ^ponent[0-9]{3}$: if it passes it gets evaled, otherwise it does not.

To me this is 100% safe since nothing will get executed unless it is simply the name of one of my known ponents, or is there something about the eval() mand that could be exploited in this code sample, e.g. regex injection, some kind of cross site scripting etc.?

window.onload = function() {

    // *** DEFINED IN ANOTHER JAVASCRIPT FILE:
    var ponent001 = 'testing111';
    var ponent002 = 'testing222';
    var ponent003 = 'testing333';

    var APP = {};

    APP.getUrlVars = function() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }

    APP.getUrlVar = function(name, defaultValue) {
        defaultValue = (typeof defaultValue == 'undefined') ? '' : defaultValue;
        var vars = APP.getUrlVars();
        if(vars[name] === undefined)
        {
            return defaultValue;
        } else {
            return vars[name];
        }
    }

    APP.safeEval = function(nameOfComponent) {
        var REGEX_VALID_NAME = /^ponent[0-9]{3}$/;
        if(REGEX_VALID_NAME.test(nameOfComponent)) {
            return eval(nameOfComponent);
        } else {
            return 'ERROR';
        }

    }

    // *** JAVASCRIPT FILE LOADED VIA AJAX:

    var nameOfComponentToDisplay = APP.getUrlVar('pname', 'ponent001');
    var ponent = APP.safeEval(nameOfComponentToDisplay);
    document.write(ponent);

}
Share Improve this question asked Dec 21, 2010 at 14:38 Edward TanguayEdward Tanguay 193k320 gold badges725 silver badges1.1k bronze badges 3
  • 4 Instead of using eval for this, I'd use the square bracket notation to call your function (e.g. APP[nameOfComponent]();) – Marcel Korpel Commented Dec 21, 2010 at 14:42
  • regex and eval() are both open ended tools that introduce risk. The problem you describe need not embrace that risk. Instead - as @Marcel Korpel and @ChaosPandio have writ - use string matching and function invoking to avoid. – orangepips Commented Dec 21, 2010 at 14:48
  • The only excuse I can think of to use eval() is if you're writing something like the Firebug console. As other answers show, most places where you might consider using eval can be acheived better without it anyway. And in ihe few remaining scenarios where eval() may genuinely be useful, it should certainly not be considered safe. – Spudley Commented Dec 21, 2010 at 14:53
Add a ment  | 

3 Answers 3

Reset to default 15

There is almost zero reasons to use eval and I think that this is not one of them. Remember that all objects act like dictionaries so you can simply do something like this:

var ponents = {
    ponent001 : 'testing111',
    ponent002 : 'testing222',
    ponent003 : 'testing333'
};

APP.safeEval = function(nameOfComponent) {
    var result = ponents[nameOfComponent];
    if(result) {
        return result;
    } else {
        return 'ERROR';
    }
}

Well, if all there is is a name, then

  eval(ponent101)

won't do anything anyway, so it seems safe. Maybe you meant

  return eval(nameOfComponent + '()');

If so, then I don't see why you don't just put your ponents in a namespace object. Then you wouldn't need eval at all:

  return ponents[nameOfComponent]();

If they're not functions, then the same thing applies, but you'd leave off the "()".

If the variables are defined in another javascript file and contain only numbers and letters, then they are part of the global namespace. As such, they can be accessed as properties of the window object (no need for eval!):

if (typeof window[nameOfComponent] !== 'undefined')
    return window[nameOfComponent]
return 'ERROR';
发布评论

评论列表(0)

  1. 暂无评论