【问题标题】:css compression with php security bug带有 php 安全漏洞的 css 压缩
【发布时间】:2013-01-25 05:17:49
【问题描述】:

我有一个脚本,它使用提供给它的查询并分解它们调用目录中的文件。这里是脚本

<?php
header("Content-type: text/css");

$safehash = sha1(binary-style_1afn34jdd2);

$css = '';
$root = 'css/'; //directory where the css lives
$files = explode(',',$_SERVER['QUERY_STRING']);
if(sizeof($files))
{
  foreach($files as $file)
  {
    $css.= (is_file($root.$file.'.css') ? file_get_contents($root.$file.'.css') : '');
  }
}
function compress($css){
  // Remove comments
  $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
  // Remove spaces before and after symbols
  $css = preg_replace('/(\s(?=\W))|((?<=\W)\s)/', '', $css);
  // Remove remaining whitespace
  $css = str_replace(array("\r\n","\r","\n","\t",'  ','    ','    '), '', $css);
  return $css;
}

echo compress($css);
?>

问题在于,有些人可能会通过在脚本中添加越来越多的查询(例如 style.php?sheet,sheet,sheet,sheet.....ect保护此代码的最佳方法是什么?

【问题讨论】:

  • 您对服务器崩溃的猜测几乎不相关(使用循环计数器或 array_slice,如果)。真正的问题是未过滤的文件名。
  • 其实我想我可能已经解决了使用define的问题
  • 记住你已经读过的文件只读一次。

标签: php security code-access-security


【解决方案1】:

您可以简单地限制查询字符串的大小:

if(mb_strlen($_SERVER['QUERY_STRING'])>10) {
    header('HTTP/1.0 413 Request Entity Too Large');
    exit;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    相关资源
    最近更新 更多