Files
itflow/functions/request_fields.php

118 lines
5.1 KiB
PHP

<?php
/*
* ITFlow - what was written in an editor, sent as a file
*
* A web application firewall in front of ITFlow - ModSecurity, Cloudflare's, CrowdSec's with
* the OWASP Core Rule Set - reads every posted field for script being slipped in, and to the
* Core Rule Set a rich-text editor's HTML is just that: an <img> at all, a pasted picture's
* data: address, a colour or an alignment (style=) each block the post, and a few in a short
* while ban whoever sent them. A reply's draft, saved a moment after typing stops, sent it
* again and again.
*
* So the browser (js/http.js) sends what an editor holds - each rich-text editor's and Markdown
* box's field, and anything itflowPost() sends that looks like HTML - as one JSON file beside
* the form, itflow_fields[0], [1]... (split to fit upload_max_filesize), which a firewall
* doesn't read as fields. requestFieldsRestore() puts the fields back into $_POST before
* anything reads them, so every page and handler gets what was written as it always has, and
* ITFlow cleans it as it always has (HTMLPurifier). The file:
*
* {"v": 1, "fields": [["ticket_reply", "<p>...</p>"], ["items[3][description]", "..."], ...]}
*
* Field names are posted names - brackets and all - and read as PHP reads a posted form.
*/
const REQUEST_FIELDS_FILE = 'itflow_fields';
/** A php.ini size - 8M, 512K, 2G - in bytes. */
function requestIniBytes($value) {
$value = trim((string) $value);
$number = intval($value);
switch (strtolower(substr($value, -1))) {
case 'g':
return $number * 1024 * 1024 * 1024;
case 'm':
return $number * 1024 * 1024;
case 'k':
return $number * 1024;
}
return $number;
}
/*
* What the browser can send as files, for js/http.js's <script> tag - how many files a post
* can carry (max_file_uploads) and how big each can be (upload_max_filesize, within
* post_max_size). What doesn't fit goes as fields, as before.
*/
function requestFieldsLimitAttributes() {
$files = max(0, intval(ini_get('max_file_uploads')));
$file = requestIniBytes(ini_get('upload_max_filesize'));
$post = requestIniBytes(ini_get('post_max_size'));
$bytes = $post > 0 ? min($file, $post) : $file;
if (!filter_var(ini_get('file_uploads'), FILTER_VALIDATE_BOOLEAN)) {
$files = 0;
}
return ' data-max-files="' . $files . '" data-max-bytes="' . max(0, $bytes) . '"';
}
/*
* The fields sent as a file put back into $_POST (and $_REQUEST), as if posted as fields -
* from functions.php, before anything reads them. How many came back; 0 with none sent. A
* file that didn't arrive whole - a part turned away, or not the JSON it should be - stops the
* request, so nothing goes ahead without what was written.
*/
function requestFieldsRestore() {
$files = $_FILES[REQUEST_FIELDS_FILE] ?? null;
unset($_FILES[REQUEST_FIELDS_FILE]);
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST' || !is_array($files) || !is_array($files['tmp_name'] ?? null) || !$files['tmp_name']) {
return 0;
}
$json = '';
$count = count($files['tmp_name']);
for ($i = 0; $i < $count; $i++) {
$tmp = $files['tmp_name'][$i] ?? null;
if (!is_string($tmp) || intval($files['error'][$i] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || !is_uploaded_file($tmp)) {
requestFieldsFailed('part ' . ($i + 1) . " of $count didn't arrive (upload error " . intval($files['error'][$i] ?? -1) . ')');
}
$json .= (string) file_get_contents($tmp);
}
$sent = json_decode($json, true);
if (!is_array($sent) || ($sent['v'] ?? null) !== 1 || !is_array($sent['fields'] ?? null)) {
requestFieldsFailed("it isn't what the page sends");
}
$query = [];
foreach ($sent['fields'] as $pair) {
if (is_array($pair) && count($pair) === 2 && is_string($pair[0] ?? null) && $pair[0] !== '' && is_string($pair[1] ?? null)) {
$query[] = rawurlencode($pair[0]) . '=' . rawurlencode($pair[1]);
}
}
if (!$query) {
return 0;
}
parse_str(implode('&', $query), $fields);
$_POST = array_replace_recursive($_POST, $fields);
$_REQUEST = array_replace_recursive($_REQUEST, $fields);
return count($query);
}
/*
* A JSON post's payload, decoded - sent as itflow_json in the fields file when it holds HTML
* (itflowJsonPost(), js/http.js), else as the request's body. Null when it isn't JSON.
*/
function requestJson() {
$json = isset($_POST['itflow_json']) && is_string($_POST['itflow_json']) ? $_POST['itflow_json'] : (string) file_get_contents('php://input');
$data = json_decode($json, true);
return is_array($data) ? $data : null;
}
/* What was written didn't arrive whole - said, logged, and the request goes no further */
function requestFieldsFailed($why) {
error_log("ITFlow: a post's fields sent as a file couldn't be read - $why (" . ($_SERVER['REQUEST_URI'] ?? '') . ')');
http_response_code(400);
header('Content-Type: text/plain; charset=UTF-8');
exit("What you wrote didn't arrive whole, so nothing was saved. Go back and send it again - if it happens again, the picture or file in it may be too large for this server.");
}