fix(combos): drop nullish entries in filterActiveConnections

`connection?.isActive !== false` evaluated to true for null/undefined
entries, so nullish elements survived the filter. Callers read properties
off the result — filterUsableConnections() reads `connection.testStatus`
— which would throw "TypeError: Cannot read properties of null".

Guard with an explicit truthiness check. Covered by a test that fails
against the previous predicate.

Reported-by: gemini-code-assist
Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com>
This commit is contained in:
itolstov
2026-07-15 06:48:22 -03:00
committed by Diego Rodrigues de Sa e Souza
parent 291045c157
commit cbf73937c0
2 changed files with 15 additions and 2 deletions

View File

@@ -16,13 +16,14 @@ export interface ConnectionActiveFlag {
/**
* Filters out connections that have been explicitly disabled
* (`isActive === false`). Connections without an `isActive` field are
* treated as active for backward compatibility.
* treated as active for backward compatibility. Nullish entries are
* dropped so callers can safely read properties off the result.
*/
export function filterActiveConnections<T extends ConnectionActiveFlag>(
connections: T[] | null | undefined
): T[] {
if (!Array.isArray(connections)) return [];
return connections.filter((connection) => connection?.isActive !== false);
return connections.filter((connection) => !!connection && connection.isActive !== false);
}
/**

View File

@@ -25,6 +25,18 @@ test("filterActiveConnections returns an empty list for invalid input", () => {
assert.deepEqual(filterActiveConnections(null), []);
});
test("filterActiveConnections drops nullish entries instead of passing them through", () => {
// A nullish element must not survive: callers read properties off the
// result (e.g. `connection.testStatus`) and would throw a TypeError.
const active = { id: "active", isActive: true };
assert.deepEqual(filterActiveConnections([null, active, undefined]), [active]);
assert.doesNotThrow(() => filterUsableConnections([null, undefined]));
assert.deepEqual(filterUsableConnections([null, { id: "ok", testStatus: "active" }]), [
{ id: "ok", testStatus: "active" },
]);
});
test("filterUsableConnections applies the isActive gate before the testStatus gate", () => {
// Regression for the exact bug: a disabled connection with a stale
// "active" testStatus must NOT survive the combined filter that