Skip to content

Commit 1a4f250

Browse files
author
cyz
committed
fix(ai): reject zero-sided aspect ratios
Validate positive sides in Media.AspectRatio so every provider rejects 0:0 and 1:0 at the request boundary instead of only the routes that call ImageSizing.exact(). Drop the duplicate aspect checks from the sizing helper.
1 parent b11cb44 commit 1a4f250

3 files changed

Lines changed: 19 additions & 24 deletions

File tree

‎packages/ai/src/media.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ export type Source = Schema.Schema.Type<typeof Source>
5656

5757
export type AspectRatio = `${number}:${number}`
5858
export const AspectRatio = Schema.declare<AspectRatio>(
59-
(value): value is AspectRatio => typeof value === "string" && /^\d+(?:\.\d+)?:\d+(?:\.\d+)?$/.test(value),
59+
(value): value is AspectRatio => {
60+
if (typeof value !== "string" || !/^\d+(?:\.\d+)?:\d+(?:\.\d+)?$/.test(value)) return false
61+
const [width, height] = value.split(":").map(Number)
62+
return width > 0 && height > 0
63+
},
6064
{ title: "Media.AspectRatio" },
6165
)
6266

‎packages/ai/src/protocols/utils/image-sizing.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ export const nearest = (shape: Shape, sizes: ReadonlyArray<Size>): Size => {
110110
/** Free width and height that must honor the shape; a size that misses it fails. */
111111
export const exact = (route: MediaProtocol.Identity, shape: Shape, limits: Limits) => {
112112
if (!isRequested(shape)) return Effect.succeed(undefined)
113-
if (shape.aspect !== undefined && !validAspect(shape.aspect))
114-
return Effect.fail(
115-
route.unsupported("media.aspect", `${route.name} needs an aspect ratio with both sides greater than zero`),
116-
)
117113
const size = fit(shape, limits)
118114
const requested = missed(shape, size)
119115
if (requested === undefined) return Effect.succeed(size)
@@ -138,14 +134,7 @@ const missed = (shape: Shape, size: Size) => {
138134
return [shape.aspect, shape.resolution].filter((value) => value !== undefined).join(" at ")
139135
}
140136

141-
const validAspect = (aspect: string) => {
142-
const [width, height] = aspect.split(":").map(Number)
143-
return Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0
144-
}
145-
146-
// A malformed shape degrades to the default rather than producing NaN; `exact` rejects it before sending.
147137
const aspectRatio = (shape: Shape) => {
148-
if (shape.aspect !== undefined && !validAspect(shape.aspect)) return 1
149138
const [width, height] = (shape.aspect ?? "1:1").split(":").map(Number)
150139
return width / height
151140
}

‎packages/ai/test/image-sizing.test.ts‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, test } from "bun:test"
2-
import { Effect, Exit } from "effect"
2+
import { Effect, Exit, Schema } from "effect"
3+
import { Media } from "../src/media.js"
34
import { ImageSizing } from "../src/protocols/utils/image-sizing.js"
45
import { MediaProtocol } from "../src/route/media-protocol.js"
56

@@ -47,17 +48,18 @@ describe("ImageSizing", () => {
4748
expect(size.width * size.height).toBeLessThanOrEqual(GPT_IMAGE_2.maxPixels)
4849
})
4950

50-
test("rejects degenerate shapes instead of producing NaN", () => {
51-
for (const shape of [{ aspect: "0:0" }, { aspect: "1:0" }, { resolution: "0K" }]) {
52-
const size = ImageSizing.fit(shape, { multiple: 16 })
53-
expect(Number.isFinite(size.width)).toBe(true)
54-
expect(Number.isFinite(size.height)).toBe(true)
55-
expect(size.width).toBeGreaterThan(0)
56-
expect(size.height).toBeGreaterThan(0)
57-
}
58-
for (const shape of [{ aspect: "0:0" }, { aspect: "1:0" }, { resolution: "0K" }]) {
59-
expect(Exit.isFailure(Effect.runSync(Effect.exit(ImageSizing.exact(route, shape, { multiple: 16 }))))).toBe(true)
60-
}
51+
test("rejects zero-sided aspect ratios at the schema boundary", () => {
52+
const decode = Schema.decodeUnknownSync(Media.AspectRatio)
53+
expect(decode("16:9")).toBe("16:9")
54+
expect(() => decode("0:0")).toThrow()
55+
expect(() => decode("1:0")).toThrow()
56+
})
57+
58+
test("ignores non-positive resolution tiers instead of producing NaN", () => {
59+
expect(ImageSizing.fit({ resolution: "0K" }, { multiple: 16 })).toEqual({ width: 1024, height: 1024 })
60+
expect(Exit.isFailure(Effect.runSync(Effect.exit(ImageSizing.exact(route, { resolution: "0K" }, { multiple: 16 }))))).toBe(
61+
true,
62+
)
6163
})
6264

6365
test("picks the nearest listed size by aspect, then by pixel count", () => {

0 commit comments

Comments
 (0)