Skip to content
Draft
Changes from 1 commit
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
Next Next commit
test(peer): refuse other spellings of our own address
Cover host and scheme case, percent-encoded host letters, leading zeros
in the port, explicit default ports, and our host and TCP port written
under the other scheme's default port.
  • Loading branch information
hackobi committed Nov 20, 2026
commit 66bf81607895b4409075db6fc99b5d762866bb44
90 changes: 90 additions & 0 deletions src/libs/peer/selfUrlAlias.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
* us.example.:53550 is our own address. Another identity advertising it, or
* our own address when we advertise the dotted form, must be refused like the
* exact spelling.
*
* Other spellings of our address are refused too: host and scheme case,
* percent-encoded host letters, leading zeros in the port and an explicit
* default port are folded by the URL parser, and the same host and TCP port
* written under the other scheme's default (https://us.example and
* http://us.example:443) is our address as well.
*/

import { afterEach, beforeEach, describe, expect, it } from "bun:test"
Expand All @@ -14,6 +20,8 @@ const ALICE = "0x" + "a1".repeat(32)
const OUR_URL = "http://us.example:53550"
const OUR_URL_DOTTED = "http://us.example.:53550"
const POINTS_AT_US = "Connection string points at us"
const OUR_URL_HTTP = "http://us.example"
const OUR_URL_HTTPS = "https://us.example"

const sharedState: Record<string, unknown> = {
publicKeyHex: OUR_KEY,
Expand Down Expand Up @@ -106,3 +114,85 @@ describe("addresses that are not ours", () => {
expect(manager.getPeer(OUR_KEY)?.connection.string).toBe(OUR_URL)
})
})

describe("other spellings of our address", () => {
it("upper- and mixed-case host is refused for another identity", () => {
expectRefused(ALICE, "http://US.EXAMPLE:53550")
expectRefused(ALICE, "http://US.Example:53550")
})

it("upper-case scheme is refused for another identity", () => {
expectRefused(ALICE, "HTTP://us.example:53550")
})

it("mixed case with a trailing dot is refused for another identity", () => {
expectRefused(ALICE, "http://Us.Example.:53550")
})

it("several trailing dots are refused for another identity", () => {
expectRefused(ALICE, "http://us.example..:53550")
})

it("a percent-encoded host letter is refused for another identity", () => {
expectRefused(ALICE, "http://%75s.example:53550")
})

it("a leading zero in the port is refused for another identity", () => {
expectRefused(ALICE, "http://us.example:053550")
})

it("an explicit http default port is refused when we advertise none", () => {
sharedState.exposedUrl = OUR_URL_HTTP
expectRefused(ALICE, "http://us.example:80")
})

it("an explicit https default port is refused when we advertise none", () => {
sharedState.exposedUrl = OUR_URL_HTTPS
expectRefused(ALICE, "https://us.example:443")
})

it("no port is refused when we advertise the explicit http default", () => {
sharedState.exposedUrl = "http://us.example:80"
expectRefused(ALICE, OUR_URL_HTTP)
})
})

describe("our host and TCP port under the other scheme's default port", () => {
it("http on 443 is refused when we advertise https", () => {
sharedState.exposedUrl = OUR_URL_HTTPS
expectRefused(ALICE, "http://us.example:443")
})

it("https is refused when we advertise http on 443", () => {
sharedState.exposedUrl = "http://us.example:443"
expectRefused(ALICE, OUR_URL_HTTPS)
})

it("https on 80 is refused when we advertise http", () => {
sharedState.exposedUrl = OUR_URL_HTTP
expectRefused(ALICE, "https://us.example:80")
})

it("http is refused when we advertise https on 80", () => {
sharedState.exposedUrl = "https://us.example:80"
expectRefused(ALICE, OUR_URL_HTTP)
})
})

describe("other spellings that are not ours", () => {
it("another host in upper case is accepted", () => {
const peer = new Peer("http://ALICE.Example:53550", ALICE)
expect(PeerManager.getInstance().canAddPeer(peer)).toEqual([true, ""])
})

it("our host on http 8443 is accepted when we advertise https", () => {
sharedState.exposedUrl = OUR_URL_HTTPS
const peer = new Peer("http://us.example:8443", ALICE)
expect(PeerManager.getInstance().canAddPeer(peer)).toEqual([true, ""])
})

it("our host over https is accepted when we advertise port 53550", () => {
const peer = new Peer(OUR_URL_HTTPS, ALICE)
expect(PeerManager.getInstance().canAddPeer(peer)).toEqual([true, ""])
})
})