Skip to content

Commit ac7a079

Browse files
[js] hold BiDi numbers to their declared ranges (#18272)
Validate numeric min, max, exclusiveMin, and exclusiveMax bounds in validateValue for primitive number/integer fields and resolved aliases. Part of #18195 Co-authored-by: Diego Molina <[email protected]>
1 parent dac3dd1 commit ac7a079

2 files changed

Lines changed: 136 additions & 1 deletion

File tree

‎javascript/selenium-webdriver/bidi/serialization/record.js‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ const { register, resolve } = require('./registry')
1919

2020
class ValidationError extends Error {}
2121

22+
function checkBounds(typeNode, value, path) {
23+
if (typeof value !== 'number') return
24+
if (typeNode.min !== undefined) {
25+
const ok = typeNode.exclusiveMin ? value > typeNode.min : value >= typeNode.min
26+
if (!ok) {
27+
const op = typeNode.exclusiveMin ? '>' : '>='
28+
throw new ValidationError(`${path}: expected ${op} ${typeNode.min}, got ${value}`)
29+
}
30+
}
31+
if (typeNode.max !== undefined) {
32+
const ok = typeNode.exclusiveMax ? value < typeNode.max : value <= typeNode.max
33+
if (!ok) {
34+
const op = typeNode.exclusiveMax ? '<' : '<='
35+
throw new ValidationError(`${path}: expected ${op} ${typeNode.max}, got ${value}`)
36+
}
37+
}
38+
}
39+
2240
// Validates a *present* value against a resolved type node, and returns the value to
2341
// assign for it — usually the same value, but for a nested ref-to-record/union (the
2442
// freshly constructed/parsed instance — fromWire()/build() inbound, `new RecordClass(value)`/
@@ -60,6 +78,7 @@ function validateValue(typeNode, value, path, direction) {
6078
if (typeNode.primitive === 'integer' && !Number.isInteger(value)) {
6179
throw new ValidationError(`${path}: expected an integer, got ${value}`)
6280
}
81+
checkBounds(typeNode, value, path)
6382
// An inline literal choice (project_bidi_schema.mjs's enumNode()) carries both
6483
// `primitive` and `enum` — the primitive check above narrows the type, but the
6584
// closed vocabulary below still needs to run, so only return early when there
@@ -146,7 +165,9 @@ function validateValue(typeNode, value, path, direction) {
146165
}
147166

148167
if (referenced.kind === 'alias') {
149-
return validateValue(referenced.type, value, path, direction)
168+
const result = validateValue(referenced.type, value, path, direction)
169+
checkBounds(typeNode, result, path)
170+
return result
150171
}
151172

152173
return value

‎javascript/selenium-webdriver/test/bidi/serialization/record_test.js‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,4 +493,118 @@ describe('serialization/record', function () {
493493
assert.strictEqual(parsed.choice, false)
494494
})
495495
})
496+
497+
describe('numeric range and bounds validation', function () {
498+
defineAlias('test.record.JsUint', {
499+
primitive: 'integer',
500+
min: 0,
501+
max: 9007199254740991,
502+
})
503+
504+
const LocateNodesParameters = defineRecord('test.record.LocateNodesParameters', [
505+
{ name: 'maxNodeCount', wire: 'maxNodeCount', required: false, type: { ref: 'test.record.JsUint', min: 1 } },
506+
])
507+
508+
const ResponseData = defineRecord('test.record.ResponseData', [
509+
{ name: 'status', wire: 'status', required: true, type: { ref: 'test.record.JsUint' } },
510+
])
511+
512+
const ViewportParameters = defineRecord('test.record.ViewportParameters', [
513+
{
514+
name: 'devicePixelRatio',
515+
wire: 'devicePixelRatio',
516+
required: false,
517+
type: { primitive: 'number', min: 0, exclusiveMin: true, nullable: true },
518+
},
519+
])
520+
521+
const GeolocationCoordinates = defineRecord('test.record.GeolocationCoordinates', [
522+
{
523+
name: 'heading',
524+
wire: 'heading',
525+
required: false,
526+
type: { primitive: 'number', min: 0, max: 360, exclusiveMax: true, nullable: true },
527+
},
528+
])
529+
530+
const PrintParameters = defineRecord('test.record.PrintParameters', [
531+
{
532+
name: 'pageRanges',
533+
wire: 'pageRanges',
534+
required: false,
535+
type: { list: { union: [{ ref: 'test.record.JsUint' }, { primitive: 'string' }] } },
536+
},
537+
])
538+
539+
it('holds an alias-typed field to its declared range outbound', function () {
540+
const resp = new ResponseData({ status: 200 })
541+
assert.strictEqual(resp.status, 200)
542+
543+
assert.throws(() => new ResponseData({ status: -1 }), /expected >= 0, got -1/)
544+
assert.throws(() => new ResponseData({ status: 9007199254740992 }), /expected <= 9007199254740991/)
545+
})
546+
547+
it('rejects a value outside the range of the alias it names on receipt', function () {
548+
const parsed = ResponseData.fromWire({ status: 200 })
549+
assert.strictEqual(parsed.status, 200)
550+
551+
assert.throws(() => ResponseData.fromWire({ status: -1 }), /expected >= 0, got -1/)
552+
assert.throws(() => ResponseData.fromWire({ status: 9007199254740992 }), /expected <= 9007199254740991/)
553+
})
554+
555+
it('holds a field narrowing an alias to its tightened range (e.g. maxNodeCount 0 vs 1)', function () {
556+
const valid = new LocateNodesParameters({ maxNodeCount: 1 })
557+
assert.strictEqual(valid.maxNodeCount, 1)
558+
559+
const parsed = LocateNodesParameters.fromWire({ maxNodeCount: 1 })
560+
assert.strictEqual(parsed.maxNodeCount, 1)
561+
562+
assert.throws(() => new LocateNodesParameters({ maxNodeCount: 0 }), /expected >= 1, got 0/)
563+
assert.throws(() => LocateNodesParameters.fromWire({ maxNodeCount: 0 }), /expected >= 1, got 0/)
564+
565+
assert.throws(() => new LocateNodesParameters({ maxNodeCount: -1 }), /expected >= 0, got -1/)
566+
assert.throws(() => LocateNodesParameters.fromWire({ maxNodeCount: -1 }), /expected >= 0, got -1/)
567+
})
568+
569+
it('enforces exclusive min bound on floating point numbers (e.g. devicePixelRatio > 0)', function () {
570+
const valid = new ViewportParameters({ devicePixelRatio: 0.5 })
571+
assert.strictEqual(valid.devicePixelRatio, 0.5)
572+
573+
const parsed = ViewportParameters.fromWire({ devicePixelRatio: 1 })
574+
assert.strictEqual(parsed.devicePixelRatio, 1)
575+
576+
const nullVal = new ViewportParameters({ devicePixelRatio: null })
577+
assert.strictEqual(nullVal.devicePixelRatio, null)
578+
579+
assert.throws(() => new ViewportParameters({ devicePixelRatio: 0 }), /expected > 0, got 0/)
580+
assert.throws(() => ViewportParameters.fromWire({ devicePixelRatio: 0 }), /expected > 0, got 0/)
581+
assert.throws(() => new ViewportParameters({ devicePixelRatio: -0.1 }), /expected > 0, got -0.1/)
582+
})
583+
584+
it('enforces exclusive max bound on floating point numbers (e.g. heading < 360)', function () {
585+
const valid = new GeolocationCoordinates({ heading: 359.9 })
586+
assert.strictEqual(valid.heading, 359.9)
587+
588+
const zero = new GeolocationCoordinates({ heading: 0 })
589+
assert.strictEqual(zero.heading, 0)
590+
591+
const parsed = GeolocationCoordinates.fromWire({ heading: 180 })
592+
assert.strictEqual(parsed.heading, 180)
593+
594+
assert.throws(() => new GeolocationCoordinates({ heading: 360 }), /expected < 360, got 360/)
595+
assert.throws(() => GeolocationCoordinates.fromWire({ heading: 360 }), /expected < 360, got 360/)
596+
assert.throws(() => new GeolocationCoordinates({ heading: -1 }), /expected >= 0, got -1/)
597+
})
598+
599+
it('holds union list elements to their declared numeric ranges (e.g. pageRanges)', function () {
600+
const valid = new PrintParameters({ pageRanges: ['1-3', 2] })
601+
assert.deepStrictEqual(valid.pageRanges, ['1-3', 2])
602+
603+
const parsed = PrintParameters.fromWire({ pageRanges: ['1-3', 2] })
604+
assert.deepStrictEqual(parsed.pageRanges, ['1-3', 2])
605+
606+
assert.throws(() => new PrintParameters({ pageRanges: ['1-3', -1] }), ValidationError)
607+
assert.throws(() => PrintParameters.fromWire({ pageRanges: ['1-3', -1] }), ValidationError)
608+
})
609+
})
496610
})

0 commit comments

Comments
 (0)