gpt4 book ai didi

php - 使用表单提交为 PHP 文件系统函数发送变量

转载 作者:搜寻专家 更新时间:2023-10-31 20:56:30 26 4
gpt4 key购买 nike

我正在尝试找到一种安全的方法来执行以下操作:

  1. 用户将值输入到 html 表单中。
  2. 表单已提交。
  3. PHP 使用提交的值作为“scandir”函数的参数。

我的理论是在 php 脚本中包含禁止绝对路径并要求目录名称包含特定值的逻辑。

我担心黑客可能会使用我的表单提交他自己的值并访问敏感文件。

这是一个 JQuery 插件。我不希望用户不得不修改 PHP 文件。

下面的代码是如何被破解的?

<?php

$foo = $_POST["some_directory"];

//validate $foo

//make sure path to directory is relative
$foo_url_test = parse_url($foo);
if (isset($foo_url_test['scheme']) || isset($foo_url_test['host'])) {
$foo = NULL;
}

//make sure the directory name contains 'bar123'
$foo_name_test = preg_split('_[\\\\/]_', $foo);
$foo_name_test = end($foo_name_test);
$foo_name_test = strpos($foo_name_test,'bar123');
if ($foo_name_test === false) {
$foo = NULL;
}

//make sure the path does not contain '..'
$foo_dot_dot_test = strpos($foo,'..');
if ($foo_dot_dot_test == TRUE || $foo_dot_dot_test === 0) {
$foo = NULL;
}

//get files
$files_array = scandir($foo);

?>

最佳答案

您可能想看看 realpath() .

$foo = realpath($foo);
if (substr($foo, 0, 8) != '/my/path') {
return false;
}

...或类似的东西。

关于php - 使用表单提交为 PHP 文件系统函数发送变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1066930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com