Skip to content

Commit b3d00ed

Browse files
authored
Merge pull request #102 from Sythos/enya/issue-97-p2-validate-kartrak-image-dimensions-bef
[P2] Validate KarTrak image dimensions before allocation
2 parents 3f286b4 + 9c4cfba commit b3d00ed

8 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎SECURITY.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ or the npm/GitHub package publication path.
8585

8686
Camera and file rasters are untrusted input. The decoder accepts only byte-valued channels and
8787
positive safe-integer dimensions, with a maximum of 16,384 pixels per side and 16,777,216 pixels
88-
in total. Renderers enforce the same final-image limits before allocating output and reject invalid,
88+
in total. The direct KarTrak detector applies the same checks, and the buffer length check, before it allocates or scans. Renderers enforce the same final-image limits before allocating output and reject invalid,
8989
fractional or oversized `scale`, `margin` and `barHeight` values.
9090

9191
Colour grid sampling (`classifyGrid` and `decodeJABCode`) rejects non-finite corners and transform

‎bundle/sythos-barcode.esm.js‎

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bundle/sythos-barcode.js‎

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/js/image/luminance.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ function validateByteData(data, expected, label) {
7474
}
7575
}
7676
}
77+
/**
78+
* Check RGBA image metadata and buffer before any allocation or scan.
79+
* Throws `NotFoundError` when the image is not valid or is too large.
80+
*
81+
* @param {ImageLike} image
82+
* @returns {number} The pixel count.
83+
*/
84+
export function validateImageData(image) {
85+
if (!image || typeof image !== 'object') {
86+
throw new NotFoundError('Image must be an object with RGBA data');
87+
}
88+
const { data, width, height } = image;
89+
const pixels = validateDimensions(width, height);
90+
validateByteData(data, pixels * 4, 'Image data');
91+
return pixels;
92+
}
7793
/**
7894
* @typedef {object} ImageLike
7995
* @property {Uint8ClampedArray | Uint8Array | number[]} data RGBA, 4 bytes per pixel.

‎src/js/kartrak/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import { PolychromeMatrix } from '../color/matrix.js';
5959
import { classifyGrid } from '../color/classify.js';
6060
import { toColorImageData } from '../color/render.js';
6161
import { PerspectiveTransform } from '../image/perspective.js';
62+
import { validateImageData } from '../image/luminance.js';
6263
// Re-exported for convenience, so a consumer of this format doesn't need a
6364
// second import from the separate (still experimental) `../color/`
6465
// subpath just to render or hold the matrix this module produces.
@@ -331,6 +332,7 @@ function findBounds(image, palette) {
331332
export function detectKarTrak(image, options = {}) {
332333
if (!image || typeof image.width !== 'number' || typeof image.height !== 'number' || !image.data)
333334
return null;
335+
validateImageData(image);
334336
const palette = options.palette ?? KARTRAK_PALETTE;
335337
const bounds = findBounds(image, palette);
336338
if (!bounds)

‎src/ts/image/luminance.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ function validateByteData(data, expected, label) {
8585
}
8686
}
8787

88+
/**
89+
* Check RGBA image metadata and buffer before any allocation or scan.
90+
* Throws `NotFoundError` when the image is not valid or is too large.
91+
*
92+
* @param {ImageLike} image
93+
* @returns {number} The pixel count.
94+
*/
95+
export function validateImageData(image) {
96+
if (!image || typeof image !== 'object') {
97+
throw new NotFoundError('Image must be an object with RGBA data');
98+
}
99+
const { data, width, height } = image;
100+
const pixels = validateDimensions(width, height);
101+
validateByteData(data, pixels * 4, 'Image data');
102+
return pixels;
103+
}
104+
88105
/**
89106
* @typedef {object} ImageLike
90107
* @property {Uint8ClampedArray | Uint8Array | number[]} data RGBA, 4 bytes per pixel.

‎src/ts/kartrak/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { PolychromeMatrix, type RGB } from '../color/matrix.js';
6161
import { classifyGrid } from '../color/classify.js';
6262
import { toColorImageData } from '../color/render.js';
6363
import { PerspectiveTransform } from '../image/perspective.js';
64+
import { validateImageData } from '../image/luminance.js';
6465

6566
// Re-exported for convenience, so a consumer of this format doesn't need a
6667
// second import from the separate (still experimental) `../color/`
@@ -364,6 +365,7 @@ export function detectKarTrak(
364365
options: KarTrakDetectOptions = {},
365366
): KarTrakResult | null {
366367
if (!image || typeof image.width !== 'number' || typeof image.height !== 'number' || !image.data) return null;
368+
validateImageData(image);
367369
const palette = options.palette ?? KARTRAK_PALETTE;
368370
const bounds = findBounds(image, palette);
369371
if (!bounds) return null;

‎test/kartrak.test.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,13 @@ test('decodeKarTrak dispatches on both a matrix and a raw image, and throws when
200200
const blank = { data: new Uint8ClampedArray(64 * 64 * 4).fill(200), width: 64, height: 64 };
201201
assert.throws(() => decodeKarTrak(blank), /was not found/);
202202
});
203+
204+
test('detectKarTrak rejects invalid dimensions and short buffers before it scans', () => {
205+
const data = new Uint8ClampedArray(16);
206+
const sizes = [[0, 4], [4, 0], [-1, 4], [1.5, 2], [NaN, 4], [Infinity, 4], [Number.MAX_SAFE_INTEGER, 1], [16385, 1], [8192, 8192]];
207+
for (const [width, height] of sizes) {
208+
assert.throws(() => detectKarTrak({ data, width, height }), /Image (dimensions|contains)/, `${width}x${height}`);
209+
}
210+
assert.throws(() => detectKarTrak({ data: new Uint8ClampedArray(60), width: 4, height: 4 }), /shorter than the required 64/);
211+
assert.throws(() => detectKarTrak({ data: 'abcd', width: 1, height: 1 }), /must be a Uint8Array/);
212+
});

0 commit comments

Comments
 (0)