Skip to content

Commit

Permalink
Merge pull request #27 from planetary-social/fix_underscore_name_with…
Browse files Browse the repository at this point in the history
…_root_domain

Fix underscore name for a root domain
  • Loading branch information
dcadenas committed Jul 16, 2024
2 parents 9339e86 + bb29b90 commit 5e0dd67
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @type {import('jest').Config} */

export default {
modulePathIgnorePatterns: ["./config/"],
modulePathIgnorePatterns: ["<rootDir>/config/"],
coveragePathIgnorePatterns: ["<rootDir>/config/"],
coverageThreshold: {
global: {
branches: 75,
Expand Down
24 changes: 12 additions & 12 deletions src/middlewares/extractValidatedName.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@ function extractName(req) {
validateDomain(host);

const nonRootSubdomains = host.split(`.${config.rootDomain}`).find(Boolean);

const nameFromQueryOrBody = getNameFromReq(req);

let name = nameFromQueryOrBody;
if (nameFromQueryOrBody === "_") {
name = validateAndReturnSubdomain(nonRootSubdomains);
if (!nonRootSubdomains) {
throw new AppError(
422,
"The _ format requires a corresponding subdomain as the NIP-05 name."
);
}

if (nonRootSubdomains === config.rootDomain) {
name = "_";
} else {
name = nonRootSubdomains;
}
}

return validateName(name);
Expand Down Expand Up @@ -56,13 +66,3 @@ function validateDomain(host) {
);
}
}

function validateAndReturnSubdomain(nonRootSubdomains) {
if (!nonRootSubdomains) {
throw new AppError(
422,
"The _ format requires a corresponding subdomain as the NIP-05 name."
);
}
return nonRootSubdomains;
}
18 changes: 11 additions & 7 deletions src/nameRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ export function validateName(name) {
throw new AppError(422, "Name is required.");
}

if (name.length < 3) {
throw new AppError(
422,
`Name '${name}' should have more than 3 characters.`
);
}

if (name.startsWith("-")) {
throw new AppError(422, `Name '${name}' should not start with a hyphen -.`);
}
Expand All @@ -44,6 +37,17 @@ export function validateName(name) {
throw new AppError(422, `Name '${name}' should not start with a hyphen -.`);
}

if (name === "_") {
return name;
}

if (name.length < 3) {
throw new AppError(
422,
`Name '${name}' should have more than 3 characters.`
);
}

if (name.includes("_")) {
throw new AppError(
422,
Expand Down
36 changes: 36 additions & 0 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ describe("Nostr NIP 05 API tests", () => {
.send(userData)
.expect(200);

await request(app)
.get("/.well-known/webfinger")
.set("Host", "nos.social")
.query({ resource: "acctWRONG:[email protected]" })
.expect(422);

const getResponse = await request(app)
.get("/.well-known/webfinger")
.set("Host", "nos.social")
Expand Down Expand Up @@ -302,6 +308,36 @@ describe("Nostr NIP 05 API tests", () => {
});
});

it("should store and retrieve Nostr NIP 05 data through an empty subdomain", async () => {
const userData = createUserPayload({ name: "_" });

await request(app)
.post("/api/names")
.set("Host", "nos.social")
.set("Authorization", `Nostr ${nip98PostAuthToken}`)
.send(userData)
.expect(200);

const getResponse = await request(app)
.get("/.well-known/nostr.json")
.set("Host", "nos.social")
.query({ name: "_" })
.expect(200);

const jsonResponse = JSON.parse(getResponse.text);

expect(jsonResponse).toEqual({
names: { _: config.servicePubkey },
relays: {
[config.servicePubkey]: [
"wss://relay1.com",
"wss://relay2.com",
"wss://relay.nos.social",
],
},
});
});

it("should not use components of the root domain as a subdomain", async () => {
const userData = createUserPayload({ name: "nos" });

Expand Down

0 comments on commit 5e0dd67

Please sign in to comment.