Commit Graph
617 Commits
Author SHA1 Message Date
Isaac Connor 7ba565221a Merge pull request #5827 from AJ0070/api-filter-fixes
fix: API filter parsing for intervals, IS NULL, operators and ambiguous columns
2026-11-14 11:52:07 -05:00
Isaac Connor a4daebd2a6 Merge pull request #5829 from AJ0070/api-host-fixes
fix: API daemon control permission level, daemonCheck error and daemonStatus
2026-11-14 11:51:22 -05:00
Isaac Connor dd9134dcdd Merge pull request #5831 from AJ0070/api-groups-fixes
fix: API group parent loops and add/edit error responses
2026-11-14 11:50:52 -05:00
Isaac Connor 0796ccd825 Merge pull request #5833 from AJ0070/api-edit-responses
fix: API configs and zones edit responses
2026-11-14 11:50:23 -05:00
Jash 001891a5f1 fix: ignore non-string token and password request values refs #5858 2026-11-14 19:16:47 +05:30
Jash 23daf1e42c fix: validate username uniqueness and length on the User model refs #5858 2026-11-14 19:16:47 +05:30
Jash 78f591b721 fix: build the legacy plain credentials from the authenticated user refs #5858 2026-11-14 19:16:47 +05:30
Jash 0434acc1fb fix: serialize the roles index for JSON responses refs #5858 2026-11-14 19:16:47 +05:30
Jash 17daba318b fix: return the validation errors in the API zone edit response instead of printing them refs #5832 2026-11-14 18:57:29 +05:30
Jash 89a9de0e92 fix: answer the API config edit in JSON instead of an empty body or the template source refs #5832 2026-11-14 18:57:28 +05:30
Jash b98818e5bf fix: include the save result in the API group edit response refs #5830 2026-11-14 18:57:23 +05:30
Jash 3657a7e2f4 fix: answer with the validation errors instead of a 500 when an API group add fails refs #5830 2026-11-14 18:57:23 +05:30
Jash 3221599a3f fix: refuse an API group edit that puts a group under itself or its descendants refs #5830 2026-11-14 18:57:23 +05:30
Jash 27e0da25f4 fix: report a monitor daemon as running in the API only when zmdc says it is refs #5828 2026-11-14 18:57:17 +05:30
Jash 950009dd5b fix: call ZM\Error for invalid daemon characters in the API host controller and reset the count refs #5828 2026-11-14 18:57:16 +05:30
Jash accde152ed fix: let an account with System Edit use the API daemonControl check and status commands refs #5828 2026-11-14 18:57:16 +05:30
Jash 0cd93c72e8 fix: qualify the API groups index filter fields so filtering on Id is not ambiguous refs #5826 2026-11-14 18:57:10 +05:30
Jash 6aa2f9b984 fix: qualify the API events index filter fields so Id or MonitorId with a GroupId or Tag filter is not ambiguous refs #5826 2026-11-14 18:57:10 +05:30
Jash 66534da715 fix: accept IS NULL in API named filters and answer 400 for operators that always failed refs #5826 2026-11-14 18:57:10 +05:30
Jash 9e8dea5c88 fix: answer 400 for an unknown interval unit in the API events search and consoleEvents refs #5826 2026-11-14 18:57:10 +05:30
Isaac Connor 29a376ba05 Merge pull request #5580 from AJ0070/5579-api-group-edit
fix: use the Group model when an API group edit fails validation
2026-11-11 19:11:34 -05:00
Jash c9de8488d5 fix: look up an event's highest scoring frame by FrameId in the API events index fixes #5591 2026-11-12 03:57:43 +05:30
Jash dcb404e5d7 fix: use the Group model when an API group edit fails validation fixes #5579 2026-11-12 03:00:29 +05:30
Jash 90993466a4 fix: quote storage paths passed to df, du, ffprobe and AutoExecute refs #5556 2026-11-12 01:07:48 +05:30
Isaac ConnorandClaude Opus 4.8 dd2a658b31 fix: add the field allow-list to ServerStatsController too refs GHSA-xqfr-6v9x-cvwc
Completes the previous commit, which omitted this one call site.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-07 10:28:53 -05:00
Isaac ConnorandClaude Opus 4.8 71cae39ed9 fix: pass a field allow-list at every API buildFilter call site refs GHSA-xqfr-6v9x-cvwc
Follow-up to the numeric field-name guard. FilterComponent::buildFilter() takes
an optional $validFields allow-list, but only EventsController's search index
and MonitorsController (via qualifyFields) used it; the other index/search/
consoleEvents call sites passed none, so a field name was checked only by the
character class, not against a known column set.

Pass array_keys($this->Model->schema()) - the pattern EventsController already
uses - at the remaining sites across Configs, EventData, Events (three), Frames,
Groups, ServerStats, Snapshots, Storage, Tags and UserPreference, so a field
must be a real column of the queried model and an unknown field is a 400 rather
than a MySQL 500. The injection itself is already closed by the numeric
field-name guard; this is defense in depth and consistent error handling.

ServerStatsController's commented-out permission check is left as-is (clients
need the data to render monitors); only the allow-list is added there.

Add tests/php/test_api_filter_fields_enforced.php.

Reported by arpitjain099.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-07 10:27:31 -05:00
Isaac ConnorandClaude Opus 4.8 71996d18b6 fix: reject all-digit API filter field names in FilterComponent refs GHSA-xqfr-6v9x-cvwc
A named parameter written `0:<payload>` reaches FilterComponent::buildFilter()
as an integer array key, and the field regex `[a-z0-9.]+` matched the all-digit
name, so `$conditions["0"] = $value` cast the key to an int. CakePHP 2.x renders
an integer-keyed condition as a raw, unescaped SQL fragment, so the value went
into the WHERE clause verbatim (UNION exfiltration, ACL bypass via a trailing
comment, blind boolean oracle) at the index() call sites that pass no
$validFields allow-list.

Refuse an all-digit field name in buildFilter(), so every key that reaches
$conditions contains a letter or a dot and stays a string key that CakePHP
quotes. Add tests/php/test_api_filter_numeric_field.php.

Reported by arpitjain099.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-07 10:01:11 -05:00
Isaac ConnorandClaude Opus 4.8 05a610a2c6 fix: revoke API tokens when a user's password is changed
Changing a user's password rewrote only the password hash and left the
user's JWT API tokens valid: a pre-change access token kept working and a
pre-change refresh token kept minting new access tokens for up to its 24
hour lifetime, so a leaked token survived the password change meant to
cut it off.

Set TokenMinExpiry to the change time in the same step that rewrites the
hash, so validateToken() rejects every token issued before the change,
matching the options page's existing revoke action. Covered paths: the
REST API User model (beforeSave), with the self-edit controller whitelist
widened to persist the field, and the classic web UI user action for both
the administrator edit and the self edit. The login-time upgrade of a
legacy hash to bcrypt rewrites the same password and is left untouched so
it does not revoke tokens.

refs GHSA-5w6w-qp25-2qqh

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-04 08:26:38 -05:00
Nic BoetandClaude Opus 5.5 96804bca31 fix: allow CORS only for exact server and same-host origins
CORSHeaders() (web UI) and AppController::beforeFilter() (API) send
Access-Control-Allow-Origin with Access-Control-Allow-Credentials: true.
With no Servers rows and ZM_MIN_STREAMING_PORT set they reflected any
Origin, and with Servers rows they matched the Origin against each
server's Hostname and Name with a regex anchored only at the start, so
https://zm.example.com.evil.net was accepted for zm.example.com.

Both now call corsAllowedOrigin() in Network.php, which accepts an
Origin of the form scheme://host[:port] whose host equals a server's
Hostname or Name, or, without Servers rows and with streaming ports,
the host this request was made to. Any port is accepted, so multi-port
streaming keeps working. Same-origin and unlisted origins get no CORS
headers, as before; the web UI still warns about unlisted origins only
when Servers rows exist.

Tests: tests/php/test_cors_allowed_origin.php

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 17:21:45 -05:00
Nic BoetandClaude Opus 5.5 d4655c1137 fix(api): accept a bare monitor filter field that one joined table has
qualifyFields() required a bare field to be a Monitors column, so names
only a joined table has, such as Status, CaptureFPS and AnalysisFPS from
Monitor_Status, answered 400 where they used to filter:

  GET /api/monitors/index/Status:Connected.json   -> 400 Unknown filter field

A bare field that Monitors has is still written Monitor.Field. Otherwise
it must be a column of exactly one joined table and is left bare, as
before, for MySQL to resolve; buildFilter() does not accept an alias with
an underscore such as Monitor_Status, so it cannot be qualified. A bare
field no table has stays a 400, and one two joined tables have and
Monitors does not (MonitorId, in Monitor_Status and Event_Summaries) is a
400 "Ambiguous filter field" instead of a 500 from MySQL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 11:31:26 -05:00
Nic BoetandClaude Opus 5.5 48180ae304 fix(api): answer 400 on a bad interval or field in events search
EventsController::search() and consoleEvents() threw a plain Exception
for an interval or field name they could not parse, which is a 500.
With the generic message in 5xx bodies the client no longer learned
what was wrong. Throw BadRequestException, as FilterComponent does
since 028ccecb5: a 400 that names the bad value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 11:31:26 -05:00
Nic BoetandClaude Opus 5.5 7fffa186f6 fix(api): check monitor filter fields, qualify them with the model
MonitorsController::index() passed its named parameters to
FilterComponent::buildFilter() without a field list, so any name made
of letters, digits and dots reached MySQL as a column:

  GET /api/monitors/index/Nope:1.json   -> 500, PDOException 42S22

and the bare names Id and Name, which the joined Manufacturers and
Models tables have too, failed as ambiguous columns (also 500).

FilterComponent::qualifyFields() checks each field against the columns
of the queried model and of the models find() joins, and writes it
Model.Field: a bare field is a Monitors column. GroupId stays bare for
the Groups_Monitors join. Anything else is a 400 "Unknown filter field",
as events/index answers since 028ccecb5. Column names match without
regard to case and are written as the schema spells them.

The web UI's map (Deleted:0) keeps working;
Name:x, Id >:0, Name LIKE:x and Id IN:1,2 now return monitors instead
of a 500.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 11:31:26 -05:00
Nic BoetandClaude Opus 5.5 66a1267b42 fix(api): keep exception text and credentials out of 5xx bodies
With debug 0, Cake replaces a 500's message with a generic one, but
Crud's exception renderer adds data.exception.message from the raw
exception. A PDOException (e.g. monitors/index/<unknown column>:1.json)
returned the SQL error and its fragment of the query to the client.
data.url also echoed the request URL with its token or password.

ZmExceptionRenderer extends Crud's renderer: with debug 0 a 5xx body
carries the generic message in data.exception.message (class and code
are kept), and data.url has credential values redacted.
AppController::beforeFilter() swaps it in for the renderer Crud's
ApiListener registers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 11:31:26 -05:00
Isaac Connor c40dfeaa1d Merge pull request #5351 from connortechnology/5348-alarm-cause-nits
fix: preserve a UTF-8 alarm cause and accept a literal "0" refs #5348
2026-10-31 19:57:58 -04:00
Isaac Connor cd6e262dc5 Merge pull request #5402 from connortechnology/fix/ghsa-3m5x-events-view-acl
fix: API events/view refuses events on monitors the user may not view (GHSA-3m5x-32ch-gg29)
2026-10-31 19:53:10 -04:00
Nic Boet 7a64e50618 fix(api): refuse events/view for an event on a monitor the user may not view
EventsController::view() looks the event up with the per-monitor
condition and then asks ZM\Event::canView(). For an event on a denied
monitor the restricted find() is empty, the empty ZM\Event has no
monitor, and canView() falls back to the user's global Monitors
permission, so it passes. The action then answered 200 with a stub
built from the id alone: the hidden event's Next/Prev ids, its max
score and alarm frame ids, fileSize and a FileSystemPath.

The event is known to exist at that point, so an empty result means
the monitor condition removed it: answer 401 as edit, delete and
archive do.

Test: tests/php/test_api_event_view_acl.php (2 of 6 checks fail
without the fix).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit cf62c05576)
2026-10-31 19:08:08 -04:00
Isaac Connor ab259c7495 Merge pull request #5399 from connortechnology/fix/ghsa-6h3c-api-associated-ids
fix: API group, snapshot and tag edits check the links they save (GHSA-6h3c-f3p7-pq4v)
2026-10-31 19:05:16 -04:00
Nic Boet 48a11c1bb7 fix: snapshot and tag edits keep the event links the editor can't view
An edit replaces all of a snapshot's or tag's event links, but only the
new list was checked, so {"Event": []} from a user denied a monitor
unlinked that monitor's events, and any list without them dropped them.
The API's view of a snapshot or tag leaves those events out, so a client
that reads the list, changes it and saves it dropped them too.

The new list still needs view on every event it links. The links to
events the user may not view are now added back to the list before the
save (eventLinksToSave()), so the user can change the links they see and
the others stay. A link to an event that no longer exists is dropped.

Test: tests/php/test_api_associated_ids.php
(cherry picked from commit 6b1d0eaf8d)
2026-10-31 18:45:52 -04:00
Nic Boet 959b47f67d fix: check the group and event links an API save will write
Groups, Snapshots and Tags add/edit checked the ids from MonitorIds or
EventIds when the request had them, but Model::save() links the HABTM
entry (Monitor, Event) and ignores those fields on edit. A Groups=Edit
user could send {"Group":{"MonitorIds":"<current members>"},"Monitor":[]}:
the membership check saw no monitor moved, the save emptied the group and
lifted the Groups_Permissions deny on its monitors. On add, an empty
MonitorIds or EventIds next to a Monitor/Event list let monitors the user
may not edit into a new group, and events the user may not view into a
snapshot.

requestAssociatedIds() now reads the list the way save() does and
rewrites the request's entry to that plain list of ids, so a row given as
a join record links only its MonitorId/EventId. add() copies MonitorIds
or EventIds into the entry before the check, as it did before the save.

Test: tests/php/test_api_associated_ids.php
(cherry picked from commit 6bf015530e)
2026-10-31 18:45:52 -04:00
Isaac Connor 077ffce1a1 Merge commit from fork
fix: make event videos only for events the user can view (GHSA-rmf7-q643-w8vm)
2026-10-31 18:42:59 -04:00
Isaac Connor 0ad172d989 Merge pull request #5387 from connortechnology/fix/ghsa-qrhf-login-token
fix: refuse host/login with both a password and a token (GHSA-qrhf)
2026-10-31 10:59:17 -04:00
Nic Boet 60880b1c91 fix: refuse host/login with both a password and a token
beforeFilter() authenticates user=/pass= (or username=/password=) and
then a token=, keeping the token's user. login() mints a refresh token
for that user whenever a password was sent, so a caller's own password
plus another user's refresh token gave a new 24 h refresh token for the
other user, renewing a token that is otherwise never renewed. A login now
takes one or the other. refs GHSA-m77q-66v7-j3fq

Test: tests/php/test_api_login_token.php
(cherry picked from commit 4223c12dad28b1ff1ff8738a6709bae6dc74f1bd)
2026-10-31 10:46:05 -04:00
Isaac Connor ac1e167e6e Merge pull request #5382 from connortechnology/fix/ghsa-2pgc-session-fixation
fix: reject client-chosen session ids and regenerate on auth= / stateful API login (GHSA-2pgc)
2026-10-31 09:34:50 -04:00
Nic Boet 8e491bbdfe fix: a stateful API request gets a new session id whenever the session takes on a user
The stateful branch regenerated the session id only on a password login.
A stateful=1 request with an access token (any action) or a refresh
token (host/login) wrote the user into the session id the client
brought. Any anonymous request stores a session id, so use_strict_mode
did not stop a planted one (CWE-384). Regenerate whenever the session
does not already hold this user, as the password login does.

(cherry picked from commit 657742ec5d)
2026-10-31 09:04:37 -04:00
Nic Boet 2985fb9154 fix: new session id when an auth= or stateful API login takes a session
An auth= login (ZM_AUTH_HASH_LOGINS) and the API's stateful login
(stateful=1 with user/pass, legacy API auth) wrote the user into the
session the client arrived with, keeping its id. With an id planted in
the client beforehand, the attacker's cookie became the victim's
session. The login form already issues a new id.

Both now call zm_session_regenerate_id_login() first: auth= when a
session the client carries a cookie for changes to another user, the
API whenever credentials log in. The API's cookie path no longer logs
the whole session.

The API's stateful login also drops $_SESSION['password'] =
$_REQUEST['password']. This is not a behaviour change: API logins send
pass=, not password=, so under ZM_AUTH_RELAY=plain the line stored
nothing for them. A stateful request that logs in with username= and
password= goes through zm_authenticate_request()'s form-login path,
which stores the password itself, as before. zmu gets its credentials
from zmuCredentials() (the zmu token change, which this one follows).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit bb3bd92b92)
2026-10-31 09:04:37 -04:00
Isaac Connor daf4fcbfda Merge pull request #5379 from connortechnology/3qq8-zmu-token
fix: give zmu a short-lived token instead of reusable credentials (GHSA-3qq8-fq8j-rx7j)
2026-10-30 23:59:22 -04:00
Nic Boet 46ccb7529f fix: without a token secret, zmu still gets an API request's own password
When ZM_AUTH_HASH_SECRET is unusable zmu refuses tokens, so
zmuCredentials() falls back to what zmu got before. With
ZM_AUTH_RELAY=plain the API's alarm action used to pass the password
from the request (pass= or password=); the fallback read only the
session, which an API call without a stateful login doesn't have, so
zmu refused the call (rc 255). zmuCredentials() takes the caller's
password first again. With a usable secret it is never used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 5ca19c2703ea9211fe040991305ce9b0a873faff)
2026-10-30 19:30:28 -04:00
Nic Boet 35e36e4dda fix: give zmu a short-lived token instead of the password or auth hash
getZmuCommand() and the API's alarm action put the user's credentials on
zmu's command line, where every local user can read them (ps,
/proc/<pid>/cmdline): an auth hash with the default hashed relay, the
plaintext password with ZM_AUTH_RELAY=plain. The API also wrote the
whole command to the debug log. With the default ZM_AUTH_HASH_IPS=0 that
hash is accepted as an auth= login from any address for ZM_AUTH_HASH_TTL
hours.

zmuCredentials() builds them in one place: with a usable
ZM_AUTH_HASH_SECRET, an access token for the user that expires after a
minute (zmu -T, accepted with every relay). Without the secret zmu
refuses tokens, so it gets what it did before. The API logs the command
without the credentials.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 32bc2b3fee87b752590ba6f210403afd9222d9ff)
2026-10-30 19:30:27 -04:00
Nic Boet 8dbc2d977e fix(api): create the API's log files readable by owner and group only
cake_error.log, error.log, cake_debug.log and debug.log in ZM_LOGDIR
were created 0644 under the usual umask of 022, so any local
account could read them. Before the redaction in this series they held
the token, auth hash or password of any API request that failed.

The three FileLog configs now set 'mask' => 0640. Cake applies it only
when it creates a file: on the first write, and after its own 10 MB
rotation. An existing file keeps its mode.

Behaviour change: new files are 0640, owned by the user PHP runs as and
its primary group (www-data:www-data on Debian/Ubuntu, apache:apache on
Red Hat/Fedora), so only that user, members of that group and root can
read them. The Debian packages chown /var/log/zm to www-data:root, which
does not change the group of the files PHP creates in it. Nothing in
ZoneMinder reads these files: the web log view and zmaudit use the Logs
table. The distros' logrotate configs run as root; with the usual
global "create" a rotated file is recreated with the same mode and
owner, otherwise Cake recreates it with the mask.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 48d5fad41d)
2026-10-30 18:53:27 -04:00
Nic Boet 8359958068 fix(api): redact credentials sent as arrays, keep empty values visible
ZmErrorHandler::redact() matched token=, auth=, pass= and password=
only. A client that sends one of them as an array (token[]=x, which
Cake logs as token%5B0%5D=x) still puts the value in the log, so the
name may now carry [..] or %5B..%5D before the =.

An empty value is no longer replaced: token= stays token=, so the log
still shows a client that sent no credential.

Tests: prefixes (authority, tokenize, password2, pass_hint, token_type,
passwd) are not touched, arrays are redacted, empty stays empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 4789df0a04)
2026-10-30 18:53:27 -04:00