Security
in package
Tags
Table of Contents
Methods
- __construct() : mixed
- check_csrf_token() : bool
- Checks the anti-CSRF token of a posted request.
- client_ip() : string
- Client address kept as the third factor of an OID.
- csp_nonce() : string
- CSP nonce of the request, allowing the inline scripts the framework emits itself (event registration and JSON answers) while blocking the ones stored content could inject.
- csp_policy() : string
- Value of the Content-Security-Policy header.
- csrf_token() : string
- Anti-CSRF token of the current session.
- get_base_url() : mixed
- my_real_escape_string() : mixed
- process_all_data() : mixed
- random_token() : string
- Random identifier of $bytes random bytes, written in hexadecimal.
- refresh_script_nonce() : string
- Gives the CSP nonce of the current request to inline scripts rendered during an earlier one.
- secure_sql_string() : mixed
- send_csp_header() : mixed
- Content-Security-Policy header of the application pages.
Methods
__construct()
public
__construct() : mixed
check_csrf_token()
Checks the anti-CSRF token of a posted request.
public
static check_csrf_token(array<string|int, mixed> $post) : bool
Parameters
- $post : array<string|int, mixed>
Return values
boolclient_ip()
Client address kept as the third factor of an OID.
public
static client_ip(string $declared) : string
Strict by default: the real connection address (REMOTE_ADDR) is used, so the triplet keeps a probative value. An instance running behind a proxy, a CDN or a VPN, where REMOTE_ADDR is the hop and the client address arrives in a header (X-HelpHP-HICUT for MCP, user_identifier for REST), sets Config::AUTH_STRICT_IP to false; the declared address is then used, which reduces the triplet to « token + user-agent » - an accepted trade-off for those setups.
Parameters
- $declared : string
-
address announced by the client
Return values
stringcsp_nonce()
CSP nonce of the request, allowing the inline scripts the framework emits itself (event registration and JSON answers) while blocking the ones stored content could inject.
public
static csp_nonce() : string
Return values
stringcsp_policy()
Value of the Content-Security-Policy header.
public
static csp_policy() : string
Kept apart from send_csp_header() so that a test can read it: in CLI the header cannot be sent, and the directives are exactly what a regression would silently break.
Return values
stringcsrf_token()
Anti-CSRF token of the current session.
public
static csrf_token() : string
It is written into a meta tag of the page (see H::new_document) and added by js/ajax.js to every request; HelPHP_module::process_data() refuses a posted action without it. One token per session: it survives reloads and forms left open for a while.
Return values
stringget_base_url()
public
static get_base_url() : mixed
my_real_escape_string()
public
static my_real_escape_string(mixed $str) : mixed
Parameters
- $str : mixed
process_all_data()
public
static process_all_data() : mixed
random_token()
Random identifier of $bytes random bytes, written in hexadecimal.
public
static random_token([int $bytes = 20 ]) : string
Every token of the framework goes through here (CSP nonce, CSRF token, session id, OID, user identifier, upload operation id) so that the length is a parameter and the generator is chosen once. random_bytes() is the CSPRNG; do not replace it with uniqid() or rand().
Parameters
- $bytes : int = 20
-
Number of random bytes; the string is twice as long.
Return values
stringrefresh_script_nonce()
Gives the CSP nonce of the current request to inline scripts rendered during an earlier one.
public
static refresh_script_nonce(string $html) : string
A document cache (public/document/cache/) and anything else stored as ready-made HTML keeps the nonce of the request that wrote it. Served later, script-src only accepts the nonce of the response, so the browser silently drops every script of the cached content: block behaviour, image viewer, animations. The markup is ours, so refreshing the attribute is enough; the length check keeps a script that carries a foreign nonce untouched.
Parameters
- $html : string
Return values
stringsecure_sql_string()
public
static secure_sql_string(mixed $str) : mixed
Parameters
- $str : mixed
send_csp_header()
Content-Security-Policy header of the application pages.
public
static send_csp_header() : mixed
script-src without 'unsafe-inline': a <script> or an on* attribute injected into the page does not run. The application's own scripts go through the nonce (see Html::open_html_tag), and 'unsafe-eval' is still required by the eval() of js/init.js and js/dom.js, which replay the fragments received by AJAX. Other resources are not restricted, so themes and remote media keep working.