From 3b748b06e1364b9807ecadaacf37180bdad58192 Mon Sep 17 00:00:00 2001 From: steunix Date: Sun, 12 Oct 2025 20:07:36 +0200 Subject: [PATCH] Add option to customize Cache-Control header Fixes #414 --- config-skel.json | 3 ++- docs/index.md | 1 + lib/schemas/system_config.json | 3 ++- passweaver-api.mjs | 10 ++++++++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/config-skel.json b/config-skel.json index ff5461e..dd1f523 100644 --- a/config-skel.json +++ b/config-skel.json @@ -39,5 +39,6 @@ }, "readonly": false, "enable_metrics": true, - "generated_password_length": 15 + "generated_password_length": 15, + "cache-control": "" } diff --git a/docs/index.md b/docs/index.md index 9fa0f7b..246ab93 100644 --- a/docs/index.md +++ b/docs/index.md @@ -433,6 +433,7 @@ Copy `config-skel.json` to `config.json` and adjust the options (all options are - `readonly`: true or false; if true, no write operation is allowed both for admins and non-admins (logging is still operational) - `enable_metrics`: true or false, enables Prometheus-formatted metrics - `generated_password_length`: default length of random generated password (default is 15) +- `cache-control`: Cache-Control header to be sent along GET/HEAD responses ## 5. Prepare the database diff --git a/lib/schemas/system_config.json b/lib/schemas/system_config.json index 06d61dd..128c8bf 100644 --- a/lib/schemas/system_config.json +++ b/lib/schemas/system_config.json @@ -76,7 +76,8 @@ }, "readonly": { "type": "boolean" }, "enable_metrics": { "type": "boolean" }, - "generated_password_length": { "type": "integer", "minimum": 10, "maximum": 50 } + "generated_password_length": { "type": "integer", "minimum": 10, "maximum": 50 }, + "cache-control": { "type": "string" } }, "required": ["jwt_duration", "listen", "log", "https", "redis", "onetimetokens", "readonly", "crypto"] } \ No newline at end of file diff --git a/passweaver-api.mjs b/passweaver-api.mjs index cf666ec..b7ee947 100644 --- a/passweaver-api.mjs +++ b/passweaver-api.mjs @@ -65,6 +65,16 @@ if (cfg?.https?.hsts) { app.use(helmet.hsts()) } +// Set Cache-Control headers +const cacheControl = Config.get()['cache-control'] +app.use((req, res, next) => { + if ((req.method === 'GET' || req.method === 'HEAD') && cacheControl) { + res.set('Cache-Control', cacheControl) + } + res.set('Vary', 'Authorization, Accept-Encoding, Accept-Language') + next() +}) + if (!FS.existsSync(cfg.log.dir)) { FS.mkdirSync(cfg.log.dir) }