diff --git a/src/features/pet/types.test.ts b/src/features/pet/types.test.ts new file mode 100644 index 0000000..57ddb2d --- /dev/null +++ b/src/features/pet/types.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, it } from "vitest"; + +import aemeathManifest from "./assets/aemeath/manifest.json"; +import { petPackManifestSchema } from "./types"; + +describe("pet pack manifest schema", () => { + // Regression: the image pattern used `\\.` instead of `\.`, which required a + // literal backslash in the file name and therefore rejected every valid pack, + // including the one shipped in this repository. + it("accepts the bundled aemeath manifest", () => { + expect(petPackManifestSchema.parse(aemeathManifest).image).toBe( + "aemeath-spritesheet.png", + ); + }); + + it("accepts plain png and webp file names", () => { + for (const image of ["pet.png", "pet.webp", "my-pet_v2.PNG"]) { + expect( + petPackManifestSchema.parse({ ...aemeathManifest, image }).image, + ).toBe(image); + } + }); + + it("rejects path traversal and unsupported extensions", () => { + for (const image of [ + "../escape.png", + "nested/pet.png", + "C:\\pet.png", + "pet.gif", + "pet.png.exe", + ]) { + expect(() => + petPackManifestSchema.parse({ ...aemeathManifest, image }), + ).toThrow(); + } + }); +}); diff --git a/src/features/pet/types.ts b/src/features/pet/types.ts index 92dbe01..4f0d6a8 100644 --- a/src/features/pet/types.ts +++ b/src/features/pet/types.ts @@ -44,7 +44,7 @@ export const petPackManifestSchema = z format: z.literal("astrbot-pet-pack.v1"), name: z.string().min(1).max(80), version: z.string().min(1).max(32), - image: z.string().regex(/^[^\\/:*?"<>|]+\\.(png|webp)$/i), + image: z.string().regex(/^[^\\/:*?"<>|]+\.(png|webp)$/i), width: z.number().int().positive().max(4096), height: z.number().int().positive().max(4096), frameWidth: z.number().int().positive().max(1024),