Commit Graph
96 Commits
Author SHA1 Message Date
Isaac Connor adc36852b4 Merge pull request #7328 from AJ0070/fix-api-search-array-param
fix: API events index applies the DateTime and StartTime/EndTime filters to query-string parameters too
2026-11-21 20:59:28 -05:00
Jash 65b55416a8 fix: API events index applies the DateTime and StartTime/EndTime filters to query-string parameters too refs #7327 2026-11-22 06:04:32 +05:30
Jash 1f4b1117f6 fix: API events index applies sort and direction when the request has no limit or page refs #7157 2026-11-21 23:03:52 +05:30
Jash 8e7f937bf8 fix: API edit for events, servers, storage and four others only saves for POST or PUT refs #6918 2026-11-20 08:07:21 +05:30
Isaac Connor 5602b67173 Merge pull request #6873 from AJ0070/fix-api-events-groupid-operator
fix: API events index joins groups for GroupId filters with an operator
2026-11-19 19:36:13 -05:00
Isaac Connor ebb9976caf Merge pull request #6867 from AJ0070/fix-api-events-add-json
fix: API event add answers JSON with the new id, and the errors on failure
2026-11-19 19:22:13 -05:00
Jash 0c6521b1b7 fix: API events index joins groups for GroupId filters with an operator refs #6872 2026-11-19 18:27:37 +05:30
Jash 06514e8567 fix: API event archive reports a failed write instead of answering archived refs #6868 2026-11-19 17:27:33 +05:30
Jash b6e37b24c3 fix: API event add answers JSON with the new id, and the errors on failure refs #6866 2026-11-19 17:27:13 +05:30
Isaac Connor 940b88fbf8 Merge pull request #6576 from AJ0070/fix-api-event-delete-archived
fix: API archived event delete returns 400
2026-11-17 08:18:00 -05:00
Jash 35b7d67921 fix: API refuses to delete an archived event with a 400 refs #6575 2026-11-17 10:06:19 +05:30
Jash 315150f0f3 fix: API events index ignores legacy auth query parameters refs #6567 2026-11-17 10:02:52 +05:30
Isaac Connor bc9cb91c2f Merge pull request #6094 from singhharsh1708/fix/api-event-delete-500
fix: answer a successful API event delete with Deleted instead of a 500
2026-11-15 21:11:08 -05:00
singhharsh1708 4b12d8b7b3 fix: answer a successful API event delete with Deleted instead of a 500 2026-11-16 02:28:52 +05:30
singhharsh1708 2910081a92 fix: limit the API event view Next and Prev to events on monitors the user may view 2026-11-16 02:28:51 +05:30
Isaac Connor 2a12d5a0ec Merge pull request #5978 from AJ0070/api-filter-in-values
fix: API IN values, events limit/page validation and default order
2026-11-15 11:26:11 -05:00
Jash 8924411e68 fix: answer API deletes with a JSON message and a 500 on failure instead of an empty 200 refs #5981 2026-11-15 13:22:49 +05:30
Jash ab5f1bca2d fix: parse API IN:(a,b) values, validate events limit and page, and order the events index by Id refs #5977 2026-11-15 12:58:02 +05:30
Isaac Connor 40ada52b42 Merge pull request #5835 from AJ0070/api-input-validation
fix: validate API input against the column types before saving
2026-11-14 11:59:38 -05:00
Jash 66619319d9 fix: validate number, date and enum values in API saves so a bad value is a field error and not a 500 refs #5834 2026-11-14 18:57:35 +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 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
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
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
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 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
Nic Boet 1d2ae06388 fix(api): count the user's role in the Events permission checks
The events, frames, event_data and tags controllers compared the user's own
Events field, so a user granted Events=View or Edit only through their role
was refused every call, while the web UI's canView('Events') and
canEdit('Events') accept the role. Use ZM\User::canView() and canEdit().
Per-monitor limits are applied as before.

refs #5189
2026-10-21 19:06:25 -05:00
Isaac ConnorandClaude Opus 5.5 1ba42e8710 fix: apply the per-monitor ACL throughout the Events API
- index/view, Frames index and EventData index/view treated an empty
  viewable-monitor list as no restriction, so a user denied every monitor
  saw all of them. They now match nothing in that case.
- search and consoleEvents had no monitor filter at all and returned
  events and per-monitor counts for denied monitors.
- add saved any MonitorId; it now needs view on that monitor, as editing
  an event does, and cannot update an existing event named in the body.
- edit checked the event in the URL but saved the body, which could name
  another event's Id or move the event to a denied monitor. Pin the id and
  check a new MonitorId.
- createThumbnail and getMaxScoreAlarmFrameId were public, so routable,
  and checked nothing. They are internal helpers; make them private.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 17:54:25 -04:00
singhharsh1708 9443c79f34 fix: use the interpolation syntax that survives PHP 9 2026-09-07 13:11:51 +05:30
Isaac Connor 4d82e1d385 fix(api): answer 400 naming the field instead of 500 on an unknown filter
FilterComponent::buildFilter() checked the operator against an allow list
but never the field, so an unrecognised name went straight into the SQL:

  GET /api/events/index/MonitorName =:Monitor-1.json
  PDOException SQLSTATE[42S22]: Column not found: 1054
    Unknown column 'MonitorName' in 'where clause'   -> HTTP 500

A caller filtering on a monitor-scoped attribute (MonitorName, Monitor,
Rack, Experiment, Server, Storage) or simply mistyping a column got an
opaque 500 with a stack trace and nothing naming the offending field.

Take an optional list of permitted fields and reject anything else with
BadRequestException.  EventsController::index() passes the Events columns
from the model schema plus the two names it resolves itself: DateTime, the
pseudo-attribute it turns into an overlap test, and GroupId, served by the
Groups_Monitors join.  A field written Model.Field is checked on the column.
The two other callers of buildFilter pass no list and are unchanged.

The invalid-argument and invalid-operator paths threw a plain Exception,
which was also a 500 for what is bad input; they throw BadRequestException
now too.

Verified against a live install:

  MonitorName / Rack / NotAColumn     400, "Unknown filter field: <name>"
  DateTime >=/<= over a window        200, 7 rows, overlap intact
  Cause, Event.Cause, GroupId         200
  ?limit=2&sort=...&direction=desc    200, 2 rows, pagination unaffected

Plus an assert script over the validation itself, including that a caller
passing no list keeps the previous pass-through behaviour.

Filters sent as a query string rather than named parameters go through a
different branch, on raw $_REQUEST, and are deliberately left unvalidated:
that array also carries auth and cache-busting parameters, so rejecting
unknown names there would break existing callers.  ?MonitorName=x is still
a 500 and wants its own change.
2026-08-28 19:54:07 -04:00
Isaac Connor 0d7abadb61 fix: make the API DateTime filter an overlap test
DateTime is a pseudo-attribute meaning "the event was running then", so a
window over it should select events that overlap the window. The code applied
the term's own operator to both StartDateTime and EndDateTime, which turns the
upper bound into StartDateTime <= max AND EndDateTime <= max -- a containment
test. Any event spanning the end of the window was dropped, which under
continuous recording is most of them, and with events longer than the window,
all of them. Montage review showed an empty timeline as a result.

The lower bound now tests the event's end and the upper bound its start.

The EndDateTime IS NULL allowance is also bounded. It was there so an event
still being written stays visible, but as written it made every crash-orphaned
event ever recorded match every window: montage review was returning events
from two weeks earlier and nothing from the requested hour. An event with no
EndDateTime still has Length, flushed every few seconds by zmc, so
StartDateTime + Length is its effective end; only an event with neither falls
back to NOW(). This is the same expression the Event model already uses for
its EndTimeSecs virtual field.

Verified against a live instance: for a 09:43-10:43 window that had one
overlapping event per monitor, the API returned 8 events for monitor 1, all
crash orphans from 2026-07-26 and none from the window. It now returns the
overlapping event, and montage review draws it and renders its frames.
2026-08-09 21:35:39 -04:00
Isaac ConnorandClaude Opus 4.8 516bab0247 fix: enforce per-monitor ACL on direct event, frame and zone API endpoints
Several API endpoints checked only the coarse Events/Monitors permission
and not the per-monitor object ACL, so a user explicitly denied a monitor
could still reach that monitor's objects by addressing them directly:

- EventsController::edit() and ::delete() checked Events=Edit but never
  called canEdit() on the event, so any event could be mutated or deleted
  by Id.
- FramesController only guaranteed Events != None in beforeFilter().
  view() returned any frame by Id, and edit()/delete() mutated frames
  without requiring Events=Edit or checking the parent event at all.
- ZonesController::forMonitor() listed zones for any monitor Id.

Resolve the owning object and apply the same canView()/canEdit() checks
the normal read paths already use. Frames are addressed by their own Id,
so their parent event is looked up to reach the monitor ACL.

Refs GHSA-hw39-qpjw-p7cg.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-19 11:51:54 -04:00
Isaac ConnorandClaude Opus 4.8 3d5de347a4 fix: enforce authorization on API events/archive action
The REST API archive action toggled an event's Archived
(retention-protection) flag with no authorization beyond the
controller's coarse "Events permission is not None" gate. Any
authenticated read-only user, including one restricted to a subset of
monitors, could flip the retention state of any event by enumerating
event ids, and the action was reachable over GET (CSRF-able).

Gate the write by direction: archiving (protects from purge) requires
view access via Event::canView(); un-archiving (re-exposes to purge)
requires edit access via Event::canEdit(). Both enforce the per-monitor
object-level ACL. Restrict the action to POST/PUT to block CSRF.

Addresses GHSA-5v9h-ww7p-hxgv.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-27 12:32:51 -04:00
Isaac Connor 61c4c6606b perf: scope Event neighbor queries to Id only
The view() action sets recursive=1 on the Event model, which the
subsequent find('neighbors') calls inherited. That made each of the four
neighbor lookups (prev/next, prevOfMonitor/nextOfMonitor) SELECT every
column from Events plus LEFT JOIN Monitor and Storage, then fire a
separate Frames hasMany query per neighbor row. Only Event.Id is used
downstream.

Pass fields=Event.Id and recursive=-1 on each neighbor call so the
generated SQL is just:

  SELECT Event.Id FROM Events AS Event WHERE Event.Id < ?
    ORDER BY Event.Id DESC LIMIT 1

The per-monitor variant uses Events_MonitorId_idx which already covers
(MonitorId, Id) via InnoDB's implicit PK suffix, so no schema change is
needed.
2026-05-15 23:01:00 -04:00
Isaac ConnorandClaude Opus 4.6 ffe6362dc3 fix: harden web interface against injection and SSRF vulnerabilities
FilterTerm.php:
- Replace eval() with safe compare() method for SystemLoad, DiskPercent,
  and DiskBlocks filter conditions (RCE via crafted op/val)
- Validate operator against allowlist in constructor
- Sanitize collate field to alphanumeric/underscore only (SQLi)

onvifprobe.php:
- Use escapeshellarg() on interface, device_ep, soapversion, username,
  and password arguments passed to execONVIF() (command injection)

Event.php:
- Use escapeshellarg() on all arguments to zmvideo.pl instead of
  escapeshellcmd() on the whole command (command injection via format)
- Anchor scale regex with ^ and $ to prevent partial matches

image.php:
- Restrict proxy URL scheme to http/https only (SSRF via file:// etc)

filterdebug.php:
- Use already-sanitized $fid instead of raw $_REQUEST['fid'] (XSS)

MonitorsController.php:
- Use escapeshellarg() on token, username, password, and monitor id
  in zmu shell command instead of escapeshellcmd() on whole command

HostController.php:
- Use escapeshellarg() on path in du command (command injection via mid)
- Remove space from daemon name allowlist (argument injection)

EventsController.php:
- Remove single quotes from interval expression regex (SQLi)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 23:30:49 -04:00
Isaac Connor ba0aa29cdb Merge branch 'master' of github.com:ZoneMinder/zoneminder 2026-01-16 14:55:33 -05:00
Isaac Connor 0ecb344723 Don't contain Tag so we don't include Tag and event_Tag in results. 2026-01-05 17:49:46 -05:00
Steve Gilvarry 58a0e68731 Add Tags to event search and return tag data with events 2026-01-05 21:43:27 +11:00
Isaac Connor 9c79b1ad6e Make filtering by Tags.Id work 2025-12-19 17:50:10 -05:00
Isaac Connor 9351835815 Update use of allowedmonitors 2025-11-19 09:24:46 -05:00
Isaac Connor dbbe2cbcb8 Add EndDateTIme IS NULL condition when using DateTime 2025-10-30 13:53:28 -04:00
Isaac Connor 802ccdcaa9 Use event->can_view to restrict viewing event 2025-10-23 13:56:33 -04:00
Isaac Connor 65d51c7d0a Add support for DateTime as a filter, which means either StartDateTime OR EndDateTime. This allows montagereview to include video that starts BEFORE the requested time but ends after 2025-10-23 13:53:57 -04:00
Isaac Connor 1dde898154 Add support for NOT using named params, using query string instead 2025-09-05 12:42:13 -04:00
Isaac Connor d5e81d3c4a Fix incorrect use of eventObjas an array. 2025-07-19 18:29:22 -04:00
Isaac Connor 97877335db Add MaxScoreFrameId to the events table and automatically update it during event creation so we don't have to look it up later. Finding the frame with the max score is actually a very heavy query, so lets not do it. 2025-07-15 16:17:36 -04:00
Isaac Connor c3d4b12286 Do pagination if either page or limit is specified. Otherwise return all 2025-07-11 17:40:54 -04:00
Isaac Connor 7a3a5ca58a If request includes pagination= then restore the old pagination code. 2025-07-09 17:24:02 -04:00
Isaac Connor ce76f953dc Use limit if it is specified 2025-06-21 21:43:01 -04:00
Isaac Connor d97b37f83f Don't do pagination in events.This may break users of API but pagination should only happen if asked for 2025-06-11 14:57:54 -04:00