Compare commits

...

1 Commits

Author SHA1 Message Date
CuriousCorrelation 44b695cdee feat: Add checks to varify duplicate domains 2022-07-17 17:48:38 +05:30
3 changed files with 53 additions and 1 deletions
+28
View File
@@ -35,6 +35,34 @@ describe("team domain model", () => {
expect(error).toBeDefined();
});
it("should not allow duplicate domains", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
let error;
try {
await TeamDomain.create({
teamId: team.id,
name: "getoutline.com",
createdById: user.id,
});
await TeamDomain.create({
teamId: team.id,
name: "www.getoutline.com",
createdById: user.id,
});
await TeamDomain.create({
teamId: team.id,
name: "https://www.getoutline.com",
createdById: user.id,
});
} catch (err) {
error = err;
}
expect(error.name).toBe("SequelizeUniqueConstraintError");
expect(error).toBeDefined();
});
it("should not allow creation of domains within restricted list", async () => {
const team = await buildTeam();
const user = await buildAdmin({ teamId: team.id });
+2 -1
View File
@@ -11,6 +11,7 @@ import {
} from "sequelize-typescript";
import { MAX_TEAM_DOMAINS } from "@shared/constants";
import { ValidationError } from "@server/errors";
import { normalizeURL } from "@server/utils/normalizeURL";
import Team from "./Team";
import User from "./User";
import IdModel from "./base/IdModel";
@@ -51,7 +52,7 @@ class TeamDomain extends IdModel {
@BeforeValidate
static async cleanupDomain(model: TeamDomain) {
model.name = model.name.toLowerCase().trim();
model.name = normalizeURL(model.name.toLowerCase().trim());
}
@BeforeCreate
+23
View File
@@ -0,0 +1,23 @@
export const normalizeURL = (urlString: string): string => {
urlString = urlString.trim();
const hasRelativeProtocol = urlString.startsWith("//");
const isRelativeURL = !hasRelativeProtocol && /^\.*\//.test(urlString);
if (!isRelativeURL) {
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, "https://");
}
const urlObject = new URL(urlString);
// Remove 'www'
urlObject.hostname = urlObject.hostname.replace(/^www\./, "");
// Remove trailing `/`
const newURLString = urlObject.toString().replace(/\/$/, "");
// Remove protocol
const finalURL = newURLString.replace(/^(?:https?:)?\/\//, "");
return finalURL;
};