Files
zoneminder/web/views/archive.php
T
Isaac ConnorandClaude Opus 4.8 0fd646ef2e fix: reject path traversal in export download and archive views refs GHSA-fvx4-86xp-9v22
views/download.php and views/archive.php sanitized the user-controlled
`file` and `export_root` request parameters with str_replace('/', '')
only, which strips slashes but leaves `..` intact. export_root=.. made the
resolved path climb out of DIR_EXPORTS_DOWNLOAD into ZM_DIR_EXPORTS (and,
with file=.. as well, higher), and download.php then readfile()s it and,
from its shutdown handler, @unlink()s it. Any user with Events or Snapshots
view permission could read and delete files the web user can reach outside
the downloads directory.

Add safeExportComponent() and pathWithinExportDir() in
download_functions.php. The export file and directory names these views
handle are always single path components (zmExport_<connkey>.zip, a merged
'<Monitor> <start> to <end>.mp4'), so safeExportComponent() rejects any
value with a directory separator, a null byte, or a '.'/'..' traversal and
falls back to the generated default. pathWithinExportDir() resolves the
final path with realpath() and is checked in front of readfile() and
unlink(), so a path that still escapes the export directory - including
through a symlink - is refused.

Tests: tests/php/test_download_path_traversal.php

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-01 22:04:58 -05:00

80 lines
2.4 KiB
PHP

<?php
//
// ZoneMinder file view file
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
require_once('includes/download_functions.php');
if (!(canView('Events') or canView('Snapshots'))) {
$view = 'error';
return;
}
$archivetype = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';
if ( !$archivetype ) {
ZM\Error('No archive type given to archive.php. Please specify a tar or zip archive.');
return;
}
switch ($archivetype) {
case 'tar.gz':
$mimetype = 'gzip';
$file_ext = 'tar.gz';
break;
case 'tar':
$mimetype = 'tar';
$file_ext = 'tar';
break;
case 'zip':
$mimetype = 'zip';
$file_ext = 'zip';
break;
default:
$mimetype = NULL;
$file_ext = NULL;
}
if ( !$mimetype ) {
ZM\Error('Unsupported archive type specified. Supported archives are tar and zip');
return;
}
$connkey = isset($_REQUEST['connkey'])?$_REQUEST['connkey']:'';
# Must be a single path component, not a path. Stripping only '/' left '..'
# intact (GHSA-fvx4-86xp-9v22).
$filename = isset($_REQUEST['file']) ? safeExportComponent($_REQUEST['file']) : '';
if ($filename === '') $filename = "zmExport_$connkey.$file_ext";
$filename_path = ZM_DIR_EXPORTS.'/'.$filename;
ZM\Debug('downloading archive from '.$filename_path);
if ( is_readable($filename_path) and pathWithinExportDir($filename_path, ZM_DIR_EXPORTS) ) {
while (ob_get_level()) {
ob_end_clean();
}
header("Content-type: application/$mimetype");
header("Content-Disposition: inline; filename=$filename");
header('Content-Length: '.filesize($filename_path));
set_time_limit(0);
if ( !@readfile($filename_path) ) {
ZM\Error("Error sending $filename_path");
}
} else {
ZM\Error($filename_path.' does not exist or is not readable.');
}
?>