Skip to content

Commit

Permalink
Integrate with Ionizer, an input filtration library.
Browse files Browse the repository at this point in the history
  • Loading branch information
soatok committed Jun 28, 2019
1 parent d75f030 commit 67bc796
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"paragonie/corner": "^1|^2",
"paragonie/csp-builder": "^2",
"paragonie/easydb": "^2",
"paragonie/ionizer": "^0|^1",
"paragonie/stern": "^0|^1",
"slim/slim": "^3",
"soatok/dhole-cryptography": "^1",
Expand Down
47 changes: 43 additions & 4 deletions src/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Interop\Container\Exception\ContainerException;
use ParagonIE\ConstantTime\Base64UrlSafe;
use ParagonIE\CSPBuilder\CSPBuilder;
use ParagonIE\Ionizer\InputFilterContainer;
use ParagonIE\Ionizer\InvalidDataException;
use Psr\Http\Message\{
RequestInterface,
ResponseInterface
Expand Down Expand Up @@ -109,12 +111,24 @@ public function checkCsrfToken(array $postData = []): bool

/**
* @param RequestInterface $request
* @param InputFilterContainer|null $filter
* @return array
*/
public function get(RequestInterface $request): array
{
public function get(
RequestInterface $request,
?InputFilterContainer $filter = null
): array {
$get = [];
parse_str($request->getUri()->getQuery(), $get);
if ($filter) {
try {
return $filter($get);
} catch (InvalidDataException $ex) {
return [];
} catch (\TypeError $ex) {
return [];
}
}
return $get;
}

Expand Down Expand Up @@ -150,21 +164,46 @@ public function json(
}

/**
* Process form data. Return [] if CSRF attack. Return []
* if invalid data.
*
* @param RequestInterface $req
* @param string $type
* @param InputFilterContainer|null $filter
* @return array
*/
public function post(RequestInterface $req, $type = self::TYPE_FORM): array
{
public function post(
RequestInterface $req,
$type = self::TYPE_FORM,
?InputFilterContainer $filter = null
): array {
$post = $this->getPostBody($req, $type);
if (!$this->checkCsrfToken($post)) {
return [];
}
unset($post[static::CSRF_FORM_INDEX]);
if ($filter) {
try {
return $filter($post);
} catch (InvalidDataException $ex) {
return [];
} catch (\TypeError $ex) {
return [];
}
}
return $post;
}

/**
* Warning:
* This does not use Ionizer to force input to conform to an expected
* contract. Use at your own risk.
*
* Warning:
* This does not perform anti-CSRF validation. Use at your own risk.
*
* Try $this->post() instead.
*
* @param RequestInterface $req
* @param string $type
* @return array
Expand Down

0 comments on commit 67bc796

Please sign in to comment.