HelPHP

Security
in package

Tags
class

Security

Small but important class, it secured all entering requests to avoid any kind of injection.

There is security measure at server level (.htaccess mod_security etc), in the javascript side (validator.js etc), but it's still possible to forge a request directly to the server from cli or another unknow method,

so the execution of Security::process_all_data is made at init to avoid any kind of injection. It protect also against script injection in UI, partial protection against URL spoofing.

HelPHP_module check also if the column names of data exist in the DB, plus the DB class that offer prepared queries.

The session protect itself too (against session spoofing and cookie stealing etc)...

We have the chance to never suffer from hacking (except from a bit of ddos on hosting server a long time ago) and we hope it will continue like that. But of course if you find a security hole in HelPHP, we'll be pleased to discuss about it and modify our code.

Thanks a lot for your concern.

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
bool

client_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
string

csp_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
string

csp_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
string

csrf_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
string

get_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
string

refresh_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
string

secure_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.

On this page

Search results