feat(http): security headers, deploy_revision on /health
Node tests / test (push) Has been cancelled
Node tests / test (push) Has been cancelled
- security-headers: mergeResponseHeaders (nosniff + referrer-policy) - status-server + ui-handlers: all responses use merged headers - /health: Cache-Control no-store; deploy_revision from DEPLOY_REVISION or GIT_REVISION - verify-deploy + .env.example: document deploy revision envs - security-headers.test.js Made-with: Cursor
This commit is contained in:
1 parent
c29b05e6a3
commit
20b94eadf9
6 files changed
+63
-13
No files matched your search
@@ -39,6 +39,9 @@ SCRAPER_RETRY_BASE_MS=500
|
||||
# App — HTTP status (listen/auto mode only; bind all interfaces for NPM → LXC)
|
||||
HOST=0.0.0.0
|
||||
PORT=3200
|
||||
# Optional: set at deploy (CI webhook) — surfaced in GET /health as deploy_revision
|
||||
# DEPLOY_REVISION=
|
||||
# GIT_REVISION=
|
||||
# Inbound socket timeouts (ms). headers must stay above request (enforced in code if misconfigured).
|
||||
HTTP_REQUEST_TIMEOUT_MS=120000
|
||||
HTTP_HEADERS_TIMEOUT_MS=125000
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Baseline response headers for the small HTTP surface (ops + /ui).
|
||||
* Caller-specific headers (Content-Type, Length, caching) win on key clash.
|
||||
*
|
||||
* @param {Record<string, string | number>} specific
|
||||
* @returns {Record<string, string | number>}
|
||||
*/
|
||||
function mergeResponseHeaders(specific) {
|
||||
return {
|
||||
'X-Content-Type-Options': 'nosniff',
|
||||
'Referrer-Policy': 'strict-origin-when-cross-origin',
|
||||
...specific,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { mergeResponseHeaders };
|
||||
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const { describe, it } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { mergeResponseHeaders } = require('./security-headers');
|
||||
|
||||
describe('mergeResponseHeaders', () => {
|
||||
it('adds baseline and merges specific', () => {
|
||||
const h = mergeResponseHeaders({ 'Content-Type': 'text/plain', 'X-Custom': '1' });
|
||||
assert.equal(h['X-Content-Type-Options'], 'nosniff');
|
||||
assert.equal(h['Referrer-Policy'], 'strict-origin-when-cross-origin');
|
||||
assert.equal(h['Content-Type'], 'text/plain');
|
||||
assert.equal(h['X-Custom'], '1');
|
||||
});
|
||||
});
|
||||
@@ -8,6 +8,7 @@ const { isRssListenerActive, getRssListenerHealthFields } = require('../runtime/
|
||||
const { tryHandleAdminUi } = require('./ui-handlers');
|
||||
const { getArticleLogHealthFields } = require('../store/article-log');
|
||||
const { parseIanaTimezone } = require('../util/iana-timezone');
|
||||
const { mergeResponseHeaders } = require('./security-headers');
|
||||
|
||||
const PACKAGE_JSON = require(path.join(__dirname, '..', '..', 'package.json'));
|
||||
|
||||
@@ -223,11 +224,13 @@ function startStatusServer() {
|
||||
(req.method === 'GET' || req.method === 'HEAD') &&
|
||||
(url === '/health' || url === '/health/')
|
||||
) {
|
||||
const deploy_revision = (process.env.DEPLOY_REVISION || process.env.GIT_REVISION || '').trim() || null;
|
||||
const body = JSON.stringify({
|
||||
ok: true,
|
||||
service: 'ai-news-writer',
|
||||
version: PACKAGE_JSON.version || '0.0.0',
|
||||
mode: 'listen',
|
||||
deploy_revision,
|
||||
pid: process.pid,
|
||||
node_version: process.version,
|
||||
uptime_sec: Math.floor(process.uptime()),
|
||||
@@ -235,7 +238,14 @@ function startStatusServer() {
|
||||
http_headers_timeout_ms: server.headersTimeout,
|
||||
...getListenFeatureFlags(),
|
||||
});
|
||||
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': Buffer.byteLength(body) });
|
||||
res.writeHead(
|
||||
200,
|
||||
mergeResponseHeaders({
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Content-Length': Buffer.byteLength(body),
|
||||
'Cache-Control': 'no-store',
|
||||
})
|
||||
);
|
||||
if (req.method === 'HEAD') {
|
||||
res.end();
|
||||
return;
|
||||
@@ -248,16 +258,16 @@ function startStatusServer() {
|
||||
(url === '/' || url === '/index.html' || url === '/dashboard' || url === '/dashboard/')
|
||||
) {
|
||||
const html = buildLandingHtml();
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) }));
|
||||
res.end(html);
|
||||
return;
|
||||
}
|
||||
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||||
res.writeHead(404, mergeResponseHeaders({ 'Content-Type': 'text/plain; charset=utf-8' }));
|
||||
res.end('Not found');
|
||||
} catch (err) {
|
||||
logger.error(`HTTP handler error: ${err.message}`, { stack: err.stack });
|
||||
if (!res.writableEnded) {
|
||||
res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
|
||||
res.writeHead(500, mergeResponseHeaders({ 'Content-Type': 'text/plain; charset=utf-8' }));
|
||||
res.end('Internal error');
|
||||
}
|
||||
}
|
||||
|
||||
+10
-9
@@ -3,6 +3,7 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const logger = require('../logger');
|
||||
const { mergeResponseHeaders } = require('./security-headers');
|
||||
const { loadSources, loadSiteConfigs, loadDefaultSiteConfig, loadSocialConfig } = require('../config/loader');
|
||||
const { readLastArticles } = require('../store/article-log');
|
||||
|
||||
@@ -284,35 +285,35 @@ async function tryHandleAdminUi(req, res) {
|
||||
|
||||
if (req.method === 'GET' && pathname === '/ui') {
|
||||
const html = pageDashboard(theme);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) }));
|
||||
res.end(html);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (req.method === 'GET' && pathname === '/ui/sources') {
|
||||
const html = pageSources(theme, null);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) }));
|
||||
res.end(html);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (req.method === 'GET' && pathname === '/ui/social') {
|
||||
const html = pageSocial(theme, null);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) }));
|
||||
res.end(html);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (req.method === 'GET' && pathname === '/ui/publishers') {
|
||||
const html = pagePublishers(theme);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) }));
|
||||
res.end(html);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (req.method === 'GET' && pathname === '/ui/theme') {
|
||||
const html = pageTheme(theme, null);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) }));
|
||||
res.end(html);
|
||||
return true;
|
||||
}
|
||||
@@ -320,7 +321,7 @@ async function tryHandleAdminUi(req, res) {
|
||||
if (req.method === 'GET' && pathname === '/api/ui/articles') {
|
||||
const limit = Math.min(200, Math.max(1, parseInt(u.searchParams.get('limit') || '50', 10) || 50));
|
||||
const body = JSON.stringify({ ok: true, articles: readLastArticles(limit) });
|
||||
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': Buffer.byteLength(body) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': Buffer.byteLength(body) }));
|
||||
res.end(body);
|
||||
return true;
|
||||
}
|
||||
@@ -345,7 +346,7 @@ async function tryHandleAdminUi(req, res) {
|
||||
msg = { type: 'err', text: err.message || String(err) };
|
||||
}
|
||||
const html = pageSources(loadUiTheme(), msg);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) }));
|
||||
res.end(html);
|
||||
return true;
|
||||
}
|
||||
@@ -370,7 +371,7 @@ async function tryHandleAdminUi(req, res) {
|
||||
msg = { type: 'err', text: err.message || String(err) };
|
||||
}
|
||||
const html = pageSocial(loadUiTheme(), msg);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) }));
|
||||
res.end(html);
|
||||
return true;
|
||||
}
|
||||
@@ -403,7 +404,7 @@ async function tryHandleAdminUi(req, res) {
|
||||
msg = { type: 'err', text: err.message || String(err) };
|
||||
}
|
||||
const html = pageTheme(loadUiTheme(), msg);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) });
|
||||
res.writeHead(200, mergeResponseHeaders({ 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': Buffer.byteLength(html) }));
|
||||
res.end(html);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -51,6 +51,9 @@ console.log(
|
||||
'node=' + (d.node_version || '?'),
|
||||
'uptime_sec=' + (d.uptime_sec ?? '?')
|
||||
);
|
||||
if (d.deploy_revision) {
|
||||
console.log('deploy_revision=' + d.deploy_revision);
|
||||
}
|
||||
if (d.http_request_timeout_ms != null) {
|
||||
console.log('http_request_timeout_ms=' + d.http_request_timeout_ms, 'http_headers_timeout_ms=' + d.http_headers_timeout_ms);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user