如何应对邪恶?在System.Shell.execute()之前验证用户输入

How to deal with evil? Validating user input before System.Shell.execute();

本文关键字:验证 输入 用户 execute Shell 何应 邪恶 System      更新时间:2023-09-26

我正在使用Windows gadget的API来启动url,但我知道eval()之类的东西的破坏力以及更糟糕和更危险的System.Shell.execute();

但是经过一番研究,我认为没有更好的方法在默认浏览器上启动URL而不执行()。既然url来自用户输入,那么如何防止用户执行恶意代码呢?我的代码是安全的还是可以利用?防止cmd.exe /c REG QUERY HKCU etc以管理员权限启动cmd.exe。

function openURL(url){
    var protocol=new Array();
    //Allowed protocols to execute
    protocol[0]='http://';
    protocol[1]='https://';
    protocol[2]='ftp://';
    protocol[3]='search-ms:query=';
    for(var i=0;i<protocol.length;i++){
        if(url.indexOf(protocol[i])==0){
            System.Shell.execute(url);
            break;
        }
    }
}
window.open(); //doesn't work (only open IE);
编辑:

允许这两个协议是不安全的file:///javascript:可以这样做的例子:

file:///c:/windows/system32/ping.exe

javascript:void( window.open('http://file:///c:/windows/system32/ping.exe','','_blank') );

您的使用听起来像是Google Caja的潜在候选人。这是一个试图清除第三方JavaScript的项目,以使其安全运行。