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
22 changes: 11 additions & 11 deletions backend/__tests__/api/controllers/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe("user controller test", () => {
],
});
});
it("should fail if username contains profanity", async () => {
it("should fail if username contains bad word", async () => {
//GIVEN
const newUser = {
uid: uid,
Expand All @@ -212,7 +212,7 @@ describe("user controller test", () => {
expect(body).toEqual({
message: "Invalid request data schema",
validationErrors: [
'"name" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
'"name" Unallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
],
});
});
Expand Down Expand Up @@ -277,7 +277,7 @@ describe("user controller test", () => {
});
expect(userIsNameAvailableMock).toHaveBeenCalledWith("bob", uid);
});
it("returns 422 if username contains profanity", async () => {
it("returns 422 if username contains bad word", async () => {
await mockApp
.get("/users/checkName/newMiodec")
//no authentication required
Expand Down Expand Up @@ -1139,7 +1139,7 @@ describe("user controller test", () => {
validationErrors: ["Unrecognized key(s) in object: 'extra'"],
});
});
it("should fail if username contains profanity", async () => {
it("should fail if username contains bad word", async () => {
//WHEN
const { body } = await mockApp
.patch("/users/name")
Expand All @@ -1151,7 +1151,7 @@ describe("user controller test", () => {
expect(body).toEqual({
message: "Invalid request data schema",
validationErrors: [
'"name" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
'"name" Unallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
],
});
});
Expand Down Expand Up @@ -3310,7 +3310,7 @@ describe("user controller test", () => {
expect.objectContaining({}),
);
});
it("should fail with profanity", async () => {
it("should fail with bad word", async () => {
//WHEN
const { body } = await mockApp
.patch("/users/profile")
Expand All @@ -3330,11 +3330,11 @@ describe("user controller test", () => {
expect(body).toEqual({
message: "Invalid request data schema",
validationErrors: [
'"bio" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
'"keyboard" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
'"socialProfiles.twitter" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
'"socialProfiles.github" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (miodec)',
'"socialProfiles.website" Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (https://i-luv-miodec.com)',
'"bio" Unallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
'"keyboard" Unallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
'"socialProfiles.twitter" Unallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
'"socialProfiles.github" Unallowed word detected. Please remove it. If you believe this is a mistake, please contact us (miodec).',
'"socialProfiles.website" Unallowed word detected. Please remove it. If you believe this is a mistake, please contact us (https://i-luv-miodec.com).',
],
});
});
Expand Down
6 changes: 4 additions & 2 deletions packages/contracts/__test__/validation/validation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { describe, it, expect } from "vitest";
import * as Validation from "@monkeytype/schemas/validation/validation";

const containsBadWords = Validation.__testing.containsBadWords;

describe("validation", () => {
it("containsProfanity", () => {
it("containsBadWords", () => {
const testCases = [
{
text: "https://www.fuckyou.com",
Expand Down Expand Up @@ -35,7 +37,7 @@ describe("validation", () => {
];

testCases.forEach((testCase) => {
expect(Validation.containsProfanity(testCase.text, "substring")).toBe(
expect(containsBadWords(testCase.text, "substring")).toBe(
testCase.expected,
);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/schemas/src/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PersonalBestSchema,
} from "./shared";
import { CustomThemeColorsSchema, FunboxNameSchema } from "./configs";
import { doesNotContainProfanity } from "./validation/validation";
import { doesNotContainBadWords } from "./validation/validation";
import { ConnectionSchema } from "./connections";

const NoneFilterSchema = z.literal("none");
Expand Down Expand Up @@ -85,7 +85,7 @@ export type UserTag = z.infer<typeof UserTagSchema>;
function profileDetailsBase(
schema: ZodString,
): ZodEffects<ZodOptional<ZodEffects<ZodString>>> {
return doesNotContainProfanity("word", schema)
return doesNotContainBadWords("word", schema)
.optional()
.transform((value) => (value === null ? undefined : value));
}
Expand Down Expand Up @@ -242,7 +242,7 @@ export const FavoriteQuotesSchema = z.record(
export type FavoriteQuotes = z.infer<typeof FavoriteQuotesSchema>;

export const UserEmailSchema = z.string().email();
export const UserNameSchema = doesNotContainProfanity(
export const UserNameSchema = doesNotContainBadWords(
"substring",
z
.string()
Expand Down
23 changes: 11 additions & 12 deletions packages/schemas/src/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { replaceHomoglyphs } from "./homoglyphs";
import { ZodEffects, ZodString } from "zod";

// Sorry for the bad words
const profanities = [
const badWords = [
"miodec",
"bitly",
"niqqa",
Expand Down Expand Up @@ -403,38 +403,37 @@ function sanitizeString(str: string | undefined): string | undefined {
.replace(/\s{3,}/g, " ");
}

export function containsProfanity(
text: string,
mode: "word" | "substring",
): boolean {
function containsBadWords(text: string, mode: "word" | "substring"): boolean {
const normalizedText = text
.toLowerCase()
.split(/[.,"/#!?$%^&*;:{}=\-_`~()\s\n]+/g)
.map((str) => {
return replaceHomoglyphs(sanitizeString(str) ?? "");
});

const hasProfanity = profanities.some((profanity) => {
const hasBadWords = badWords.some((badWord) => {
return normalizedText.some((word) => {
return mode === "word"
? word.startsWith(profanity)
: word.includes(profanity);
? word.startsWith(badWord)
: word.includes(badWord);
});
});

return hasProfanity;
return hasBadWords;
}

export function doesNotContainProfanity(
export function doesNotContainBadWords(
mode: "word" | "substring",
schema: ZodString,
): ZodEffects<ZodString> {
return schema.refine(
(val) => {
return !containsProfanity(val, mode);
return !containsBadWords(val, mode);
},
(val) => ({
message: `Profanity detected. Please remove it. If you believe this is a mistake, please contact us. (${val})`,
message: `Unallowed word detected. Please remove it. If you believe this is a mistake, please contact us (${val}).`,
}),
);
}

export const __testing = { containsBadWords };
Loading