Skip to content

Commit 860b67f

Browse files
authored
Merge pull request #107 from Sythos/enya/issue-98-p2-validate-jabcode-version-before-matri
[P2] Validate JABCode version before matrix allocation
2 parents 3afe1b4 + 5f6fe22 commit 860b67f

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎docs/formats/jabcode.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ Both return the decoded payload as a `Uint8Array`.
8484

8585
`decodeJABCode` throws `NotFoundError` when `version` is not an integer from 1
8686
through 32. The check runs before the decoder calculates the grid size or
87-
allocates a matrix. `decodeJABCodeMatrix` applies the same check to the version
87+
allocates a matrix. The decoder then checks that the derived grid size is from
88+
21 through 145 modules. `decodeJABCodeMatrix` applies the same check to the version
8889
derived from the matrix side. `PolychromeMatrix` also rejects non-integer
8990
dimensions, more than 4096 modules on one side, and more than 4096 x 4096
9091
cells. These limits stop untrusted input from causing a large allocation.

‎docs/guides/security.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ The current hard limits are:
4646
- positive safe-integer source image dimensions and four channel values per
4747
pixel.
4848

49+
The colour matrix path has its own limits. `decodeJABCode` accepts only an
50+
integer version from 1 through 32. It checks the version and the derived grid
51+
size before it allocates a matrix. `PolychromeMatrix` accepts only integer
52+
dimensions, no more than 4,096 modules on one side and no more than 16,777,216
53+
cells.
54+
4955
The total-pixel ceiling is still a high limit: it is roughly twice the area of
5056
an ordinary 4K image. It is a safety ceiling, not a recommendation to accept
5157
every frame at that size. An application processing remote files or a live

‎src/js/jabcode/index.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ function assertVersion(version) {
8282
throw new NotFoundError(`jabcode: version must be an integer from 1 to ${MAX_VERSION}, got ${version}`);
8383
}
8484
}
85+
/** Returns the side length in modules for a version. Checks the version and the derived size first. */
86+
function gridSizeForVersion(version) {
87+
assertVersion(version);
88+
const size = 4 * version + 17;
89+
if (!Number.isSafeInteger(size) || size < 21 || size > 4 * MAX_VERSION + 17) {
90+
throw new NotFoundError(`jabcode: grid size ${size} is outside the valid range for version ${version}`);
91+
}
92+
return size;
93+
}
8594
function chooseVersion(requiredBits) {
8695
for (let version = 1; version <= MAX_VERSION; version++) {
8796
const gross = masterSymbolCapacity(version);
@@ -161,8 +170,7 @@ export function decodeJABCodeMatrix(matrix) {
161170
* @returns {Uint8Array}
162171
*/
163172
export function decodeJABCode(image, corners, version) {
164-
assertVersion(version);
165-
const size = 4 * version + 17;
173+
const size = gridSizeForVersion(version);
166174
for (const corner of [corners.topLeft, corners.topRight, corners.bottomRight, corners.bottomLeft]) {
167175
if (!Number.isFinite(corner.x) || !Number.isFinite(corner.y)) {
168176
throw new NotFoundError('jabcode: corner coordinates must be finite numbers');

‎src/ts/jabcode/index.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ function assertVersion(version: number): void {
9090
}
9191
}
9292

93+
/** Returns the side length in modules for a version. Checks the version and the derived size first. */
94+
function gridSizeForVersion(version: number): number {
95+
assertVersion(version);
96+
const size = 4 * version + 17;
97+
if (!Number.isSafeInteger(size) || size < 21 || size > 4 * MAX_VERSION + 17) {
98+
throw new NotFoundError(`jabcode: grid size ${size} is outside the valid range for version ${version}`);
99+
}
100+
return size;
101+
}
102+
93103
function chooseVersion(requiredBits: number): number {
94104
for (let version = 1; version <= MAX_VERSION; version++) {
95105
const gross = masterSymbolCapacity(version);
@@ -181,8 +191,7 @@ export function decodeJABCode(
181191
corners: { topLeft: { x: number; y: number }; topRight: { x: number; y: number }; bottomRight: { x: number; y: number }; bottomLeft: { x: number; y: number } },
182192
version: number,
183193
): Uint8Array {
184-
assertVersion(version);
185-
const size = 4 * version + 17;
194+
const size = gridSizeForVersion(version);
186195
for (const corner of [corners.topLeft, corners.topRight, corners.bottomRight, corners.bottomLeft]) {
187196
if (!Number.isFinite(corner.x) || !Number.isFinite(corner.y)) {
188197
throw new NotFoundError('jabcode: corner coordinates must be finite numbers');

0 commit comments

Comments
 (0)