mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2026-11-20 23:22:50 +00:00
fix(review): don't block saving because of a disabled location's host
Problem: validateLocations() ran the host format checks (whitespace,
characters such as ; $ { }, invalid characters in the host/path part) on
every non-blank row, also on rows switched off with `enabled: false`. The
backend never renders a disabled location (preflightLocation() stops at
`enabled: false`) and the API accepts such a host, so a stored location with
an invalid host could be disabled safely, yet the editor refused to save
the host until the disabled row was fixed or deleted.
Fix: a disabled row only gets the checks the API itself makes for every
row: required path and host, the port range, and duplicate paths (the
backend's findDuplicatePath() counts disabled rows too). Switching the row
back on brings the format errors back, because validation runs on every
change.
Tests: validateLocations cases for disabled rows (format errors skipped,
required / port / duplicate checks kept, the same rows enabled still
invalid) and a form test: a disabled row with a stored `app;legacy` host
submits, after switching it on the submit is blocked again.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
07917e1893
commit
b7720afab4
2 changed files with 45 additions and 0 deletions
|
|
@ -68,6 +68,35 @@ describe("validateLocations", () => {
|
|||
expect(validateLocations([loc({ forwardHost: " app/sub/ " })])[0]).toBeUndefined();
|
||||
});
|
||||
|
||||
it("skips the format checks of a disabled row, the backend never renders it", () => {
|
||||
const hosts = ["app;evil", "my app", "app/a b", "app$x"];
|
||||
expect(validateLocations(hosts.map((forwardHost, i) => loc({ path: `/d${i}`, enabled: false, forwardHost })))).toEqual(
|
||||
hosts.map(() => undefined),
|
||||
);
|
||||
// the same rows switched on (or without `enabled`) still have errors
|
||||
for (const enabled of [true, undefined]) {
|
||||
const errors = validateLocations(hosts.map((forwardHost, i) => loc({ path: `/d${i}`, enabled, forwardHost })));
|
||||
expect(errors.every((e) => e?.forwardHost)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the checks the API makes for every row on a disabled row", () => {
|
||||
const [host, low, high, path] = validateLocations([
|
||||
loc({ path: "/a", enabled: false, forwardHost: "" }),
|
||||
loc({ path: "/b", enabled: false, forwardPort: 0 }),
|
||||
loc({ path: "/c", enabled: false, forwardPort: 70000 }),
|
||||
loc({ path: "", enabled: false }),
|
||||
]);
|
||||
expect(host?.forwardHost).toBe("This is required");
|
||||
expect(low?.forwardPort).toMatch(/1/);
|
||||
expect(high?.forwardPort).toMatch(/65535/);
|
||||
expect(path?.path).toBe("This is required");
|
||||
// the backend rejects duplicate paths among disabled rows too
|
||||
const [, dup] = validateLocations([loc({ path: "/x" }), loc({ path: "/x", enabled: false, forwardHost: "a b" })]);
|
||||
expect(dup?.path).toMatch(/already uses/);
|
||||
expect(dup?.forwardHost).toBeUndefined();
|
||||
});
|
||||
|
||||
it("requires a host part, not just a path", () => {
|
||||
// the server would store an empty forward host and quarantine the location
|
||||
for (const forwardHost of ["/api", " /x", "/", " "]) {
|
||||
|
|
@ -236,6 +265,16 @@ describe("LocationsFields", () => {
|
|||
expect(screen.getAllByLabelText("This location has errors").length).toBe(2);
|
||||
});
|
||||
|
||||
it("saves a disabled row whose stored host is invalid, and blocks again once it is switched on", async () => {
|
||||
const { onSubmit } = renderFields([loc({ path: "/old", enabled: false, forwardHost: "app;legacy" })]);
|
||||
fireEvent.click(screen.getByText("save"));
|
||||
await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
|
||||
fireEvent.click(screen.getByLabelText("Enable or disable this location"));
|
||||
fireEvent.click(screen.getByText("save"));
|
||||
await waitFor(() => expect(screen.getByText(/The host contains characters that are not allowed/)).toBeInTheDocument());
|
||||
expect(onSubmit).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("submits valid locations, storing the port as a number", async () => {
|
||||
const { onSubmit } = renderFields([loc({ path: "/a" })]);
|
||||
fireEvent.click(screen.getByText("/a"));
|
||||
|
|
|
|||
|
|
@ -76,8 +76,14 @@ export const validateLocations = (items: ProxyLocation[] = []): (LocationErrors
|
|||
const host = `${item.forwardHost ?? ""}`.trim();
|
||||
const hostPart = locationHostPart(host);
|
||||
const pathPart = host.slice(hostPart.length);
|
||||
// A disabled row is never rendered (the backend's preflightLocation() stops at `enabled: false`), so it only
|
||||
// gets the checks the API itself makes for every row: required host, port range and duplicate paths.
|
||||
// Switching it back on brings the format errors back.
|
||||
const isEnabled = item.enabled !== false;
|
||||
if (!hostPart) {
|
||||
errors.forwardHost = intl.formatMessage({ id: "error.required" });
|
||||
} else if (!isEnabled) {
|
||||
// no format checks
|
||||
} else if (/\s/.test(hostPart)) {
|
||||
errors.forwardHost = intl.formatMessage({ id: "location.error.host-whitespace" });
|
||||
} else if (!isVariableHost(hostPart) && INVALID_HOST_CHARS.test(hostPart)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue