fix: reject partial browse client env integers

This commit is contained in:
陈家名
2026-05-06 19:07:32 +08:00
parent 19e699ab9b
commit e6172f8b7e
3 changed files with 39 additions and 6 deletions
+10 -3
View File
@@ -54,6 +54,13 @@ interface ResolvedAuth {
source: 'env' | 'state-file';
}
function parseIntegerEnvValue(value: string | undefined): number | undefined {
const trimmed = value?.trim();
if (!trimmed || !/^-?\d+$/.test(trimmed)) return undefined;
const parsed = parseInt(trimmed, 10);
return Number.isFinite(parsed) ? parsed : undefined;
}
/** Resolve the daemon port + token. Throws a clear error if neither path works. */
export function resolveBrowseAuth(opts: BrowseClientOptions = {}): ResolvedAuth {
if (opts.port !== undefined && opts.token !== undefined) {
@@ -64,8 +71,8 @@ export function resolveBrowseAuth(opts: BrowseClientOptions = {}): ResolvedAuth
const envPort = process.env.GSTACK_PORT;
const envToken = process.env.GSTACK_SKILL_TOKEN;
if (envPort && envToken) {
const port = opts.port ?? parseInt(envPort, 10);
if (!isNaN(port)) {
const port = opts.port ?? parseIntegerEnvValue(envPort);
if (port !== undefined) {
return { port, token: opts.token ?? envToken, source: 'env' };
}
}
@@ -132,7 +139,7 @@ export class BrowseClient {
const auth = resolveBrowseAuth(opts);
this.port = auth.port;
this.token = auth.token;
this.tabId = opts.tabId ?? (process.env.BROWSE_TAB ? parseInt(process.env.BROWSE_TAB, 10) : undefined);
this.tabId = opts.tabId ?? parseIntegerEnvValue(process.env.BROWSE_TAB);
this.timeoutMs = opts.timeoutMs ?? 30_000;
}