diff --git a/CHANGELOG.md b/CHANGELOG.md index c0dc12d64..0e5b5b51d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,11 @@ This file documents all notable changes made to ITFlow. ### Upgrading -Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's three database changes (3.6.6 to 3.6.9) in the same pass. They add four tables for RMM providers and their alerts, and a setting for device actions, and change nothing that is there. Nothing else is required. This release has three security fixes - see Security. +Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's four database changes (3.6.6 to 3.7.0) in the same pass. They add four tables for RMM providers and their alerts, a setting for device actions and one for API keys in the address, and change nothing that is there. Nothing else is required. This release has four security fixes - see Security. ### Security -- API: send your API key in the `X-API-Key` header. A key put in the address (`?api_key=`) was written to your web server's access log with every call, and to a proxy's in front of it - a header isn't. The address and a JSON body still work, so nothing that uses the API breaks, but move your scripts and integrations to the header, then give each one that sent its key in the address a new key and revoke the old one, as the old logs still have it. The API docs now send the key in the header throughout, and the New API Key window says where it goes. +- API: send your API key in the `X-API-Key` header. A key put in the address (`?api_key=`) was written to your web server's access log with every call, and to a proxy's in front of it - a header isn't. The address and a JSON body still work, so nothing that uses the API breaks, but move your scripts and integrations to the header, then give each one that sent its key in the address a new key and revoke the old one, as the old logs still have it. The API docs now send the key in the header throughout, and the New API Key window says where it goes. Admin > API Keys flags each key sent in the address in the last 30 days, and Admin > Settings > Security has Take API keys in the address - on if your ITFlow had API keys that still worked, so nothing stops when you update, and off on a new ITFlow. Once the flags have gone, turn it off, and a key in the address is turned away from then on. - Credentials: a login saved with a piece of web page code as its username or password could run that code in the browser of an agent who followed a link made for the purpose. The address ITFlow's own pages use to fetch a login's username and password answered as a web page, so a browser sent straight to it drew what the login held instead of showing it as text - and the code then acted as that agent. It took someone able to get such a login saved - an agent who can edit credentials, or a person sent an Ask for a login link, once an agent had saved what they sent back - and then an agent with access to that login opening the link while signed in. The pages that list and show logins were never affected. That answer now says it is data, which no browser draws as a page, and so do sixteen like it that carried only text ITFlow had already made safe. Releases from 26.10 to 26.11.8 are affected, and there is nothing to do after updating. - Invites: with a login key required, the page behind an agent's invite link went on showing the sign-in address with the key in it after the invite had been cancelled or had run out. Someone whose invite was withdrawn could still read the key from their old link. The key signs no one in by itself - it only opens the agents' sign-in page, which still asks for an email and password. A cancelled or run-out link now leads to the plain sign-in page. Releases from 26.11 to 26.11.8 are affected. If you cancelled an invite because the person should never have had it, change the login key in Admin > Settings > Security. - Client access: an agent limited to some clients could read a few names belonging to the others, by putting a record's number in a page's address - a project's name on the tickets list, a site's name, a ticket's subject and an invoice's number on Trips and its export window, a vendor's name in the domains export window, and a client's name on Update Prices and in the message when an AI request was turned away. Nothing else of those records showed, and nothing could be changed. These names now show only for clients the agent can open. Releases from 26.09 to 26.11.8 are affected. @@ -110,6 +110,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u - Picture fields: `pictureField($name, $remove_name, $current, $empty, $opts)` (`functions/picture.php`) renders the picture as a button with a camera badge, a hidden file input named `$name` and a hidden `$remove_name` input; `js/picture_field.js` opens the Picture window over the form's own (the window stacking in `js/app.js`), crops on a canvas and puts the crop in the file input through `DataTransfer` - a JPG for a JPG, else WebP, PNG where the browser can't write WebP - and sets the remove input to 1 on Remove, so the forms post the same fields as before and no handler changed. Options: `round`, `shapes`, `max`, `class` (the new picture's ``), `box` and `name` (the field the initials follow). `picturePhoto()` and `picturePhotoEmpty()` are a photo's two looks. Used on the profile, Admin > Users, contacts, assets, locations and racks, replacing their file inputs, remove boxes and preview scripts. A browser without `DataTransfer` gets the file picker and the file as picked. Free crops: `shapes => 'free'` gives a frame with corner and edge handles over the whole picture instead of the fixed-shape crop, `trim` adds Trim the edges (transparent, or within a small distance of the corner colour, found on a downscaled probe), `accept` sets the file types - with `application/pdf` a PDF is used as it is - and `submit` sends the form once a picture is used; `box` adds `tall` (a receipt) and `logo`, and `pictureFile()` is a PDF's look. A PNG source stays PNG when the field doesn't take WebP, so a logo keeps its transparency. Used on the expense form and quick look (`expense_receipt_save` is now posted as a hidden field), Admin > Settings > Company details and the setup wizard's company and account steps; `js/setup.js` loses its `data-setup-image` preview and checks, and `.itflow-setup-preview` is gone. - `cron/ticket_email_parser.php` no longer writes its run summary to App Logs - it echoes the counts and the time for a run from the command line - and ends with `cronJobStop("No mailbox is set up to read - see Admin > Settings > Mail")` when `config_imap_provider` is empty instead of logging. `cron/mail_queue.php` does the same with no `config_smtp_provider`. `cron/mail_queue.php`, `ticket_sla.php`, `domain_refresher.php` and `certificate_refresher.php` stop with `config_enable_cron` off without a `logApp()` error, as the parser has since 26.11.4. - `apiKeyFromRequest()` (`functions/api_keys.php`) reads the key an API call came with for `api/v1/validate_api_key.php` - the `X-API-Key` header (`HTTP_X_API_KEY`), else the JSON body's `api_key`, else the query string's; one that's empty or not a string counts as none, a 401 with an empty body. The key was also escaped twice on its way in; it's escaped once now. +- Database 3.7.0: `api_keys.api_key_address_at`, when a key last came in a call's address (`apiKeyNoteAddress()`, at most once a minute), and `settings.config_api_key_in_address` - on where a key that hadn't expired was there at the update, off otherwise and in `db.sql`. `apiKeyInAddress()` says whether a call's query string has an `api_key`. With `apiKeyInAddressAccepted()` false (read guarded, so true before the update), `api/v1/validate_api_key.php` answers a working key in the address with a 401 and a message, and logs it as an `API` `Failed` against the key without counting it toward `apiKeyWatch()` - even when a header sends the key too, as the address is logged all the same. Admin > API Keys reads `api_key_address_at` for its flag and note, and Admin > Settings > Security saves the switch. ## [26.11.8] diff --git a/admin/api_keys.php b/admin/api_keys.php index 374f785c5..fdeea5d7a 100644 --- a/admin/api_keys.php +++ b/admin/api_keys.php @@ -6,7 +6,8 @@ * they see, whether the key works (it expires, and an agent who is * disabled, archived or has no role turns every call away), when it was * last used and from where, and its calls over the last 30 days - each - * opening what it was used for (modals/api/api_key_usage.php). A revoked key + * opening what it was used for (modals/api/api_key_usage.php), and a key sent + * in a call's address, where the web server's access log keeps it. A revoked key * has expired, so it moves to Expired, where it can be deleted. The work is in functions/api_keys.php; the handlers are * admin/post/api_keys.php. */ @@ -34,7 +35,7 @@ if (!isset($sortable_columns[$sort])) { $today = date('Y-m-d'); $search = "(api_key_name LIKE '%$q%' OR user_name LIKE '%$q%')"; -$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS api_key_id, api_key_name, api_key_secret, api_key_created_at, api_key_expire, api_key_user_id, api_key_last_used_at, +$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS api_key_id, api_key_name, api_key_secret, api_key_created_at, api_key_expire, api_key_user_id, api_key_last_used_at, api_key_address_at, user_name, user_avatar, user_type, user_status, user_archived_at, user_role_id FROM api_keys LEFT JOIN users ON user_id = api_key_user_id @@ -53,6 +54,11 @@ $access = userClientAccess(array_filter(array_map('intval', array_column($keys, // What each is used for, the last 30 days (functions/api_keys.php) $usage = apiKeyUsageSummary(array_keys($keys)); +// Keys that still work and came in a call's address lately - what to move to the header (functions/api_keys.php) +$address_since = date('Y-m-d H:i:s', strtotime('-30 days')); +$address_keys = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM api_keys WHERE api_key_expire > '$today' AND api_key_address_at > '$address_since'"))[0]); +$address_taken = apiKeyInAddressAccepted(); + $counts = mysqli_fetch_row(mysqli_query($mysqli, "SELECT COALESCE(SUM(api_key_expire > '$today'), 0), COALESCE(SUM(api_key_expire <= '$today'), 0) FROM api_keys LEFT JOIN users ON user_id = api_key_user_id WHERE $search")); @@ -72,6 +78,22 @@ $csrf = $_SESSION['csrf_token']; + + +
+ +
+ key sent in the address in the last 30 days, as ?api_key=, so your web server's access log has . + + +
+ + Security settings + +
+ +
@@ -135,6 +157,10 @@ $csrf = $_SESSION['csrf_token']; if ($use && $use['last_ip'] !== '') { $last_used .= '
' . escapeHtml($use['last_ip']) . '
'; } + if ($row['api_key_address_at'] && $row['api_key_address_at'] > $address_since) { + $last_used .= '
' + . 'In the address ' . escapeHtml(timeAgo($row['api_key_address_at'])) . '
'; + } $usage_url = "modals/api/api_key_usage.php?id=$api_key_id"; ob_start(); ?> diff --git a/admin/database_updates/3.7.0.php b/admin/database_updates/3.7.0.php new file mode 100644 index 000000000..d1c049b62 --- /dev/null +++ b/admin/database_updates/3.7.0.php @@ -0,0 +1,31 @@ + API Keys flags it + * settings.config_api_key_in_address - whether the API takes a key there at all (Admin > Settings > + * Security). On where a key that still works was here already, so nothing using one breaks; + * off on every other install, and on new ones + * + * Re-runnable: each column is made only where it isn't there yet, and the setting is only set + * when its column is made. + */ + +defined('FROM_DB_UPDATER') || die("Direct file access is not allowed"); + + $itflow_column_exists = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'api_keys' AND COLUMN_NAME = 'api_key_address_at' LIMIT 1"); + if (!$itflow_column_exists || mysqli_num_rows($itflow_column_exists) === 0) { + mysqli_query($mysqli, "ALTER TABLE `api_keys` ADD `api_key_address_at` datetime DEFAULT NULL AFTER `api_key_last_used_at`"); + } + + $itflow_column_exists = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'settings' AND COLUMN_NAME = 'config_api_key_in_address' LIMIT 1"); + if (!$itflow_column_exists || mysqli_num_rows($itflow_column_exists) === 0) { + mysqli_query($mysqli, "ALTER TABLE `settings` ADD `config_api_key_in_address` tinyint(1) NOT NULL DEFAULT 0 AFTER `config_login_alert_failures`"); + mysqli_query($mysqli, "UPDATE `settings` SET `config_api_key_in_address` = 1 WHERE EXISTS (SELECT 1 FROM `api_keys` WHERE `api_key_expire` > CURDATE())"); + } diff --git a/admin/post/settings_security.php b/admin/post/settings_security.php index 4ae07a04b..c4d3596fe 100644 --- a/admin/post/settings_security.php +++ b/admin/post/settings_security.php @@ -3,8 +3,8 @@ /* * Admin > Settings > Security (admin/settings_security.php): * edit_security_settings saves the sign-in page's message and login key, how - * long a browser is remembered, when admins hear of failed sign-ins, and - * shared links. A login key required with none set - which would leave + * long a browser is remembered, when admins hear of failed sign-ins, whether + * the API takes a key in the address, and shared links. A login key required with none set - which would leave * agents no way in but the database - is refused, as is a key that couldn't * be typed into an address, or a time out of range. Nothing is saved then. * A choice not offered settles to the usual one. @@ -17,7 +17,7 @@ if (isset($_POST['edit_security_settings'])) { validateCSRFToken(); $before = settingsCurrent(['config_login_message', 'config_login_key_required', 'config_login_key_secret', 'config_login_remember_me_expire', - 'config_login_alert_failures', 'config_share_max_hours', 'config_share_require_protection']); + 'config_login_alert_failures', 'config_api_key_in_address', 'config_share_max_hours', 'config_share_require_protection']); // What was sent, and what is saved for anything that wasn't - a switch not sent is off $in = $_POST + array_map(function ($value) { return (string) $value; }, $before); @@ -28,6 +28,7 @@ if (isset($_POST['edit_security_settings'])) { 'config_login_key_secret' => trim((string) $in['config_login_key_secret']), 'config_login_remember_me_expire' => intval($in['config_login_remember_me_expire']), 'config_login_alert_failures' => intval($in['config_login_alert_failures']), + 'config_api_key_in_address' => empty($_POST['config_api_key_in_address']) ? 0 : 1, 'config_share_max_hours' => intval($in['config_share_max_hours']), 'config_share_require_protection' => empty($_POST['config_share_require_protection']) ? 0 : 1, ]; diff --git a/admin/settings_security.php b/admin/settings_security.php index bbc94ae05..ea4325181 100644 --- a/admin/settings_security.php +++ b/admin/settings_security.php @@ -3,7 +3,8 @@ /* * Admin > Settings > Security - the sign-in page (its message, and a login * key agents need in its address), how long two-step sign-in remembers a - * device, when admins hear of failed sign-ins, and shared links. Saved by + * device, when admins hear of failed sign-ins, whether the API takes a key in + * the address, and shared links. Saved by * edit_security_settings (admin/post/settings_security.php). */ @@ -11,6 +12,7 @@ require_once "includes/inc_all_admin.php"; // Read here rather than with every page's settings, so a page still opens before the update that adds it $config_login_alert_failures = signInAlertLimit(); +$config_api_key_in_address = apiKeyInAddressAccepted() ? 1 : 0; $login_url = rtrim('https://' . preg_replace('#^https?://#', '', (string) $config_base_url), '/') . '/login.php?key='; $login_key_shown = (string) settingsValue('config_login_key_secret', $config_login_key_secret); @@ -47,6 +49,10 @@ echo settingsPageStart('security'); 'help' => 'Each admin gets a notification and an email.']) ?> + + 'Off, a key in the address is turned away. API Keys flags each key that still comes that way - move them to the header first.']) ?> + + 'fa-hourglass-half', 'help' => 'The longest an agent can pick - a link stops working then, or sooner.']) ?> diff --git a/api/v1/validate_api_key.php b/api/v1/validate_api_key.php index eb8345ece..243322a93 100644 --- a/api/v1/validate_api_key.php +++ b/api/v1/validate_api_key.php @@ -105,6 +105,26 @@ if (isset($api_key)) { $api_key_decrypt_hash = $row['api_key_decrypt_hash']; // No sanitization $api_key_user_id = intval($row['api_key_user_id']); + // A key in the address is in the web server's access log: Admin > API Keys flags it, and + // once Admin > Settings > Security stops taking keys there, the call is turned away + if (apiKeyInAddress()) { + apiKeyNoteAddress($row['api_key_id']); + if (!apiKeyInAddressAccepted()) { + $url_path = escapeSql(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)); + $refused_description = escapeSql("API key " . $row['api_key_name'] . " sent in the address, which ITFlow doesn't take") . " (endpoint: $url_path)"; + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'API', log_action = 'Failed', log_description = '$refused_description', log_ip = '$ip', log_user_agent = '$user_agent', + log_entity_id = " . intval($row['api_key_id'])); + logForwardQueue('audit', mysqli_insert_id($mysqli)); + + $return_arr['success'] = "False"; + $return_arr['message'] = "This ITFlow doesn't take API keys in the address, where web server logs keep them - send the key in the X-API-Key header."; + + header(WORDING_UNAUTHORIZED); + echo json_encode($return_arr); + exit(); + } + } + // When it was last used, for Admin > API Keys - at most once a minute, not every call mysqli_query($mysqli, "UPDATE api_keys SET api_key_last_used_at = NOW() WHERE api_key_id = " . intval($row['api_key_id']) . " AND (api_key_last_used_at IS NULL OR api_key_last_used_at < NOW() - INTERVAL 1 MINUTE)"); diff --git a/db.sql b/db.sql index ea3475a2c..de2aaa938 100644 --- a/db.sql +++ b/db.sql @@ -194,6 +194,7 @@ CREATE TABLE `api_keys` ( `api_key_expire` date NOT NULL, `api_key_user_id` int(11) NOT NULL DEFAULT 0, `api_key_last_used_at` datetime DEFAULT NULL, + `api_key_address_at` datetime DEFAULT NULL, PRIMARY KEY (`api_key_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; /*!40101 SET character_set_client = @saved_cs_client */; @@ -3399,6 +3400,7 @@ CREATE TABLE `settings` ( `config_share_max_hours` int(11) NOT NULL DEFAULT 720, `config_share_require_protection` tinyint(1) NOT NULL DEFAULT 0, `config_login_alert_failures` int(11) NOT NULL DEFAULT 10, + `config_api_key_in_address` tinyint(1) NOT NULL DEFAULT 0, `config_app_log_retention` int(11) NOT NULL DEFAULT 90, `config_log_forward_enabled` tinyint(1) NOT NULL DEFAULT 0, `config_log_forward_host` varchar(200) DEFAULT NULL, diff --git a/functions/api_keys.php b/functions/api_keys.php index 7930cc7e3..5338d9f32 100644 --- a/functions/api_keys.php +++ b/functions/api_keys.php @@ -34,6 +34,42 @@ function apiKeyFromRequest() { return ''; } +/* Did this call come with a key in its address (?api_key=)? Then the web server's access log has it. */ +function apiKeyInAddress() { + return is_string($_GET['api_key'] ?? null) && trim($_GET['api_key']) !== ''; +} + +/* + * Whether the API still takes a key in the address - Admin > Settings > Security. Installs + * that had keys when the setting came (database 3.7.0) do, so nothing using them broke; others + * don't. Read here rather than with every page's settings, so the API still answers before the + * update that adds it - taking it, as it always did. + */ +function apiKeyInAddressAccepted() { + global $mysqli; + static $accepted = null; + if ($accepted === null) { + try { + $row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_api_key_in_address FROM settings WHERE company_id = 1")); + $accepted = !empty($row[0]); + } catch (Throwable $e) { + $accepted = true; + } + } + return $accepted; +} + +/* Note that a key came in the address, for Admin > API Keys to flag - at most once a minute, not every call. */ +function apiKeyNoteAddress($api_key_id) { + global $mysqli; + try { + mysqli_query($mysqli, "UPDATE api_keys SET api_key_address_at = NOW() WHERE api_key_id = " . intval($api_key_id) . " + AND (api_key_address_at IS NULL OR api_key_address_at < NOW() - INTERVAL 1 MINUTE)"); + } catch (Throwable $e) { + // Before the update that adds it + } +} + /* Has this key expired? It works until the start of its expiry date. */ function apiKeyExpired(array $row) { return (string) $row['api_key_expire'] <= date('Y-m-d'); diff --git a/functions/settings.php b/functions/settings.php index a963355a3..1abb1eaa3 100644 --- a/functions/settings.php +++ b/functions/settings.php @@ -56,7 +56,7 @@ function settingsPages() { 'notification' => ['group' => 'email', 'title' => 'Notifications', 'icon' => 'fa-bell', 'url' => 'settings_notification.php', 'about' => 'Which alerts ITFlow raises, and which emails and texts clients get.'], 'security' => ['group' => 'access', 'title' => 'Security', 'icon' => 'fa-shield-alt', 'url' => 'settings_security.php', - 'about' => 'The sign-in page, two-step sign-in, failed sign-ins and shared links.'], + 'about' => 'The sign-in page, two-step sign-in, failed sign-ins, API keys and shared links.'], 'client_portal' => ['group' => 'access', 'title' => 'Client portal', 'icon' => 'fa-user-shield', 'url' => 'settings_client_portal.php', 'module' => 'portal', 'about' => 'How your clients\' contacts sign in to the portal, and the announcements they see there.'], 'identity' => ['group' => 'access', 'title' => 'Single sign-on', 'icon' => 'fa-fingerprint', 'url' => 'identity_providers.php', 'module' => 'portal', @@ -262,6 +262,7 @@ function settingsFields() { 'config_login_key_secret' => ['page' => 'security', 'section' => 'The sign-in page', 'label' => 'Login key', 'quiet' => true, 'words' => 'login key secret url address'], 'config_login_remember_me_expire' => ['page' => 'security', 'section' => 'Two-step sign-in', 'label' => 'Remember a browser for', 'words' => '2fa mfa remember me days trusted device'], 'config_login_alert_failures' => ['page' => 'security', 'section' => 'Failed sign-ins', 'label' => 'Tell admins', 'kind' => 'select', 'options' => 'sign_in_alerts', 'words' => 'failed login brute force lockout alert'], + 'config_api_key_in_address' => ['page' => 'security', 'section' => 'API keys', 'label' => 'Take API keys in the address', 'words' => 'api key address url query string api_key header x-api-key access log'] + $on_off, 'config_share_max_hours' => ['page' => 'security', 'section' => 'Shared links', 'label' => 'Longest a shared link can last', 'kind' => 'select', 'options' => 'share_max', 'words' => 'share link expiry guest'], 'config_share_require_protection' => ['page' => 'security', 'section' => 'Shared links', 'label' => 'Shared passwords need a passphrase or code', 'words' => 'share passphrase code protect'] + $on_off,