paragonie / ionizer
为PHP应用程序提供强大的输入值过滤功能
v1.0.3
2024-05-11 22:18 UTC
Requires
- php: ^7|^8
- ext-json: *
- paragonie/constant_time_encoding: ^2.1|^3
Requires (Dev)
- phpunit/phpunit: ^6.5|^7|^8|^9
- vimeo/psalm: ^1|^2|^3|^4
README
Ionizer为动态输入(例如HTTP请求参数)提供严格的数据类型和输入验证。需要PHP 7或更高版本。
Ionizer是什么?
Ionizer是一个针对HTTP表单数据的结构化输入过滤系统。
为什么Ionizer很重要?
除了能够为接受用户输入的应用程序严格指定数据类型的好处外,Ionizer还使得减轻一些NoSQL注入技术变得容易。
安装
获取Composer,然后运行以下命令:
composer require paragonie/ionizer
使用方法
<?php use ParagonIE\Ionizer\GeneralFilterContainer; use ParagonIE\Ionizer\Filter\{ StringFilter, AllowList }; // 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 AllowList('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. }
Ionizer甚至可以指定结构化输入,但有几点需要注意。
<?php use ParagonIE\Ionizer\GeneralFilterContainer; use ParagonIE\Ionizer\Filter\{ IntFilter, IntArrayFilter, StringArrayFilter, StringFilter }; $ic = new GeneralFilterContainer(); // You can type entire arrays at once: $ic->addFilter('numbers', new IntArrayFilter()) ->addFilter('strings', new StringArrayFilter()) // You can also specify subkeys, separated by a period: ->addFilter('user.name', new StringFilter()) ->addFilter('user.unixtime', new IntFilter()); $input = [ 'numbers' => [1, 2, 3], 'strings' => ['a', 'b'], 'user' => [ 'name' => 'test', 'unixtime' => time() ] ]; try { $valid = $ic($input); } catch (\TypeError $ex) { }
支持合同
如果您的公司在产品或服务中使用此库,您可能会对从Paragon Initiative Enterprises购买支持合同感兴趣。