mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2026-11-27 04:31:11 +00:00
Fix second round of review findings from PR #6029
Security - Check advanced_config permissions before creating a certificate on host update (proxy, redirection and 404 hosts). A refused request previously requested a Let's Encrypt certificate first, leaving an orphan certificate and using up rate limit. - Only trust X-NPM-Peer-Addr when it comes with a secret generated at container start. The admin nginx sends both headers. Any proxy host or stream forwarding to 127.0.0.1:3000 was also a loopback peer, so it could forge the header and get a fresh rate-limit bucket on every request. Without the secret (dev, or a mounted production.conf), the header is ignored. - Add a login limit per IP across all accounts (30 failed attempts a minute) alongside the per-account one. Before this, one address could spray passwords over any number of accounts. Behind a shared proxy, everyone using that proxy shares this limit. - Stop trusting X-Real-IP from loopback. It was only there for an older version of the rate limiter, and it let a stream host forwarding to a local proxy host forge the client IP and get past IP access lists. Loopback now behaves as it does on master. Correctness - Refuse to delete a certificate that hosts still use (400, with counts per host type). Their configs include its files, and a successful revoke deleted them anyway, which broke nginx -t. Otherwise the certificate is revoked and its files are removed even if the revoke fails. - Let non-admins clear a location's advanced config. Deleting the location was already allowed and has the same effect, and a location deleted and re-added at the same path arrives with an empty config, which was refused. - Clear proxy host grants (meta.proxy_host_ids) when visibility isn't "user" or proxy hosts are hidden, and drop ids of deleted hosts. Before, hidden grants came back when proxy hosts were shown again. The backend enforces this; the permissions modal also stops sending them. - 404 host and redirection host modals now wait for the current user to load, so an admin's advanced config change isn't dropped if they submit before it loads. Tests - NonAdminHosts.cy.js: refused change doesn't create a certificate, location rename and clear rules, grant clearing. - Certificates.cy.js: deleting a certificate that is in use.
This commit is contained in:
1 parent
04c1f5ae76
commit
5b7e34eb67
18 files changed
+423
-62
No files matched your search
@@ -566,7 +566,24 @@ const internalCertificate = {
|
||||
throw new error.ItemNotFoundError(data.id);
|
||||
}
|
||||
|
||||
const inUse = hostRelations.some((rel) => row[rel]?.length);
|
||||
// A certificate that hosts still use can't be deleted: their rendered configs include its
|
||||
// files, and revoking it would leave them serving a revoked certificate. Hosts may belong
|
||||
// to other users, so only counts are reported.
|
||||
const labels = {
|
||||
proxy_hosts: "proxy host",
|
||||
redirection_hosts: "redirection host",
|
||||
dead_hosts: "404 host",
|
||||
streams: "stream",
|
||||
};
|
||||
const usage = hostRelations
|
||||
.map((rel) => ({ rel, count: row[rel]?.length || 0 }))
|
||||
.filter((u) => u.count > 0)
|
||||
.map((u) => `${u.count} ${labels[u.rel]}${u.count === 1 ? "" : "s"}`);
|
||||
if (usage.length) {
|
||||
throw new error.ValidationError(
|
||||
`Certificate is in use by ${usage.join(", ")}. Remove it from them before deleting it.`,
|
||||
);
|
||||
}
|
||||
for (const rel of hostRelations) {
|
||||
delete row[rel];
|
||||
}
|
||||
@@ -586,20 +603,10 @@ const internalCertificate = {
|
||||
});
|
||||
|
||||
if (row.provider === "letsencrypt") {
|
||||
// Revoke the cert
|
||||
const revoked = await internalCertificate.revokeLetsEncryptSsl(row, true).then(
|
||||
() => true,
|
||||
() => false,
|
||||
);
|
||||
// Certbot leaves the files behind when it can't revoke (e.g. a broken renewal config).
|
||||
// Remove them anyway, unless a host still uses this certificate: its rendered config
|
||||
// includes these files, and removing them would fail nginx -t on the next reload.
|
||||
if (!revoked && inUse) {
|
||||
logger.warn(
|
||||
`Certificate #${row.id} was deleted but could not be revoked, and hosts still use it; its files in /etc/letsencrypt were kept`,
|
||||
);
|
||||
return true;
|
||||
}
|
||||
// Revoke the cert. Errors are logged by revokeLetsEncryptSsl.
|
||||
await internalCertificate.revokeLetsEncryptSsl(row);
|
||||
// Certbot leaves the files behind when it can't revoke (e.g. a broken renewal config),
|
||||
// so remove them either way. No host uses them; that was checked above.
|
||||
// The row is already deleted and audited, so leftover files are logged, not thrown:
|
||||
// an error here would report a completed delete as failed, and a retry would 404.
|
||||
const leftover = internalCertificate.removeLetsEncryptFiles(row.id);
|
||||
|
||||
@@ -132,6 +132,9 @@ const internalDeadHost = {
|
||||
);
|
||||
}
|
||||
|
||||
// Before the certificate is created, so a refused request doesn't leave one behind
|
||||
internalHost.assertAdvancedConfigAllowed(access, data, row);
|
||||
|
||||
if (createCertificate) {
|
||||
const cert = await internalCertificate.createQuickCertificate(access, {
|
||||
domain_names: data.domain_names || row.domain_names,
|
||||
@@ -153,8 +156,6 @@ const internalDeadHost = {
|
||||
|
||||
thisData = internalHost.cleanSslHstsData(thisData, row);
|
||||
|
||||
internalHost.assertAdvancedConfigAllowed(access, data, row);
|
||||
|
||||
// do the row update
|
||||
await deadHostModel.query().where({ id: data.id }).patch(data);
|
||||
|
||||
|
||||
@@ -45,10 +45,12 @@ const internalHost = {
|
||||
},
|
||||
|
||||
/**
|
||||
* For non-admins: every location's advanced_config must be one that already exists on this
|
||||
* host. Matching is by value, not path, so renaming or reordering a location that carries
|
||||
* admin-written config is allowed, but new or edited config is not. Each existing config can
|
||||
* be used once (no duplicating it onto other paths), and same-path matches are taken first.
|
||||
* For non-admins: every location's advanced_config must be empty or one that already exists
|
||||
* on this host. Matching is by value, not path, so renaming or reordering a location that
|
||||
* carries admin-written config is allowed, but new or edited config is not. Each existing
|
||||
* config can be used once (no duplicating it onto other paths), and same-path matches are
|
||||
* taken first. Clearing a config is allowed: deleting the location is, and has the same
|
||||
* effect, and a location deleted and re-added at the same path arrives with an empty config.
|
||||
* A location that omits advanced_config keeps the one at its path, unless another location
|
||||
* explicitly carries it. Mutates `locations`.
|
||||
*
|
||||
@@ -86,10 +88,10 @@ const internalHost = {
|
||||
}
|
||||
}
|
||||
|
||||
// Pass 2: renamed/moved locations may carry an existing config not already claimed.
|
||||
// Clearing a config is not allowed, as before.
|
||||
// Pass 2: renamed/moved locations may carry an existing config not already claimed, or
|
||||
// none at all
|
||||
for (const loc of unresolved) {
|
||||
if (!loc.advanced_config || !take(loc.advanced_config)) {
|
||||
if (loc.advanced_config && !take(loc.advanced_config)) {
|
||||
deny();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +157,9 @@ const internalProxyHost = {
|
||||
);
|
||||
}
|
||||
|
||||
// Before the certificate is created, so a refused request doesn't leave one behind
|
||||
internalHost.assertAdvancedConfigAllowed(access, thisData, row);
|
||||
|
||||
if (createCertificate) {
|
||||
return internalCertificate
|
||||
.createQuickCertificate(access, {
|
||||
@@ -185,8 +188,6 @@ const internalProxyHost = {
|
||||
|
||||
thisData = internalHost.cleanSslHstsData(thisData, row);
|
||||
|
||||
internalHost.assertAdvancedConfigAllowed(access, thisData, row);
|
||||
|
||||
return proxyHostModel
|
||||
.query()
|
||||
.where({ id: thisData.id })
|
||||
|
||||
@@ -157,6 +157,9 @@ const internalRedirectionHost = {
|
||||
);
|
||||
}
|
||||
|
||||
// Before the certificate is created, so a refused request doesn't leave one behind
|
||||
internalHost.assertAdvancedConfigAllowed(access, thisData, row);
|
||||
|
||||
if (createCertificate) {
|
||||
return internalCertificate
|
||||
.createQuickCertificate(access, {
|
||||
@@ -185,8 +188,6 @@ const internalRedirectionHost = {
|
||||
|
||||
thisData = internalHost.cleanSslHstsData(thisData, row);
|
||||
|
||||
internalHost.assertAdvancedConfigAllowed(access, thisData, row);
|
||||
|
||||
return redirectionHostModel
|
||||
.query()
|
||||
.where({ id: thisData.id })
|
||||
|
||||
@@ -3,6 +3,7 @@ import _ from "lodash";
|
||||
import errs from "../lib/error.js";
|
||||
import utils from "../lib/utils.js";
|
||||
import authModel from "../models/auth.js";
|
||||
import proxyHostModel from "../models/proxy_host.js";
|
||||
import userModel from "../models/user.js";
|
||||
import userPermissionModel from "../models/user_permission.js";
|
||||
import internalAuditLog from "./audit-log.js";
|
||||
@@ -460,7 +461,8 @@ const internalUser = {
|
||||
.query()
|
||||
.where("user_id", user.id)
|
||||
.first()
|
||||
.then((existing_auth) => {
|
||||
.then(async (existing_auth) => {
|
||||
permData.meta = await internalUser.normalisePermissionMeta(existing_auth, permData);
|
||||
if (existing_auth) {
|
||||
// patch
|
||||
return userPermissionModel
|
||||
@@ -489,6 +491,36 @@ const internalUser = {
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* The permission meta to store. Proxy host grants (meta.proxy_host_ids) only apply while
|
||||
* visibility is "user" and proxy hosts are not hidden, so they're cleared otherwise; kept,
|
||||
* they would silently come back when proxy hosts are shown again. Ids of hosts that no
|
||||
* longer exist are dropped. `permData` may be partial, so the stored values fill the gaps.
|
||||
*
|
||||
* @param {Object} [existing] the stored user_permission row
|
||||
* @param {Object} permData incoming permission fields
|
||||
* @returns {Promise<Object>}
|
||||
*/
|
||||
normalisePermissionMeta: async (existing, permData) => {
|
||||
const effective = _.assign(
|
||||
{ visibility: "user", proxy_hosts: "hidden" },
|
||||
existing || {},
|
||||
_.omit(permData, ["meta"]),
|
||||
);
|
||||
const meta = _.assign({}, existing?.meta || {}, permData.meta || {});
|
||||
const requested = Array.isArray(meta.proxy_host_ids) ? _.uniq(meta.proxy_host_ids) : [];
|
||||
|
||||
if (effective.visibility !== "user" || effective.proxy_hosts === "hidden" || !requested.length) {
|
||||
meta.proxy_host_ids = [];
|
||||
return meta;
|
||||
}
|
||||
|
||||
const rows = await proxyHostModel.query().select("id").where("is_deleted", 0).whereIn("id", requested);
|
||||
const found = new Set(rows.map((r) => r.id));
|
||||
meta.proxy_host_ids = requested.filter((id) => found.has(id));
|
||||
return meta;
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {Access} access
|
||||
* @param {Object} data
|
||||
|
||||
Reference in new issue
Block a user