diff --git a/README.md b/README.md index 4081551..d94d65f 100644 --- a/README.md +++ b/README.md @@ -52,10 +52,15 @@ require_once 'vendor/autoload.php'; use InitPHP\Input\Facade\Inputs as Input; // GET /?name=Jane -// echo isset($_GET['name']) ? $_GET['name'] : 'John'; -echo Input::get('name', 'John'); // 'Jane' +// $name = isset($_GET['name']) ? $_GET['name'] : 'John'; +$name = Input::get('name', 'John'); // 'Jane' ``` +> **Security:** `Input` returns request data as-is. Values may be arrays and may +> contain markup, so escape them for the context they are printed in, for example +> `htmlspecialchars($name, ENT_QUOTES, 'UTF-8')` inside HTML (after checking +> `is_string($name)`). Never `echo` a raw `Input` value into a page. + ### With an instance ```php diff --git a/docs/Quick-Start.md b/docs/Quick-Start.md index 0ffcbbd..c124a7b 100644 --- a/docs/Quick-Start.md +++ b/docs/Quick-Start.md @@ -17,7 +17,11 @@ require __DIR__ . '/vendor/autoload.php'; use InitPHP\Input\Facade\Inputs as Input; // GET /?name=Jane -echo Input::get('name', 'John'); // Jane +$name = Input::get('name', 'John'); // 'Jane' + +// Input returns the request value as-is (it can be an array, or contain markup). +// Escape it for the context you print it in — here, HTML: +echo htmlspecialchars(is_string($name) ? $name : 'John', ENT_QUOTES, 'UTF-8'); ``` ### An instance — explicit and testable @@ -31,7 +35,10 @@ use InitPHP\Input\Inputs; $input = new Inputs(); // reads $_GET, $_POST and php://input -echo $input->get('name', 'John'); +$name = $input->get('name', 'John'); + +// Escape request data before printing it into HTML. +echo htmlspecialchars(is_string($name) ? $name : 'John', ENT_QUOTES, 'UTF-8'); ``` Pass the sources in to make the object self-contained (great for tests): diff --git a/docs/Recipes.md b/docs/Recipes.md index 8afc4e4..1e78c9a 100644 --- a/docs/Recipes.md +++ b/docs/Recipes.md @@ -60,9 +60,15 @@ Useful for endpoints that take parameters either in the URL or the body. use InitPHP\Input\Facade\Inputs as Input; // Checks $_GET first, then the JSON body. -$apiKey = Input::getRaw('api_key', null, ['required']); +$locale = Input::getRaw('locale', null, ['required']); ``` +Only do this for non-secret parameters such as `locale`, `page` or `sort`. A +value read from the query string ends up in access logs, browser history and +`Referer` headers, so credentials such as API keys and tokens belong in the +`Authorization` header or the request body — read them with +`raw()` only (see [Source Priority](Source-Priority.md)), never from `$_GET`. + ## A login form ```php