Skip to content

Commit 3f286b4

Browse files
authored
Merge pull request #101 from Sythos/enya/issue-96-p2-bound-color-sampling-loops-before-usi
[P2] Bound color sampling loops before using caller geometry
2 parents d372bdc + 5903847 commit 3f286b4

5 files changed

Lines changed: 43 additions & 10 deletions

File tree

‎SECURITY.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ positive safe-integer dimensions, with a maximum of 16,384 pixels per side and 1
8888
in total. Renderers enforce the same final-image limits before allocating output and reject invalid,
8989
fractional or oversized `scale`, `margin` and `barHeight` values.
9090

91+
Colour grid sampling (`classifyGrid` and `decodeJABCode`) rejects non-finite corners and transform
92+
output, and rejects sample centres outside the image before any integer conversion. The sample
93+
window radius has a fixed limit of 16 pixels, so the work for each module has a fixed bound.
94+
9195
The browser examples render scanned payloads and browser error messages as DOM text, not HTML.
9296
Applications using decoded data should preserve that boundary and apply their own scheme and
9397
business-policy checks before acting on a scanned value.

‎src/js/color/classify.js‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ function nearestPaletteIndex(sample, palette) {
8181
}
8282
return best;
8383
}
84+
/** Upper limit of the sample window radius, in image pixels. */
85+
const MAX_SAMPLE_RADIUS = 16;
8486
/**
8587
* Sample and classify a `width` x `height` grid.
8688
*
@@ -101,16 +103,23 @@ export function classifyGrid(image, width, height, transform, palette) {
101103
const p2 = transform.transformPoint(0.5, 1.5);
102104
const stepX = Math.hypot(p1.x - p0.x, p1.y - p0.y);
103105
const stepY = Math.hypot(p2.x - p0.x, p2.y - p0.y);
104-
const rx = Math.max(1, Math.round(stepX / 4));
105-
const ry = Math.max(1, Math.round(stepY / 4));
106+
if (!Number.isFinite(stepX) || !Number.isFinite(stepY)) {
107+
throw new NotFoundError('Sampling grid has a non-finite or degenerate transform');
108+
}
109+
// The radius has a fixed upper limit, so the work for each module stays
110+
// within a fixed budget for any caller geometry.
111+
const rx = Math.min(MAX_SAMPLE_RADIUS, Math.max(1, Math.round(stepX / 4)));
112+
const ry = Math.min(MAX_SAMPLE_RADIUS, Math.max(1, Math.round(stepY / 4)));
106113
for (let y = 0; y < height; y++) {
107114
for (let x = 0; x < width; x++) {
108115
const c = transform.transformPoint(x + 0.5, y + 0.5);
109-
const cx = c.x | 0;
110-
const cy = c.y | 0;
111-
if (cx < 0 || cy < 0 || cx >= image.width || cy >= image.height) {
116+
// Check the real values before the integer conversion: `|0` wraps large
117+
// values and turns NaN into 0.
118+
if (!(c.x >= 0 && c.y >= 0 && c.x < image.width && c.y < image.height)) {
112119
throw new NotFoundError(`Sampling grid escapes the image at module (${x}, ${y})`);
113120
}
121+
const cx = c.x | 0;
122+
const cy = c.y | 0;
114123
let sumR = 0, sumG = 0, sumB = 0, count = 0;
115124
for (let dy = -ry; dy <= ry; dy++) {
116125
for (let dx = -rx; dx <= rx; dx++) {

‎src/js/jabcode/index.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ export function decodeJABCodeMatrix(matrix) {
155155
*/
156156
export function decodeJABCode(image, corners, version) {
157157
const size = 4 * version + 17;
158+
for (const corner of [corners.topLeft, corners.topRight, corners.bottomRight, corners.bottomLeft]) {
159+
if (!Number.isFinite(corner.x) || !Number.isFinite(corner.y)) {
160+
throw new NotFoundError('jabcode: corner coordinates must be finite numbers');
161+
}
162+
}
158163
const transform = PerspectiveTransform.quadToQuad(0, 0, size, 0, size, size, 0, size, corners.topLeft.x, corners.topLeft.y, corners.topRight.x, corners.topRight.y, corners.bottomRight.x, corners.bottomRight.y, corners.bottomLeft.x, corners.bottomLeft.y);
159164
const classified = classifyGrid(image, size, size, transform, JABCODE_PALETTE);
160165
return decodeJABCodeMatrix(classified);

‎src/ts/color/classify.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ function nearestPaletteIndex(sample, palette) {
8686
return best;
8787
}
8888

89+
/** Upper limit of the sample window radius, in image pixels. */
90+
const MAX_SAMPLE_RADIUS = 16;
91+
8992
/**
9093
* Sample and classify a `width` x `height` grid.
9194
*
@@ -107,18 +110,25 @@ export function classifyGrid(image, width, height, transform, palette) {
107110
const p2 = transform.transformPoint(0.5, 1.5);
108111
const stepX = Math.hypot(p1.x - p0.x, p1.y - p0.y);
109112
const stepY = Math.hypot(p2.x - p0.x, p2.y - p0.y);
110-
const rx = Math.max(1, Math.round(stepX / 4));
111-
const ry = Math.max(1, Math.round(stepY / 4));
113+
if (!Number.isFinite(stepX) || !Number.isFinite(stepY)) {
114+
throw new NotFoundError('Sampling grid has a non-finite or degenerate transform');
115+
}
116+
// The radius has a fixed upper limit, so the work for each module stays
117+
// within a fixed budget for any caller geometry.
118+
const rx = Math.min(MAX_SAMPLE_RADIUS, Math.max(1, Math.round(stepX / 4)));
119+
const ry = Math.min(MAX_SAMPLE_RADIUS, Math.max(1, Math.round(stepY / 4)));
112120

113121
for (let y = 0; y < height; y++) {
114122
for (let x = 0; x < width; x++) {
115123
const c = transform.transformPoint(x + 0.5, y + 0.5);
116-
const cx = c.x | 0;
117-
const cy = c.y | 0;
118-
if (cx < 0 || cy < 0 || cx >= image.width || cy >= image.height) {
124+
// Check the real values before the integer conversion: `|0` wraps large
125+
// values and turns NaN into 0.
126+
if (!(c.x >= 0 && c.y >= 0 && c.x < image.width && c.y < image.height)) {
119127
throw new NotFoundError(`Sampling grid escapes the image at module (${x}, ${y})`);
120128
}
121129

130+
const cx = c.x | 0;
131+
const cy = c.y | 0;
122132
let sumR = 0, sumG = 0, sumB = 0, count = 0;
123133
for (let dy = -ry; dy <= ry; dy++) {
124134
for (let dx = -rx; dx <= rx; dx++) {

‎src/ts/jabcode/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ export function decodeJABCode(
174174
version: number,
175175
): Uint8Array {
176176
const size = 4 * version + 17;
177+
for (const corner of [corners.topLeft, corners.topRight, corners.bottomRight, corners.bottomLeft]) {
178+
if (!Number.isFinite(corner.x) || !Number.isFinite(corner.y)) {
179+
throw new NotFoundError('jabcode: corner coordinates must be finite numbers');
180+
}
181+
}
177182
const transform = PerspectiveTransform.quadToQuad(
178183
0, 0, size, 0, size, size, 0, size,
179184
corners.topLeft.x, corners.topLeft.y,

0 commit comments

Comments
 (0)