Files
proxy/nodejs/routes/host.js
T
wmantly 3e5590288a Per-host HTTP basic auth (#57)
Adds opt-in basic auth per Host, following the existing per-host controls
pattern:
- Host fields basicauth_enabled / basicauth_realm / basicauth_users
  ({user: base64(sha1(pw))}). Credentials are parsed to plaintext by the pure
  host_features normalizer and hashed at the route layer (utils/basicauth.js),
  so plaintext never reaches Redis.
- ops/nginx_conf/hostfeatures.lua enforces it in access phase: verifies the
  Authorization header against base64(sha1(password)), fails closed with a 401
  WWW-Authenticate challenge.
- hosts.ejs gains an enable toggle, realm, and a username:password textarea
  (passwords never echoed back; blank keeps the current set).

Unit tests cover hashing (matches the htpasswd {SHA} vector), credential
parsing, and normalization. Note: the Lua path needs verification on a live
OpenResty box.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 11:47:08 -04:00

159 lines
4.2 KiB
JavaScript
Executable File

'use strict';
const router = require('express').Router();
const {Host, Domain} = require('../models').models;
const authz = require('../middleware/authz');
const {normalizeHostFeatures} = require('../utils/host_features');
const {collectHostFieldErrors} = require('../utils/hostname_validate');
const {hashBasicAuthUsers} = require('../utils/basicauth');
const Model = Host;
// Reject a malformed host/target before it reaches the model. Throws a 422
// ObjectValidateError (per-field keys) that the frontend surfaces inline.
function validateHostFields(body){
let errors = collectHostFieldErrors(body);
if(errors.length) throw Model.errors.ObjectValidateError(errors);
}
// After normalizeHostFeatures has parsed basic-auth creds to {user: plaintext},
// hash them so plaintext never reaches Redis. Runs at the route layer only, so
// internally-copied records (cache/wildcard children) keep their existing hashes.
function hashHostSecrets(body){
if(body.basicauth_users && typeof body.basicauth_users === 'object'){
body.basicauth_users = hashBasicAuthUsers(body.basicauth_users);
}
}
router.get('/', async function(req, res, next){
try{
let results = await Model[req.query.detail ? "listDetail" : "list"]();
// Restrict to hosts whose domain the caller may view. list() yields host
// strings; listDetail() yields instances with a .host.
results = await authz.filterViewable(req, results,
item => (typeof item === 'string' ? item : item.host));
return res.json({results});
}catch(error){
return next(error);
}
});
router.post('/', authz.requireDomainRole('manager', authz.resolve.hostBody), async function(req, res, next){
try{
req.body.created_by = authz.reqUsername(req);
validateHostFields(req.body);
normalizeHostFeatures(req.body);
hashHostSecrets(req.body);
let item = await Model.create(req.body);
return res.json({
message: `"${item[Model._key]}" added.`,
...item,
});
} catch (error){
next(error);
}
});
router.get('/lookup/:item', authz.requireDomainRole('viewer', authz.resolve.hostParam), async function(req, res, next){
try{
return res.json({
string: req.params.item,
results: await Model.lookUp(req.params.item),
});
}catch(error){
return next(error);
}
});
// The full lookup tree exposes every host, so restrict it to admins.
router.get('/lookupobj', authz.requireAdmin, async function(req, res, next){
try{
return res.json({
results: Model.lookUpObj,
});
}catch(error){
return next(error);
}
});
router.delete('/cache', authz.requireAdmin, async function(req, res, next){
try{
let count = await Model.clearCache();
return res.json({
message: `Cleared ${count} cached host${count === 1 ? '' : 's'}.`,
count,
});
}catch(error){
return next(error);
}
});
router.get('/:item', authz.requireDomainRole('viewer', authz.resolve.hostParam), async function(req, res, next){
try{
return res.json({
item: req.params.item,
results: await Model.get(req.params.item)
});
}catch(error){
return next(error);
}
});
router.put('/:item', authz.requireDomainRole('manager', authz.resolve.hostParam), async function(req, res, next){
try{
req.body.updated_by = authz.reqUsername(req);
validateHostFields(req.body);
normalizeHostFeatures(req.body);
hashHostSecrets(req.body);
let item = await Model.get(req.params.item);
item = await item.update(req.body);
return res.json({
message: `"${req.params.item}" updated.`,
__requestedHost: req.params.item,
...item,
});
}catch(error){
return next(error);
}
});
router.delete('/:item', authz.requireDomainRole('manager', authz.resolve.hostParam), async function(req, res, next){
try{
let item = await Model.get(req.params.item);
let count = await item.remove();
return res.json({
message: `${req.params.item} deleted`,
...item,
});
}catch(error){
return next(error);
}
});
router.put('/:item/renew', authz.requireDomainRole('manager', authz.resolve.hostParam), async function(req, res, next){
try{
let item = await Model.get(req.params.item);
item.createWildcardCert();
return res.json({
message: `Requesting wildcard cert for ${req.params.item}`,
})
}catch(error){
next(error);
}
});
module.exports = router;