From 7efeb06bc588ad250e5a6f26d8c907d6f6c4955e Mon Sep 17 00:00:00 2001 From: "Tomwang.eth" Date: Tue, 1 Dec 2026 02:24:46 +0000 Subject: [PATCH] fix(node): reject non-integer values for integer-only numeric options (#4916) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem Several Node APIs accept integer-only options but pass JavaScript numbers straight to integer N-API parameters. Fractional values are silently truncated, so `checkout(1.5)` checks out version 1, `tags.create("v1", 1.5)` pins version 1, `limit(2.5)` returns two rows, and bucket `numBuckets: 2.5` enables two buckets instead of rejecting the input. Negative or out-of-range values also reach Rust after lossy conversion. ## Change - The native `Tags` export is kept: code registering `Tags` as a `new () => Tags` constructor still compiles. Validation for tag versions happens in `Tags.prototype.create`/`update` wrappers, and #5145's `tags.create(name)` and `tags.create(name, null)` behavior (tag the handle's visible version) is preserved and tested. - `checkout(version)` validates numeric versions as non-negative integers before conversion. - `numBuckets` is checked only for the `bucket` write spec and must be an integer in 1–1024 before native conversion; `identity` and `unsharded` specs ignore it. - `limit` on plain and vector queries is checked against the native `u32` range (0–4294967295) before conversion, covering the upper boundary; `limit(0)` still returns no rows from plain queries. - `nprobes`, `minimumNprobes`, `maximumNprobes`, `ef` and `refineFactor` reject non-integers via the existing `validateNonNegativeInteger`. Fractional `searchEffort` stays valid and is covered by a positive regression assertion. Branch APIs are outside this patch. This does not change the separate `TagContents` serialization fix in #4915, and is based directly on main rather than stacked on it. This is not a complete audit of numeric options. ## Validation - New regression tests: fractional checkout version; non-integer tag create/update versions plus `create(name)`/`create(name, null)` preserved; non-integer query options with `searchEffort` positive case; `limit` u32 upper boundary accepted, one-past rejected, `limit(0)` returning no rows; `numBuckets` fractional/0/1025 rejected for `bucket` and ignored by `identity`/`unsharded`. - All pass locally on Linux x86_64 against a native build from main; `tsc -b` and Biome checks pass. TypeScript-only change; Rust and Python sources unchanged. --------- Co-authored-by: Xuanwo --- docs/src/js/README.md | 2 +- nodejs/__test__/table.test.ts | 135 ++++++++++++++++++++++++++++ nodejs/lancedb/materialized_view.ts | 17 ++++ nodejs/lancedb/query.ts | 12 +++ nodejs/lancedb/table.ts | 44 +++++++++ 5 files changed, 209 insertions(+), 1 deletion(-) diff --git a/docs/src/js/README.md b/docs/src/js/README.md index 286a0de17..599299b45 100644 --- a/docs/src/js/README.md +++ b/docs/src/js/README.md @@ -16,7 +16,7 @@ This will download the appropriate native library for your platform. We currentl support: - Linux (x86_64 and aarch64 on glibc and musl) -- MacOS (Intel and ARM/M1/M2) +- macOS (Apple silicon / arm64) - Windows (x86_64 and aarch64) ## Usage diff --git a/nodejs/__test__/table.test.ts b/nodejs/__test__/table.test.ts index 74ebdd853..a77c6b44e 100644 --- a/nodejs/__test__/table.test.ts +++ b/nodejs/__test__/table.test.ts @@ -2899,6 +2899,17 @@ describe("when dealing with versioning", () => { "checkout before running restore", ); }); + + it("rejects a fractional checkout version", async () => { + const con = await connect(tmpDir.name); + const table = await con.createTable("vectors", [ + { id: 1n, vector: [0.1, 0.2] }, + ]); + // A fractional version would be silently truncated at the N-API boundary. + await expect(table.checkout(1.5)).rejects.toThrow( + "version must be a non-negative integer", + ); + }); }); function legacyBlob(name: string): Field { @@ -4243,6 +4254,103 @@ describe("when dealing with tags", () => { expect(serialized["tag1"].version).toBe(1); expect(serialized["tag1"].manifestSize).toBeGreaterThan(0); }); + + it("rejects non-integer tag versions", async () => { + const conn = await connect(tmpDir.name, { + readConsistencyInterval: 0, + }); + const table = await conn.createTable("my_table", [ + { id: 1n, vector: [0.1, 0.2] }, + ]); + const tagsManager = await table.tags(); + // Fractional versions would be silently truncated at the N-API boundary + // and negative ones would wrap; they must be rejected instead. + await expect(tagsManager.create("bad", 1.5)).rejects.toThrow( + "version must be a non-negative integer", + ); + await expect(tagsManager.create("bad", -1)).rejects.toThrow( + "version must be a non-negative integer", + ); + await expect(tagsManager.update("bad", 2.9)).rejects.toThrow( + "version must be a non-negative integer", + ); + }); + + it("keeps create without a version tagging the visible version", async () => { + const conn = await connect(tmpDir.name, { + readConsistencyInterval: 0, + }); + const table = await conn.createTable("my_table", [{ id: 1 }]); + const tagsManager = await table.tags(); + + await tagsManager.create("omitted"); + expect(await tagsManager.getVersion("omitted")).toBe(1); + await tagsManager.create("nullable", null); + expect(await tagsManager.getVersion("nullable")).toBe(1); + }); +}); + +describe("query option validation", () => { + let tmpDir: tmp.DirResult; + beforeEach(() => { + tmpDir = tmp.dirSync({ unsafeCleanup: true }); + }); + afterEach(() => { + tmpDir.removeCallback(); + }); + + it("rejects non-integer query options", async () => { + const conn = await connect(tmpDir.name); + const table = await conn.createTable("my_table", [ + { id: 1n, vector: [0.1, 0.2] }, + ]); + // Integer-contract options would be silently truncated at the N-API + // boundary; they must be rejected instead. + expect(() => table.query().limit(2.5)).toThrow( + "limit must be an integer between 0 and 4294967295", + ); + expect(() => table.vectorSearch([0.1, 0.2]).nprobes(1.5)).toThrow( + "nprobes must be a non-negative integer", + ); + expect(() => table.vectorSearch([0.1, 0.2]).minimumNprobes(0.5)).toThrow( + "minimumNprobes must be a non-negative integer", + ); + expect(() => table.vectorSearch([0.1, 0.2]).maximumNprobes(2.5)).toThrow( + "maximumNprobes must be a non-negative integer", + ); + expect(() => table.vectorSearch([0.1, 0.2]).ef(3.5)).toThrow( + "ef must be a non-negative integer", + ); + expect(() => table.vectorSearch([0.1, 0.2]).refineFactor(1.5)).toThrow( + "refineFactor must be a non-negative integer", + ); + // searchEffort is legitimately fractional and must keep working. + expect(() => + table.vectorSearch([0.1, 0.2]).searchEffort(0.75), + ).not.toThrow(); + }); + + it("checks limit against the native u32 range and preserves limit(0)", async () => { + const conn = await connect(tmpDir.name); + const table = await conn.createTable("my_table", [ + { id: 1n, vector: [0.1, 0.2] }, + { id: 2n, vector: [0.3, 0.4] }, + ]); + // The upper u32 boundary is accepted for plain and vector queries... + expect(() => table.query().limit(4294967295)).not.toThrow(); + expect(() => + table.vectorSearch([0.1, 0.2]).limit(4294967295), + ).not.toThrow(); + // ...and one past it is rejected before native conversion. + expect(() => table.query().limit(4294967296)).toThrow( + "limit must be an integer between 0 and 4294967295", + ); + expect(() => table.vectorSearch([0.1, 0.2]).limit(4294967296)).toThrow( + "limit must be an integer between 0 and 4294967295", + ); + // limit(0) still returns no rows from a plain query. + expect(await table.query().limit(0).toArray()).toHaveLength(0); + }); }); describe("when dealing with branches", () => { @@ -6299,6 +6407,33 @@ describe("enableRTE / disableRTE", () => { ).rejects.toThrow(); }); + it("validates numBuckets only for the bucket write spec", async () => { + const conn = await connect(tmpDir.name); + const table = await makeTable(conn); + await table.setUnenforcedPrimaryKey("id"); + + // A fractional count would be silently truncated at the N-API boundary; + // the native range is 1-1024. + await expect( + table.enableRTE({ specType: "bucket", column: "id", numBuckets: 2.5 }), + ).rejects.toThrow("numBuckets must be an integer between 1 and 1024"); + await expect( + table.enableRTE({ specType: "bucket", column: "id", numBuckets: 1025 }), + ).rejects.toThrow("numBuckets must be an integer between 1 and 1024"); + await expect( + table.enableRTE({ specType: "bucket", column: "id", numBuckets: 0 }), + ).rejects.toThrow("numBuckets must be an integer between 1 and 1024"); + + // Identity and unsharded specs ignore numBuckets entirely. + await table.enableRTE({ + specType: "identity", + column: "id", + numBuckets: 2.5, + }); + await table.disableRTE(); + await table.enableRTE({ specType: "unsharded", numBuckets: 2.5 }); + }); + it("reads back the installed spec via getRTESpec", async () => { const conn = await connect(tmpDir.name); const table = await makeTable(conn); diff --git a/nodejs/lancedb/materialized_view.ts b/nodejs/lancedb/materialized_view.ts index d6c101b67..43e9916c6 100644 --- a/nodejs/lancedb/materialized_view.ts +++ b/nodejs/lancedb/materialized_view.ts @@ -45,6 +45,23 @@ export function validateNonNegativeInteger( } } +/** + * Validates a parameter that crosses N-API as a Rust `u32`: it must be an + * integer inside the representable range, otherwise the conversion would + * silently truncate or wrap it. + */ +export function validateU32Integer( + value: number | undefined, + name: string, +): void { + if ( + value !== undefined && + !(Number.isSafeInteger(value) && value >= 0 && value <= 4294967295) + ) { + throw new Error(`${name} must be an integer between 0 and 4294967295`); + } +} + /** @internal Quote a column name as a Lance SQL identifier (backticks). */ function quoteIdentifier(name: string): string { return "`" + name.replace(/`/g, "``") + "`"; diff --git a/nodejs/lancedb/query.ts b/nodejs/lancedb/query.ts index 51d4ae2d8..45e0c3085 100644 --- a/nodejs/lancedb/query.ts +++ b/nodejs/lancedb/query.ts @@ -20,6 +20,10 @@ import { import { isBlobDescriptorField, isBlobField } from "./blob"; import { Expr } from "./expr"; import { type IvfPqOptions } from "./indices"; +import { + validateNonNegativeInteger, + validateU32Integer, +} from "./materialized_view"; import { JsFullTextQuery, RecordBatchIterator as NativeBatchIterator, @@ -819,6 +823,9 @@ export class StandardQueryBase< * called then every valid row from the table will be returned. */ limit(limit: number): this { + // The native side takes a u32; anything fractional or out of range would + // be silently truncated or wrapped by the N-API conversion. + validateU32Integer(limit, "limit"); this.doCall((inner: NativeQueryType) => inner.limit(limit)); return this; } @@ -946,6 +953,7 @@ export class VectorQuery extends StandardQueryBase { * or maximum settings can override their respective bounds. */ nprobes(nprobes: number): VectorQuery { + validateNonNegativeInteger(nprobes, "nprobes"); this.doVectorCall((inner) => inner.nprobes(nprobes)); return this; @@ -986,6 +994,7 @@ export class VectorQuery extends StandardQueryBase { * but will also increase latency. */ minimumNprobes(minimumNprobes: number): VectorQuery { + validateNonNegativeInteger(minimumNprobes, "minimumNprobes"); this.doVectorCall((inner) => inner.minimumNprobes(minimumNprobes)); return this; } @@ -1000,6 +1009,7 @@ export class VectorQuery extends StandardQueryBase { * potential false negatives. */ maximumNprobes(maximumNprobes: number): VectorQuery { + validateNonNegativeInteger(maximumNprobes, "maximumNprobes"); this.doVectorCall((inner) => inner.maximumNprobes(maximumNprobes)); return this; } @@ -1027,6 +1037,7 @@ export class VectorQuery extends StandardQueryBase { * also increase the latency of your query. The default value is 1.5*limit. */ ef(ef: number): VectorQuery { + validateNonNegativeInteger(ef, "ef"); this.doVectorCall((inner) => inner.ef(ef)); return this; } @@ -1117,6 +1128,7 @@ export class VectorQuery extends StandardQueryBase { * distance between the query vector and the actual uncompressed vector. */ refineFactor(refineFactor: number): VectorQuery { + validateNonNegativeInteger(refineFactor, "refineFactor"); this.doVectorCall((inner) => inner.refineFactor(refineFactor)); return this; } diff --git a/nodejs/lancedb/table.ts b/nodejs/lancedb/table.ts index 7b6fe4efc..a898aab3b 100644 --- a/nodejs/lancedb/table.ts +++ b/nodejs/lancedb/table.ts @@ -130,6 +130,44 @@ Tags.prototype.list = async function ( } return tags; }; + +// Validate tag versions before N-API can silently truncate or wrap them. +const nativeTagsCreate = Tags.prototype.create; +Tags.prototype.create = async function ( + this: Tags, + tag: string, + version?: number | null, +): Promise { + // An omitted or null version tags the version this handle currently + // shows; only a provided version needs validation. + if (version !== undefined && version !== null) { + validateNonNegativeInteger(version, "version"); + } + return nativeTagsCreate.call(this, tag, version); +}; + +const nativeTagsUpdate = Tags.prototype.update; +Tags.prototype.update = async function ( + this: Tags, + tag: string, + version: number, +): Promise { + validateNonNegativeInteger(version, "version"); + return nativeTagsUpdate.call(this, tag, version); +}; + +/** + * Validates `numBuckets` for a bucket RTE write spec before it crosses + * N-API: the native side requires an integer in [1, 1024]. + */ +function validateNumBuckets(numBuckets: number | undefined): void { + if ( + numBuckets !== undefined && + !(Number.isSafeInteger(numBuckets) && numBuckets >= 1 && numBuckets <= 1024) + ) { + throw new Error("numBuckets must be an integer between 1 and 1024"); + } +} import { AutoQuery, FullTextQuery, @@ -2348,6 +2386,11 @@ export class LocalTable extends Table { } async enableRTE(spec: RTEWriteSpec): Promise { + // Only the bucket write spec uses numBuckets; identity and unsharded + // specs ignore it. + if (spec.specType === "bucket") { + validateNumBuckets(spec.numBuckets); + } return await this.inner.enableRTE(spec); } @@ -2408,6 +2451,7 @@ export class LocalTable extends Table { if (typeof version === "string") { return this.inner.checkoutTag(version); } + validateNonNegativeInteger(version, "version"); return this.inner.checkout(version); }