mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-07 03:29:19 +02:00
security: fix all HIGH priority items from TODO.SECURITY.md
Items resolved: - #3 (HIGH): Move file uploads outside webroot to STORAGE_ROOT (/var/www/posterg/storage). Uploads were previously stored in public/admin/actions/data/ which is web-accessible. - #4 (HIGH): Align file paths and add media.php controller. DB paths are now storage-relative (theses/YEAR/ID/file, covers/file). New public/media.php serves files with path-traversal jail, MIME allow-list, and proper caching headers. memoire.php and search.php updated to use /media.php?path=. Also fixed: cover images were never recorded in thesis_files (broken INSERT). - #5 (HIGH): RateLimit::getClientIdentifier() now uses REMOTE_ADDR only. HTTP_X_FORWARDED_FOR and HTTP_CLIENT_IP are attacker-controlled headers that allowed unlimited rate-limit bypass by rotating spoofed IPs. - #6 (HIGH): Port public/admin/.htaccess security rules to nginx/posterg.conf. Apache .htaccess directives are silently ignored by nginx; none were active. CSP added to /admin/ location block, .log file denial added globally, autoindex off made explicit. Documented in nginx/HTACCESS_TO_NGINX.md. Supporting changes: - config/bootstrap.php: add STORAGE_ROOT constant - nginx/SECURITY_HEADERS.md: updated to reflect admin CSP and pending public CSP - docs/TODO.SECURITY.md: items #3-6 moved to resolved; priority order updated
This commit is contained in:
@@ -194,9 +194,10 @@ try {
|
||||
|
||||
// ===== HANDLE FILE UPLOADS =====
|
||||
|
||||
// Create necessary directories
|
||||
$uploadBaseDir = __DIR__ . "/data/theses/{$annee}/{$identifier}/";
|
||||
$coverDir = __DIR__ . "/data/covers/";
|
||||
// Create necessary directories — outside the webroot (security items #3 & #4).
|
||||
// Files are served through /media.php, never directly via a URL path.
|
||||
$uploadBaseDir = STORAGE_ROOT . "/theses/{$annee}/{$identifier}/";
|
||||
$coverDir = STORAGE_ROOT . "/covers/";
|
||||
|
||||
if (!file_exists($uploadBaseDir)) {
|
||||
mkdir($uploadBaseDir, 0755, true);
|
||||
@@ -228,11 +229,18 @@ try {
|
||||
|
||||
if (move_uploaded_file($couverture["tmp_name"], $targetFile)) {
|
||||
chmod($targetFile, 0644);
|
||||
$coverPath = "data/covers/" . $safeFileName;
|
||||
// Path stored relative to STORAGE_ROOT; served via /media.php?path=...
|
||||
$coverPath = "covers/" . $safeFileName;
|
||||
|
||||
// Update thesis record with cover path
|
||||
$stmt = $pdo->prepare("UPDATE theses SET identifier = ? WHERE id = ?");
|
||||
// Store cover path in remarks for now (we could add a cover_path column)
|
||||
// Record cover in thesis_files (type = 'cover')
|
||||
$db->insertThesisFile(
|
||||
$thesisId,
|
||||
'cover',
|
||||
$coverPath,
|
||||
basename($couverture["name"]),
|
||||
$couverture["size"],
|
||||
$mimeType
|
||||
);
|
||||
error_log("Cover image uploaded: " . $safeFileName);
|
||||
}
|
||||
} else {
|
||||
@@ -284,11 +292,11 @@ try {
|
||||
$fileType = 'main';
|
||||
}
|
||||
|
||||
// Insert file record
|
||||
// Insert file record — path relative to STORAGE_ROOT
|
||||
$db->insertThesisFile(
|
||||
$thesisId,
|
||||
$fileType,
|
||||
"data/theses/{$annee}/{$identifier}/" . $safeFileName,
|
||||
"theses/{$annee}/{$identifier}/" . $safeFileName,
|
||||
basename($files["name"][$i]),
|
||||
$files["size"][$i],
|
||||
$mimeType
|
||||
|
||||
90
public/media.php
Normal file
90
public/media.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Media file serving controller
|
||||
*
|
||||
* Serves uploaded files that are stored outside the webroot (STORAGE_ROOT).
|
||||
* This is the sole access point for thesis files, covers, and annexes — they
|
||||
* are never exposed as direct filesystem paths from the web server.
|
||||
*
|
||||
* Security:
|
||||
* - Strict character whitelist on the path parameter (no path traversal)
|
||||
* - realpath() jail: resolved path must stay inside STORAGE_ROOT
|
||||
* - MIME type verified against an allow-list before serving
|
||||
*/
|
||||
require_once __DIR__ . '/../config/bootstrap.php';
|
||||
|
||||
// --- 1. Extract and validate the requested path -------------------------------
|
||||
|
||||
$requestedPath = $_GET['path'] ?? '';
|
||||
|
||||
// Only allow safe characters: letters, digits, forward slashes, dots, hyphens, underscores.
|
||||
// This explicitly rejects null bytes, backslashes, and sequences like "../".
|
||||
if (!preg_match('#^[a-zA-Z0-9/_\-.]+$#', $requestedPath) || $requestedPath === '') {
|
||||
http_response_code(400);
|
||||
exit;
|
||||
}
|
||||
|
||||
// --- 2. Resolve full filesystem path and enforce storage jail -----------------
|
||||
|
||||
$storageRoot = defined('STORAGE_ROOT') ? STORAGE_ROOT : '/var/www/posterg/storage';
|
||||
$fullPath = $storageRoot . '/' . $requestedPath;
|
||||
|
||||
$realStorage = realpath($storageRoot);
|
||||
$realFull = realpath($fullPath);
|
||||
|
||||
if (
|
||||
$realFull === false
|
||||
|| $realStorage === false
|
||||
|| strpos($realFull, $realStorage . '/') !== 0
|
||||
) {
|
||||
http_response_code(404);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (!is_file($realFull)) {
|
||||
http_response_code(404);
|
||||
exit;
|
||||
}
|
||||
|
||||
// --- 3. Verify MIME type from file content (not extension) --------------------
|
||||
|
||||
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
||||
$mimeType = $finfo->file($realFull);
|
||||
|
||||
$allowedMimes = [
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
'image/gif',
|
||||
'application/pdf',
|
||||
'video/mp4',
|
||||
'application/zip',
|
||||
];
|
||||
|
||||
if (!in_array($mimeType, $allowedMimes, true)) {
|
||||
http_response_code(403);
|
||||
exit;
|
||||
}
|
||||
|
||||
// --- 4. Send response headers -------------------------------------------------
|
||||
|
||||
header('Content-Type: ' . $mimeType);
|
||||
header('Content-Length: ' . filesize($realFull));
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
|
||||
$ext = strtolower(pathinfo($realFull, PATHINFO_EXTENSION));
|
||||
|
||||
if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'], true)) {
|
||||
// Images: cache publicly for 7 days
|
||||
header('Cache-Control: public, max-age=604800');
|
||||
} elseif ($ext === 'pdf') {
|
||||
// PDFs: cache for 1 day, display inline
|
||||
header('Cache-Control: public, max-age=86400');
|
||||
header('Content-Disposition: inline');
|
||||
} else {
|
||||
// Everything else: no public caching
|
||||
header('Cache-Control: private, no-store');
|
||||
}
|
||||
|
||||
// --- 5. Stream file -----------------------------------------------------------
|
||||
|
||||
readfile($realFull);
|
||||
@@ -125,16 +125,16 @@ include APP_ROOT . '/includes/header.php'; ?>
|
||||
<div class="block">
|
||||
<?php if ($ext === 'pdf'): ?>
|
||||
<!-- Display PDF files using the embed element -->
|
||||
<embed src="<?= htmlspecialchars($file['file_path']); ?>" type="application/pdf" width="100%" height="600px" />
|
||||
<embed src="/media.php?path=<?= urlencode($file['file_path']); ?>" type="application/pdf" width="100%" height="600px" />
|
||||
<?php elseif (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp'])): ?>
|
||||
<!-- Display image files using the img element -->
|
||||
<figure>
|
||||
<img src="<?= htmlspecialchars($file['file_path']); ?>" alt="<?= htmlspecialchars($file['file_name']); ?>">
|
||||
<img src="/media.php?path=<?= urlencode($file['file_path']); ?>" alt="<?= htmlspecialchars($file['file_name']); ?>">
|
||||
</figure>
|
||||
<?php elseif ($ext === 'mp4'): ?>
|
||||
<!-- Display MP4 video files using the video element -->
|
||||
<video width="100%" height="auto" controls>
|
||||
<source src="<?= htmlspecialchars($file['file_path']); ?>" type="video/mp4">
|
||||
<source src="/media.php?path=<?= urlencode($file['file_path']); ?>" type="video/mp4">
|
||||
Your browser does not support the video tag.
|
||||
</video>
|
||||
<?php endif; ?>
|
||||
|
||||
@@ -341,9 +341,9 @@ include APP_ROOT . '/includes/header.php'; ?>
|
||||
if (!empty($item['id'])) {
|
||||
$files = $db->getThesisFiles($item['id']);
|
||||
foreach ($files as $file) {
|
||||
$ext = strtolower(pathinfo($file['file_path'], PATHINFO_EXTENSION));
|
||||
if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif']) && $file['file_type'] === 'main') {
|
||||
$coverImage = $file['file_path'];
|
||||
// Look for dedicated cover file (type = 'cover')
|
||||
if ($file['file_type'] === 'cover') {
|
||||
$coverImage = '/media.php?path=' . urlencode($file['file_path']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user