Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Prevent 4-byte character input in entry form validation #1385

Merged
merged 1 commit into from
Mar 10, 2025
Merged
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
41 changes: 41 additions & 0 deletions frontend/src/components/entry/entryForm/EntryFormSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,47 @@ describe("schema", () => {
expect(() => schema.parse(value)).toThrow();
});

test("validation fails if name contains 4-byte characters", () => {
const value = {
...baseValue,
name: "テスト😊", // Contains emoji (4-byte character)
};

expect(() => schema.parse(value)).toThrow();
});

test("validation fails if string attribute value contains 4-byte characters", () => {
const value = {
...baseValue,
attrs: {
string: {
...baseValue.attrs.string,
value: {
asString: "テスト🚀", // Contains emoji (4-byte character)
},
},
},
};

expect(() => schema.parse(value)).toThrow();
});

test("validation fails if array string attribute value contains 4-byte characters", () => {
const value = {
...baseValue,
attrs: {
arrayString: {
...baseValue.attrs.arrayString,
value: {
asArrayString: [{ value: "テスト🎮" }], // Contains emoji (4-byte character)
},
},
},
};

expect(() => schema.parse(value)).toThrow();
});

test("validation fails if array-string attr value is mandatory and empty", () => {
const value = {
...baseValue,
Expand Down
84 changes: 73 additions & 11 deletions frontend/src/components/entry/entryForm/EntryFormSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ import { EditableEntry } from "./EditableEntry";
import { AttributeTypes } from "services/Constants";
import { schemaForType } from "services/ZodSchemaUtil";

// Function to detect 4-byte characters (characters outside the BMP - Basic Multilingual Plane)
const hasFourByteChars = (value: string): boolean => {
// Check for surrogate pairs (4-byte characters are represented as surrogate pairs in UTF-16)
for (let i = 0; i < value.length; i++) {
const code = value.charCodeAt(i);
if (code >= 0xd800 && code <= 0xdbff) {
// High surrogate
if (i + 1 < value.length) {
const nextCode = value.charCodeAt(i + 1);
if (nextCode >= 0xdc00 && nextCode <= 0xdfff) {
// Low surrogate - this is a 4-byte character (surrogate pair)
return true;
}
}
}
}
return false;
};

// A schema that's compatible with existing types
// TODO rethink it, e.g. consider to use union as a type of value
export const schema = schemaForType<EditableEntry>()(
Expand All @@ -15,6 +34,9 @@ export const schema = schemaForType<EditableEntry>()(
.trim()
.min(1, "アイテム名は必須です")
.max(200, "アイテム名が大きすぎます")
.refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
})
.default(""),
schema: z.object({
id: z.number(),
Expand All @@ -38,37 +60,57 @@ export const schema = schemaForType<EditableEntry>()(
asString: z
.string()
.max(1 << 16, "属性の値が大きすぎます")
.refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
})
.default("")
.optional(),
asArrayString: z
.array(
z.object({
value: z.string().max(1 << 16, "属性の値が大きすぎます"),
value: z
.string()
.max(1 << 16, "属性の値が大きすぎます")
.refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
}),
)
.optional(),
asObject: z
.object({
id: z.number(),
name: z.string(),
name: z.string().refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
})
.nullable()
.optional(),
asArrayObject: z
.array(
z.object({
id: z.number(),
name: z.string(),
name: z
.string()
.refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
}),
)
.optional(),
asNamedObject: z
.object({
name: z.string(),
name: z.string().refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
object: z
.object({
id: z.number(),
name: z.string(),
name: z
.string()
.refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
})
.nullable()
.default(null),
Expand All @@ -77,11 +119,19 @@ export const schema = schemaForType<EditableEntry>()(
asArrayNamedObject: z
.array(
z.object({
name: z.string(),
name: z
.string()
.refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
object: z
.object({
id: z.number(),
name: z.string(),
name: z
.string()
.refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
})
.nullable()
.default(null),
Expand All @@ -92,30 +142,42 @@ export const schema = schemaForType<EditableEntry>()(
asGroup: z
.object({
id: z.number(),
name: z.string(),
name: z.string().refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
})
.nullable()
.optional(),
asArrayGroup: z
.array(
z.object({
id: z.number(),
name: z.string(),
name: z
.string()
.refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
}),
)
.optional(),
asRole: z
.object({
id: z.number(),
name: z.string(),
name: z.string().refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
})
.nullable()
.optional(),
asArrayRole: z
.array(
z.object({
id: z.number(),
name: z.string(),
name: z
.string()
.refine((value) => !hasFourByteChars(value), {
message: "使用できない文字が含まれています",
}),
}),
)
.optional(),
Expand Down
Loading