项目作者: Aldin-SXR

项目描述 :
A simple, no-dependency defense against MongoDB query selector injection attacks.
高级语言: PHP
项目地址: git://github.com/Aldin-SXR/mongo-sanitize.git
创建时间: 2019-10-18T21:16:31Z
项目社区:https://github.com/Aldin-SXR/mongo-sanitize

开源协议:MIT License

下载


mongo-sanitize

Build Status

A simple, no-dependency PHP library for defense against MongoDB query selector injection attacks. Inspired by the homonymous NPM package for NodeJS.

Installation and Usage

The library is available via Composer.

composer require aldin-sxr/mongo-sanitize

After installing, include vendor/autoload.php in your project.

  1. <?php
  2. require_once 'vendor/autoload.php';
  3. $data = [
  4. 'hello' => 'world',
  5. 'foo' => [ '$eq' => 'bar' ]
  6. ];
  7. $cleaned = mongo_sanitize($data);
  8. // Cleaned array:
  9. // [ 'hello' => 'world, 'foo' => [ ] ]

Call mongo_sanitize() on the arrays (user input) which you want to sanitize. The function will remove any array elements whose keys start with a $ (MongoDB operator identifier). The function also works recursively, on embedded array elements.

Testing

All library methods come with several unit tests in PHPUnit, which are available under tests/unit.

License

The library is licensed under the MIT license. See the LICENSE file for details.