Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export function safeDomain(dn: unknown): string | null {
.replace(/^https?:\/\//, "")
.replace(/[/?#&].*$/, "");
if (!/^[a-z0-9.-]{3,253}$/.test(d) || !d.includes(".") || d.includes("..")) return null;
if (d.startsWith(".") || d.endsWith(".")) return null;
if (d.split(".").some((label) => label.startsWith("-") || label.endsWith("-"))) return null;
return d;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/safe-domain.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import assert from "node:assert/strict";
import test from "node:test";

import { safeDomain } from "../lib/config.ts";

test("safeDomain strips forwarded paths and accepts ordinary domains", () => {
assert.equal(safeDomain("https://example.com/path?ref=abc"), "example.com");
assert.equal(safeDomain("Sub.Example.COM#top"), "sub.example.com");
});

test("safeDomain rejects empty labels and edge punctuation", () => {
assert.equal(safeDomain(".example.com"), null);
assert.equal(safeDomain("example.com."), null);
assert.equal(safeDomain("example..com"), null);
assert.equal(safeDomain("-example.com"), null);
assert.equal(safeDomain("example-.com"), null);
assert.equal(safeDomain("example.-com"), null);
assert.equal(safeDomain("example.com-"), null);
});
Loading