【问题标题】:How to protect against input arrays如何防止输入数组
【发布时间】:2018-10-08 07:44:11
【问题描述】:

我有一个程序。它接受字母数字字符串的输入(我已经对其进行了检查)。 所以一个有效的输入是www.example.com/myfile.php?input=John1

但是,如果有人输入www.example.com/myfile.php?input[],那么它会破坏我整个程序的逻辑中断,因为我不接受作为数组的输入。我怎么能不确定用户输入的东西只是一个字符串。不是数组,或任何其他数据类型/结构?

【问题讨论】:

  • 使用if (!strpos("["))
  • @Mohammad 对不起,你能给我举个例子吗?
  • 欢迎。 www.example.com/myfile.php?input=John1 包含非字母数字字符。
  • if (is_string($_GET['input'])) {echo 'variable is a string';}

标签: php security get


【解决方案1】:

解决这个问题的方法缓慢而乏味,其中涉及大量的手动类型检查。在整个应用程序中写下if (!is_string($foo)) 条件时,键盘会磨损。

或者您可以使用Ionizer,它专为解决这个确切问题而设计。

<?php

use ParagonIE\Ionizer\GeneralFilterContainer;
use ParagonIE\Ionizer\Filter\{
    StringFilter,
    WhiteList
};

// Define properties to filter:
$ic = new GeneralFilterContainer();
$ic->addFilter(
        'username',
        (new StringFilter())->setPattern('^[A-Za-z0-9_\-]{3,24}$')
    )
    ->addFilter('passphrase', new StringFilter())
    ->addFilter(
        'domain',
        new WhiteList('US-1', 'US-2', 'EU-1', 'EU-2')
    );

// Invoke the filter container on the array to get the filtered result:
try {
    // $post passed all of our filters.
    $post = $ic($_POST);
} catch (\TypeError $ex) {
    // Invalid data provided.
}

如果有人尝试传递数组而不是字符串,$ic($_POST) 会抛出 TypeError,然后您可以优雅地捕获、记录和失败。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 2020-09-23
    • 2019-12-18
    • 1970-01-01
    • 2013-10-19
    • 2013-03-21
    相关资源
    最近更新 更多