Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
0bbcee4
fix(serve): use authority-scoped credential stripping in provider war…
C0d3N1nja97342 Aug 3, 2026
5722231
fix(serve): bound provider warning authority by whitespace
C0d3N1nja97342 Aug 3, 2026
456a597
Merge branch 'main' into fix/provider-warning-sanitizer
wenshao Aug 3, 2026
9622a32
fix(serve): redesign provider warning credential stripping heuristic
C0d3N1nja97342 Aug 4, 2026
e901f90
Merge remote-tracking branch 'upstream/main' into fix/provider-warnin…
C0d3N1nja97342 Aug 4, 2026
69b153a
fix(serve): bound userinfo strip by path shape and delegate warning s…
C0d3N1nja97342 Aug 4, 2026
03b1141
Merge branch 'main' into fix/provider-warning-sanitizer
wenshao Aug 5, 2026
81fe680
fix(cli): trust new URL() terminator, drop host-shaped-char heuristic
C0d3N1nja97342 Aug 6, 2026
b431df7
fix(cli): scan sanitizer strip point structurally, not via new URL()
C0d3N1nja97342 Aug 6, 2026
46b93f7
Merge branch 'main' into fix/provider-warning-sanitizer
wenshao Aug 8, 2026
4f77712
fix(cli): close round-5 sanitizer findings (underscore host, case-ins…
C0d3N1nja97342 Aug 10, 2026
26578a1
fix(cli): close round-6 sanitizer findings (IPv6, port-punctuation, w…
C0d3N1nja97342 Aug 10, 2026
bbccdc4
fix(cli): close round-7 sanitizer findings (multi-punctuation port, U…
C0d3N1nja97342 Aug 10, 2026
7758092
refactor(cli): collapse dead IPv6 colonBeforeAt branch + fix R1-2 tes…
C0d3N1nja97342 Aug 11, 2026
68d4871
refactor(cli): simplify windowsCred scan bound and pin real-domain pr…
C0d3N1nja97342 Aug 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions packages/cli/src/serve/workspace-providers-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,83 @@ describe('createWorkspaceProvidersStatusProvider', () => {
expect(result.initialized).toBe(false);
});

it('does not corrupt a pathless URL with a port followed by prose email (#8136)', async () => {
coreMock.throwModelsConfigError = true;
coreMock.modelsConfigErrorMessage =
'Cannot reach https://api.example:8443 - contact admin@example.com';
const provider = createWorkspaceProvidersStatusProvider({ env: {} });
await writeUserSettings({
security: { auth: { selectedType: 'openai' } },
modelProviders: {
openai: [{ id: 'model-a', name: 'Model A' }],
},
});

const result = await provider(workspace, true);

expect(result.errors?.[0]?.error).toBe(
'Cannot reach https://api.example:8443 - contact admin@example.com',
);
});

it('does not corrupt a pathless URL without a port followed by prose email (#8136)', async () => {
coreMock.throwModelsConfigError = true;
coreMock.modelsConfigErrorMessage =
'Cannot reach https://api.example - contact admin@example.com';
const provider = createWorkspaceProvidersStatusProvider({ env: {} });
await writeUserSettings({
security: { auth: { selectedType: 'openai' } },
modelProviders: {
openai: [{ id: 'model-a', name: 'Model A' }],
},
});

const result = await provider(workspace, true);

expect(result.errors?.[0]?.error).toBe(
'Cannot reach https://api.example - contact admin@example.com',
);
});

it('strips credentials from a pathless URL and keeps host and prose (#8136)', async () => {
coreMock.throwModelsConfigError = true;
coreMock.modelsConfigErrorMessage =
'Failed https://user:pass@host.example:8443 - contact admin@example.com';
const provider = createWorkspaceProvidersStatusProvider({ env: {} });
await writeUserSettings({
security: { auth: { selectedType: 'openai' } },
modelProviders: {
openai: [{ id: 'model-a', name: 'Model A' }],
},
});

const result = await provider(workspace, true);

expect(result.errors?.[0]?.error).toBe(
'Failed https://host.example:8443 - contact admin@example.com',
);
expect(JSON.stringify(result)).not.toContain('user:pass@');
});

it('strips a password containing @ in full (#8136)', async () => {
coreMock.throwModelsConfigError = true;
coreMock.modelsConfigErrorMessage =
'Failed loading provider https://user:p@ssw0rd-tail@broken.example/v1';
const provider = createWorkspaceProvidersStatusProvider({ env: {} });
await writeUserSettings({
security: { auth: { selectedType: 'openai' } },
modelProviders: {
openai: [{ id: 'model-a', name: 'Model A' }],
},
});

const result = await provider(workspace, true);

expect(JSON.stringify(result)).toContain('https://broken.example/v1');
expect(JSON.stringify(result)).not.toContain('ssw0rd-tail');
expect(JSON.stringify(result)).not.toContain('p@ssw0rd');
});

async function writeUserSettings(settings: Record<string, unknown>) {
await fs.writeFile(
path.join(qwenHome, 'settings.json'),
Expand Down
36 changes: 11 additions & 25 deletions packages/cli/src/serve/workspace-providers-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ function sanitizeProviderWarning(warning: string): string {

const segmentEnd = findUrlSegmentEnd(warning, next.index, next.marker);
const segment = warning.slice(next.index, segmentEnd);
result += sanitizeProviderWarningSegment(segment, next.marker.length);
result += sanitizeProviderWarningSegment(segment);

index = segmentEnd;
next = findNextUrlStart(warning, index);
Expand Down Expand Up @@ -295,36 +295,22 @@ function findUrlSegmentEnd(
return Math.min(lineEnd, nextUrl?.index ?? value.length);
}

function sanitizeProviderWarningSegment(
segment: string,
markerLength: number,
): string {
const at = segment.indexOf('@', markerLength);
if (
at !== -1 &&
hasCredentialPrefix(segment, markerLength, at) &&
segment[at + 1] !== undefined &&
/[A-Za-z0-9.[\]-]/.test(segment[at + 1])
) {
return `${segment.slice(0, markerLength)}${segment.slice(at + 1)}`;
function sanitizeProviderWarningSegment(segment: string): string {
// When the whole segment is a URL that sanitizeProviderBaseUrl confirms
// carries real userinfo (it changes the segment), use its result directly.
// This handles space-containing-credential URLs that URL_LIKE_PATTERN cannot
// match past the whitespace, and the '@'-in-password shape (last '@' wins).
// The veto in sanitizeProviderBaseUrl leaves pathless-URL + prose-email
// shapes unchanged, so a prose email's '@' is never stripped. #8136.
const sanitized = sanitizeProviderBaseUrl(segment);
if (sanitized !== segment) {
return sanitized;
}

return segment.replace(URL_LIKE_PATTERN, (url) =>
sanitizeProviderBaseUrl(url),
);
}

function hasCredentialPrefix(
segment: string,
markerLength: number,
at: number,
): boolean {
const colon = segment.indexOf(':', markerLength);
if (colon === -1 || colon > at) return false;
const username = segment.slice(markerLength, colon);
return !/[/?#\s'"`<>]/.test(username);
}

function buildCurrent(
authType: AuthType | undefined,
modelId: string | undefined,
Expand Down
57 changes: 57 additions & 0 deletions packages/cli/src/utils/acpModelUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,63 @@ describe('acpModelUtils', () => {
['https://user:p?x@api.example/v1', 'https://api.example/v1'],
['https://user:p#x@api.example/v1', 'https://api.example/v1'],
['https://user:secret@api.example', 'https://api.example'],
// #8136: pathless URL + prose email shapes. WHATWG misparses these as
// userinfo; the veto (all-digit port before first whitespace) protects the
// with-port shape, and the pathless-prose guard protects the no-colon shape.
[
'https://api.example:8443 - contact admin@example.com',
'https://api.example:8443 - contact admin@example.com',
],
[
'https://api.example - contact admin@example.com',
'https://api.example - contact admin@example.com',
],
[
'https://ollama.local - contact admin@example.com',
'https://ollama.local - contact admin@example.com',
],
// Credentials + pathless + email: strip the credential, keep host + prose.
[
'https://user:pass@host.example:8443 - contact admin@example.com',
'https://host.example:8443 - contact admin@example.com',
],
// Space-in-password: the last '@' within the bounded authority is the real
// userinfo terminator (the password's '@' precedes it). #8136 R1-1/R1-3.
[
'https://user:sec ret@host.example/v1 - contact admin@example.com',
'https://host.example/v1 - contact admin@example.com',
],
['https://user:p@ss word@host.example/v1', 'https://host.example/v1'],
// KNOWN RESIDUAL: digit-prefix + space password is vetoed like a pathless
// port URL and leaks. Documented tradeoff (#8136 R1-2).
['https://user:1234 secret@host', 'https://user:1234 secret@host'],
// #8136 R3: passwords/hostnames the previous host-shaped-char heuristic
// mishandled. `new URL()` resolves the terminator authoritatively for these.
// Password containing '@' (pathless): strip at the LAST '@', not the first.
['https://user:p@ss@host', 'https://host'],
// Underscore-leading host (previously outside HOST_SHAPED_CHAR): strip.
['https://user:pass@_host', 'https://_host'],
// Tab immediately after '@' (WHATWG strips it as userinfo terminator): strip.
[`https://user:pass@\thost`, 'https://\thost'],
// Password containing '@' AND whitespace (bounded authority): the last '@'
// within the bounded authority is the terminator.
['https://user:p@ss word@host.example/v1', 'https://host.example/v1'],
// #8136 repro-1 (with-path port + prose email): must stay unchanged - the
// path bounds the authority, so the prose '@' is never the strip point.
[
'https://api.example:8443/v1 - contact admin@example.com',
'https://api.example:8443/v1 - contact admin@example.com',
],
// URL-throwing shapes (invalid %, space in host) + prose email: do not
// strip the prose '@'. #8136 R2-2.
[
'https://api.example%/v1, contact admin@example.com',
'https://api.example%/v1, contact admin@example.com',
],
[
'https://my service/v1 - contact admin@example.com',
'https://my service/v1 - contact admin@example.com',
],
])('sanitizes provider base URL credentials for %s', (input, expected) => {
expect(sanitizeProviderBaseUrl(input)).toBe(expected);
});
Expand Down
157 changes: 133 additions & 24 deletions packages/cli/src/utils/acpModelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,51 +171,160 @@ export function sanitizeProviderBaseUrl(baseUrl: string): string {
const stripAt = (at: number) =>
`${baseUrl.slice(0, authorityStart)}${baseUrl.slice(at + 1)}`;
const authorityEnd = findAuthorityEnd(baseUrl, authorityStart);
const authorityAt = baseUrl
.slice(authorityStart, authorityEnd)
.lastIndexOf('@');
const authorityAtIndex =
authorityAt === -1 ? -1 : authorityStart + authorityAt;

// The prose guards run first and may veto to -1 (no strip) when the input
// looks like a pathless host followed by prose rather than credentials. They
// protect misparsed-prose shapes (port+prose, host+prose) that `new URL()`
// would otherwise report as userinfo. A veto is not final, though: a pathless
// URL whose userinfo contains whitespace (e.g. `user name:pass@host`) also
// trips the prose guard but IS a real credential that `new URL()` confirms,
// so the parser gets the final say below. #8136.
const stripPoint = findUserInfoStripPoint(
baseUrl,
authorityStart,
authorityEnd,
);

try {
const parsed = new URL(baseUrl);
if (parsed.username || parsed.password) {
return authorityAtIndex >= authorityStart
? stripAt(authorityAtIndex)
: baseUrl;
const isPathless = authorityEnd === baseUrl.length;
if (!isPathless) {
// Bounded authority (has / ? #): `new URL()` parses the host
// authoritatively, so the LAST '@' within the authority is the real
// userinfo terminator - even when the password contains '@' or
// whitespace (e.g. `user:p@ss word@host.example/v1`). #8136.
const at = baseUrl.lastIndexOf('@', authorityEnd - 1);
return at >= authorityStart ? stripAt(at) : baseUrl;
}
// Pathless URL: WHATWG extends the authority to end-of-string, so a
// trailing prose email's '@' (e.g. `host - contact admin@example.com`)
// is also reported as userinfo and `lastIndexOf('@')` would pick it,
// deleting the real host. The userinfo terminator always precedes the
// first whitespace (the password ends at '@'); a prose email '@' does
// not. Scan only up to the first whitespace. A password containing
// whitespace beyond that point (e.g. `user name:pass@host`) is then
// indistinguishable from prose and is left as a documented residual.
const authority = baseUrl.slice(authorityStart, authorityEnd);
const firstWs = authority.search(/\s/);
const scanEnd = firstWs === -1 ? authorityEnd : authorityStart + firstWs;
const at = baseUrl.lastIndexOf('@', scanEnd - 1);
return at >= authorityStart ? stripAt(at) : baseUrl;
}
return baseUrl;
} catch {
if (authorityAtIndex >= authorityStart) {
return stripAt(authorityAtIndex);
}
return stripPoint === -1 ? baseUrl : stripAt(stripPoint);
}
}

const fallbackAt = findUnescapedUserInfoFallbackAt(
baseUrl,
authorityStart,
authorityEnd,
);
return fallbackAt === -1 ? baseUrl : stripAt(fallbackAt);
/**
* A pathless URL with a numeric port followed by prose containing '@' (e.g.
* `https://api.example:8443 - contact admin@example.com`) is misparsed by WHATWG
* as userinfo (username=`api.example`, password=`8443 - contact admin`). The
* prose '@' lands inside the wide authority and would become the strip point,
* corrupting the message. Veto when the segment between the first ':' and the
* next whitespace is all digits (a port) AND the candidate '@' is past the
* colon - a real userinfo like `user@host:99999` has its '@' before the colon.
* #8136.
*
* KNOWN RESIDUAL: a password starting with digits followed by a space (e.g.
* `user:1234 secret@host`) is also vetoed and leaks in full. These two input
* classes are locally indistinguishable at this veto; protecting the common
* pathless-port-with-prose case is the documented tradeoff (see #8136 R1-2).
*/
function isLikelyPathlessPortMisparse(
baseUrl: string,
authorityStart: number,
authorityEnd: number,
): boolean {
const colon = baseUrl.indexOf(':', authorityStart);
if (colon === -1 || colon > authorityEnd) {
return false;
}
const at = baseUrl.indexOf('@', authorityStart);
if (at !== -1 && at < colon) {
return false;
}
const afterColon = baseUrl.slice(colon + 1, authorityEnd);
const wsInAfter = afterColon.search(/\s/);
const portCandidate =
wsInAfter === -1 ? afterColon : afterColon.slice(0, wsInAfter);
return /^\d+$/.test(portCandidate);
}

function findUnescapedUserInfoFallbackAt(
/**
* Locate the userinfo terminator '@' to strip, or -1 when stripping would be
* unsafe. The try-branch of `sanitizeProviderBaseUrl` overrides this with the
* authoritative last '@' from `new URL()` when the parser succeeds, so this
* heuristic only decides (a) whether to veto prose shapes up front, and (b) the
* strip point when `new URL()` throws. Handles the credential-stripping shapes
* from #8136:
*
* - Pathless URL (no `/ ? #`, authority runs to end): veto port+prose and
* host+prose misparses; otherwise the last '@' within the authority is the
* terminator (a password's inner '@' precedes it). A real credential whose
* userinfo contains whitespace (e.g. `user name:pass@host`) still parses with
* `new URL()`, so the try-branch handles it; this path only fires when the
* parser throws.
* - Bounded authority (has `/ ? #`): the last '@' within the authority.
* - Password containing `/ ? #` makes `new URL()` throw and pushes '@' past
* `authorityEnd`; fall back to the full-string last '@' only when there is a
* ':' within the authority (a real userinfo delimiter) and the run between the
* last '/' and the candidate has no whitespace (prose guard).
*/
function findUserInfoStripPoint(
baseUrl: string,
authorityStart: number,
authorityEnd: number,
): number {
const at = baseUrl.lastIndexOf('@');
if (at < authorityStart || authorityEnd >= at) {
const authority = baseUrl.slice(authorityStart, authorityEnd);
const firstWs = authority.search(/\s/);
const isPathless = authorityEnd === baseUrl.length;
// Pathless + prose: only veto when the FIRST whitespace-delimited token has
// neither ':' nor '@' (a bare host followed by prose). A token with ':' or '@'
// is userinfo-shaped and is left for `new URL()` / the bounded branch to
// decide, so whitespace inside a username (e.g. `user name:pass@host`) is not
// falsely vetoed. #8136.
const beforeWs = firstWs === -1 ? authority : authority.slice(0, firstWs);
const isPathlessProse =
isPathless &&
firstWs !== -1 &&
!beforeWs.includes(':') &&
!beforeWs.includes('@');
if (
isLikelyPathlessPortMisparse(baseUrl, authorityStart, authorityEnd) ||
isPathlessProse
) {
return -1;
}
// Bounded authority or pathless-without-prose: the last '@' within the
// authority is the terminator. `new URL()` refines this on the try-branch when
// it parses successfully; the catch-branch (parser threw) uses it directly.
const at = baseUrl.lastIndexOf('@', authorityEnd - 1);
if (at >= authorityStart) {
return at;
}
// Fallback for passwords containing / ? # (new URL() throws, '@' is past
// `authorityEnd`). Require a ':' within the authority (a real userinfo
// delimiter) and reject a full-string '@' when the run between the last '/'
// and it contains whitespace - that is prose, not userinfo. #8136 R3-2.
const fullAt = baseUrl.lastIndexOf('@');
if (fullAt < authorityStart) {
return -1;
}

const colon = baseUrl.indexOf(':', authorityStart);
if (colon === -1 || colon > authorityEnd) {
return -1;
}

const portCandidate = baseUrl.slice(colon + 1, authorityEnd);
return /^\d+$/.test(portCandidate) ? -1 : at;
const lastSlashBeforeAt = baseUrl.lastIndexOf('/', fullAt);
const between =
lastSlashBeforeAt >= authorityStart
? baseUrl.slice(lastSlashBeforeAt, fullAt)
: baseUrl.slice(authorityStart, fullAt);
if (/\s/.test(between)) {
return -1;
}
return fullAt;
}

function findAuthorityEnd(baseUrl: string, authorityStart: number): number {
Expand Down
Loading