Implement security hardening across frontend, backend, and infrastructure

- nginx: add X-Content-Type-Options, X-Frame-Options, X-XSS-Protection,
  and Referrer-Policy headers on all responses; rate limit /api/auth/login
  to 5 req/min per IP (burst 3) to prevent brute force
- frontend: add escHtml() utility to api.js; use it on all notes fields
  across dashboard, log, history, flock, and budget pages to prevent XSS
- log.js: fix broken loadRecent() call referencing removed #recent-body
  element; replaced with loadHistory() from history.js
- schemas.py: raise minimum password length from 6 to 10 characters
- admin.py: add audit logging for password reset, disable, delete, and
  impersonate actions; fix impersonate to use named admin param for logging
- main.py: add startup env validation — exits with clear error if any
  required env var is missing; configure structured logging to stdout
- docker-compose.yml: add log rotation (10 MB / 3 files) to all services

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-17 23:55:08 -07:00
parent b660263f30
commit 37f19a83ed
11 changed files with 100 additions and 42 deletions

View File

@@ -66,6 +66,17 @@ function fmtMoneyFull(val) {
return '$' + Number(val).toFixed(4);
}
// Escape HTML special characters to prevent XSS when rendering user content
function escHtml(str) {
if (!str) return '';
return str
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}
// Highlight the nav link that matches the current page
function highlightNav() {
const path = window.location.pathname.replace(/\.html$/, '').replace(/\/$/, '') || '/';

View File

@@ -59,7 +59,7 @@ function renderTable() {
<td>Feed</td>
<td>${parseFloat(e.bags)} bags @ ${fmtMoney(e.price_per_bag)}/bag</td>
<td>${fmtMoney(total)}</td>
<td class="notes">${e.notes || ''}</td>
<td class="notes">${escHtml(e.notes)}</td>
<td class="actions">
<button class="btn btn-ghost btn-sm" onclick="startEditFeed(${e.id})">Edit</button>
<button class="btn btn-danger btn-sm" onclick="deleteFeed(${e.id})">Delete</button>
@@ -73,7 +73,7 @@ function renderTable() {
<td>Other</td>
<td>—</td>
<td>${fmtMoney(e.total)}</td>
<td class="notes">${e.notes || ''}</td>
<td class="notes">${escHtml(e.notes)}</td>
<td class="actions">
<button class="btn btn-ghost btn-sm" onclick="startEditOther(${e.id})">Edit</button>
<button class="btn btn-danger btn-sm" onclick="deleteOther(${e.id})">Delete</button>
@@ -111,7 +111,7 @@ function startEditFeed(id) {
<input type="number" min="0.01" step="0.01" value="${parseFloat(entry.price_per_bag)}" placeholder="price" style="width:80px;">/bag
</td>
<td>—</td>
<td><input type="text" value="${entry.notes || ''}" placeholder="Notes"></td>
<td><input type="text" value="${escHtml(entry.notes)}" placeholder="Notes"></td>
<td class="actions">
<button class="btn btn-primary btn-sm" onclick="saveEditFeed(${id})">Save</button>
<button class="btn btn-ghost btn-sm" onclick="renderTable()">Cancel</button>
@@ -165,7 +165,7 @@ function startEditOther(id) {
<td>Other</td>
<td>—</td>
<td><input type="number" min="0.01" step="0.01" value="${parseFloat(entry.total)}" style="width:100px;"></td>
<td><input type="text" value="${entry.notes || ''}" placeholder="Notes"></td>
<td><input type="text" value="${escHtml(entry.notes)}" placeholder="Notes"></td>
<td class="actions">
<button class="btn btn-primary btn-sm" onclick="saveEditOther(${id})">Save</button>
<button class="btn btn-ghost btn-sm" onclick="renderTable()">Cancel</button>

View File

@@ -115,7 +115,7 @@ async function loadDashboard() {
<tr>
<td>${fmtDate(e.date)}</td>
<td>${e.eggs}</td>
<td class="notes">${e.notes || ''}</td>
<td class="notes">${escHtml(e.notes)}</td>
</tr>
`).join('');

View File

@@ -30,7 +30,7 @@ function renderTable() {
<tr data-id="${e.id}">
<td>${fmtDate(e.date)}</td>
<td>${e.chicken_count}</td>
<td class="notes">${e.notes || ''}</td>
<td class="notes">${escHtml(e.notes)}</td>
<td class="actions">
<button class="btn btn-ghost btn-sm" onclick="startEdit(${e.id})">Edit</button>
<button class="btn btn-danger btn-sm" onclick="deleteEntry(${e.id})">Delete</button>
@@ -46,7 +46,7 @@ function startEdit(id) {
row.innerHTML = `
<td><input type="date" value="${entry.date}"></td>
<td><input type="number" min="0" value="${entry.chicken_count}" style="width:80px;"></td>
<td><input type="text" value="${entry.notes || ''}" placeholder="Notes"></td>
<td><input type="text" value="${escHtml(entry.notes)}" placeholder="Notes"></td>
<td class="actions">
<button class="btn btn-primary btn-sm" onclick="saveEdit(${id})">Save</button>
<button class="btn btn-ghost btn-sm" onclick="renderTable()">Cancel</button>

View File

@@ -42,7 +42,7 @@ function renderTable() {
<tr data-id="${e.id}">
<td>${fmtDate(e.date)}</td>
<td>${e.eggs}</td>
<td class="notes">${e.notes || ''}</td>
<td class="notes">${escHtml(e.notes)}</td>
<td class="actions">
<button class="btn btn-ghost btn-sm" onclick="startEdit(${e.id})">Edit</button>
<button class="btn btn-danger btn-sm" onclick="deleteEntry(${e.id})">Delete</button>
@@ -67,7 +67,7 @@ function startEdit(id) {
row.innerHTML = `
<td><input type="date" value="${entry.date}"></td>
<td><input type="number" min="0" value="${entry.eggs}" style="width:80px;"></td>
<td><input type="text" value="${entry.notes || ''}" placeholder="Notes"></td>
<td><input type="text" value="${escHtml(entry.notes)}" placeholder="Notes"></td>
<td class="actions">
<button class="btn btn-primary btn-sm" onclick="saveEdit(${id})">Save</button>
<button class="btn btn-ghost btn-sm" onclick="renderTable()">Cancel</button>

View File

@@ -1,26 +1,3 @@
async function loadRecent() {
const tbody = document.getElementById('recent-body');
try {
const eggs = await API.get('/api/eggs');
const recent = eggs.slice(0, 7);
if (recent.length === 0) {
tbody.innerHTML = '<tr class="empty-row"><td colspan="3">No entries yet.</td></tr>';
return;
}
tbody.innerHTML = recent.map(e => `
<tr>
<td>${fmtDate(e.date)}</td>
<td>${e.eggs}</td>
<td class="notes">${e.notes || ''}</td>
</tr>
`).join('');
} catch (err) {
tbody.innerHTML = '<tr class="empty-row"><td colspan="3">Could not load recent entries.</td></tr>';
}
}
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('log-form');
const msg = document.getElementById('msg');
@@ -42,11 +19,9 @@ document.addEventListener('DOMContentLoaded', () => {
showMessage(msg, 'Entry saved!');
form.reset();
setToday(document.getElementById('date'));
loadRecent();
loadHistory();
} catch (err) {
showMessage(msg, `Error: ${err.message}`, 'error');
}
});
loadRecent();
});

View File

@@ -13,6 +13,9 @@ http {
gzip_types text/plain text/css application/javascript application/json;
gzip_min_length 1000;
# ── Rate limiting — login endpoint ────────────────────────────────────────
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
server {
listen 80;
server_name _;
@@ -20,6 +23,12 @@ http {
root /usr/share/nginx/html;
index index.html;
# ── Security headers ──────────────────────────────────────────────────
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# ── Static files ──────────────────────────────────────────────────────
location / {
try_files $uri $uri.html $uri/ =404;
@@ -28,10 +37,33 @@ http {
# Cache static assets aggressively and suppress access log noise
location ~* \.(css|js|svg|ico|png|jpg|webp|woff2?)$ {
expires 7d;
add_header Cache-Control "public, immutable";
add_header Cache-Control "public, immutable";
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
access_log off;
}
# ── Login rate limiting ───────────────────────────────────────────────
location = /api/auth/login {
limit_req zone=login burst=3 nodelay;
proxy_pass http://api:8000;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
add_header Cache-Control "no-store" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
}
# ── API reverse proxy ─────────────────────────────────────────────────
# All /api/* requests are forwarded to the FastAPI container.
# The container is reachable by its service name on the Docker network.
@@ -44,8 +76,11 @@ http {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Don't cache API responses
add_header Cache-Control "no-store";
add_header Cache-Control "no-store" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
}
# ── Custom error pages ────────────────────────────────────────────────