Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions packages/dsh-desktop-enterprise/deep-link.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export interface EnterpriseLoginDeepLink {
serverUrl: string
}
export interface EnterpriseUrlOptions {
/** Retained for compatibility; literal loopback HTTP is always allowed. */
allowInsecureLoopback?: boolean
/** Legacy name: permits all non-loopback HTTP origins after risk confirmation. */
allowInsecurePrivateHttp?: boolean
}
export declare function isInsecurePrivateHttpOrigin(value: string): boolean
Expand Down
49 changes: 5 additions & 44 deletions packages/dsh-desktop-enterprise/deep-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,12 @@ function isLoopbackHost(hostname) {
return hostname === '127.0.0.1' || hostname === '[::1]' || hostname === '::1'
}

function parseIpv4(hostname) {
const parts = hostname.split('.')
if (parts.length !== 4) return undefined
const octets = []
for (const part of parts) {
if (!/^\d{1,3}$/u.test(part)) return undefined
const value = Number(part)
if (!Number.isInteger(value) || value < 0 || value > 255) return undefined
octets.push(value)
}
return octets
}

function isPrivateIpv4(octets) {
const [first, second] = octets
return first === 10
|| (first === 192 && second === 168)
|| (first === 172 && second >= 16 && second <= 31)
}

function ipv6Hostname(hostname) {
return hostname.startsWith('[') && hostname.endsWith(']') ? hostname.slice(1, -1) : hostname
}

function isPrivateNetworkHost(hostname) {
const mapped = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/iu.exec(ipv6Hostname(hostname))
const ipv4 = parseIpv4(mapped?.[1] ?? hostname)
if (ipv4) return isPrivateIpv4(ipv4)
return /^f[cd][0-9a-f]{2}:/iu.test(ipv6Hostname(hostname))
}

// Keep the legacy API name for broker/client compatibility. It now covers all
// non-loopback HTTP origins, including public IPs and domain names.
export function isInsecurePrivateHttpOrigin(value) {
try {
const url = new URL(value)
return url.protocol === 'http:' && isPrivateNetworkHost(url.hostname)
return url.protocol === 'http:' && !isLoopbackHost(url.hostname)
} catch {
return false
}
Expand All @@ -69,20 +40,10 @@ export function normalizeEnterpriseServerUrl(value, options = {}) {
return url.origin
}
if (url.protocol === 'http:') {
if (options.allowInsecureLoopback === true && isLoopbackHost(url.hostname)) {
if (isLoopbackHost(url.hostname) || options.allowInsecurePrivateHttp === true) {
return url.origin
}
if (options.allowInsecurePrivateHttp === true && isPrivateNetworkHost(url.hostname)) {
return url.origin
}
if (isPrivateNetworkHost(url.hostname)) {
throw new Error('HTTP intranet addresses require explicit confirmation.')
}
throw new Error(
options.allowInsecureLoopback === true
? 'HTTP BiSheng addresses are limited to 127.0.0.1 or [::1].'
: 'The BiSheng platform address must use HTTPS.'
)
throw new Error('HTTP addresses require explicit confirmation.')
}
throw new Error('The BiSheng platform address must use HTTP or HTTPS.')
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/enterprise/enterprise-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class EnterpriseService {
allowInsecurePrivateHttp: true
})
if (isInsecurePrivateHttpOrigin(preview) && options.confirmInsecurePrivateHttp !== true) {
throw Object.assign(new Error('HTTP intranet addresses require explicit confirmation.'), {
throw Object.assign(new Error('HTTP addresses require explicit confirmation.'), {
status: 400
})
}
Expand Down
74 changes: 27 additions & 47 deletions test/enterprise-deep-link.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import {
enterpriseLoginDeepLinkFromArgv,
isInsecurePrivateHttpOrigin,
normalizeEnterpriseServerUrl,
parseEnterpriseLoginDeepLink
} from '../packages/dsh-desktop-enterprise/deep-link.js'
Expand All @@ -22,58 +23,37 @@ describe('BiSheng enterprise deep link', () => {
expect(parsed.url).toBe('bisheng-dev://login?server=https%3A%2F%2Fbisheng.example.com')
})

it('rejects remote HTTP origins and localhost, even in development', () => {
expect(() => parseEnterpriseLoginDeepLink(
'bisheng://login?server=http%3A%2F%2Fbisheng.example.com%3A8080'
)).toThrow('must use HTTPS')
expect(() => parseEnterpriseLoginDeepLink(
'bisheng://login?server=http%3A%2F%2Fbisheng.example.com%3A8080',
{ allowInsecureLoopback: true }
)).toThrow('127.0.0.1 or [::1]')
expect(() => normalizeEnterpriseServerUrl('http://localhost:17860', { allowInsecureLoopback: true }))
.toThrow('127.0.0.1 or [::1]')
it.each(['http://127.0.0.1:17860', 'http://[::1]:17860'])('allows literal loopback HTTP without a development opt-in: %s', (base) => {
expect(normalizeEnterpriseServerUrl(base, { allowInsecureLoopback: false })).toBe(base)
expect(isInsecurePrivateHttpOrigin(base)).toBe(false)
expect(parseEnterpriseLoginDeepLink('bisheng://login?server=' + encodeURIComponent(base)).serverUrl).toBe(base)
})

it('allows HTTP only for loopback when explicitly opted in', () => {
expect(normalizeEnterpriseServerUrl('http://127.0.0.1:17860', { allowInsecureLoopback: true }))
.toBe('http://127.0.0.1:17860')
expect(normalizeEnterpriseServerUrl('http://[::1]:17860', { allowInsecureLoopback: true }))
.toBe('http://[::1]:17860')
expect(parseEnterpriseLoginDeepLink(
'bisheng://login?server=http%3A%2F%2F127.0.0.1%3A17860',
{ allowInsecureLoopback: true }
).serverUrl).toBe('http://127.0.0.1:17860')
it.each([
'http://8.8.8.8',
'http://bisheng.example.com:8080',
'http://bisheng.internal',
'http://localhost:17860',
'http://192.168.106.119:30006',
'http://10.1.2.3',
'http://172.15.0.1',
'http://172.32.0.1',
'http://[fd12:3456::1]',
'http://[2001:db8::1]'
])('requires confirmation for every non-loopback HTTP origin: %s', (base) => {
expect(() => normalizeEnterpriseServerUrl(base)).toThrow('explicit confirmation')
expect(() => normalizeEnterpriseServerUrl(base, { allowInsecureLoopback: true })).toThrow('explicit confirmation')
expect(isInsecurePrivateHttpOrigin(base)).toBe(true)
expect(normalizeEnterpriseServerUrl(base, { allowInsecurePrivateHttp: true })).toBe(base)
const link = 'bisheng://login?server=' + encodeURIComponent(base)
expect(() => parseEnterpriseLoginDeepLink(link)).toThrow('explicit confirmation')
expect(parseEnterpriseLoginDeepLink(link, { allowInsecurePrivateHttp: true }).serverUrl).toBe(base)
})

it('requires HTTPS when insecure loopback is not allowed', () => {
it('keeps HTTPS warning-free', () => {
expect(normalizeEnterpriseServerUrl('https://bisheng.example.com')).toBe('https://bisheng.example.com')
expect(() => normalizeEnterpriseServerUrl('http://127.0.0.1:17860')).toThrow('must use HTTPS')
})

it('allows HTTP private-network IPs only after an explicit opt-in', () => {
expect(() => normalizeEnterpriseServerUrl('http://192.168.106.119:30006')).toThrow('explicit confirmation')
expect(() => normalizeEnterpriseServerUrl('http://10.0.0.8:8080', { allowInsecureLoopback: true }))
.toThrow('explicit confirmation')
expect(() => normalizeEnterpriseServerUrl('http://8.8.8.8', { allowInsecurePrivateHttp: true }))
.toThrow('must use HTTPS')
expect(() => normalizeEnterpriseServerUrl('http://bisheng.internal', { allowInsecurePrivateHttp: true }))
.toThrow('must use HTTPS')
expect(normalizeEnterpriseServerUrl('http://192.168.106.119:30006', { allowInsecurePrivateHttp: true }))
.toBe('http://192.168.106.119:30006')
expect(normalizeEnterpriseServerUrl('http://10.1.2.3', { allowInsecurePrivateHttp: true }))
.toBe('http://10.1.2.3')
expect(normalizeEnterpriseServerUrl('http://172.16.0.1:443', { allowInsecurePrivateHttp: true }))
.toBe('http://172.16.0.1:443')
expect(() => normalizeEnterpriseServerUrl('http://172.15.0.1', { allowInsecurePrivateHttp: true }))
.toThrow('must use HTTPS')
expect(() => normalizeEnterpriseServerUrl('http://172.32.0.1', { allowInsecurePrivateHttp: true }))
.toThrow('must use HTTPS')
expect(normalizeEnterpriseServerUrl('http://[fd12:3456::1]', { allowInsecurePrivateHttp: true }))
.toBe('http://[fd12:3456::1]')
expect(parseEnterpriseLoginDeepLink(
'bisheng://login?server=http%3A%2F%2F192.168.106.119%3A30006',
{ allowInsecurePrivateHttp: true }
).serverUrl).toBe('http://192.168.106.119:30006')
expect(isInsecurePrivateHttpOrigin('https://bisheng.example.com')).toBe(false)
expect(isInsecurePrivateHttpOrigin('not a URL')).toBe(false)
})

it.each([
Expand Down
23 changes: 22 additions & 1 deletion test/enterprise-insecure-confirm-view.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Context } from '@deepseek-ai/cordis'
import { createRoot } from 'react-dom/client'
import { JSDOM } from 'jsdom'
import { afterEach, expect, it, vi } from 'vitest'
import { isInsecurePrivateHttpOrigin, normalizeEnterpriseServerUrl } from '../packages/dsh-desktop-enterprise/deep-link.js'

// Coverage AC: AC-03, AC-30, AC-31, AC-56.
const source = await readFile(new URL('../packages/dsh-desktop-enterprise/client.js', import.meta.url), 'utf8')
Expand Down Expand Up @@ -92,7 +93,9 @@ async function mountEnterprise(initial, hooks = {}) {
return { ok: true, json: async () => snapshot() }
}
if (path === '/api/enterprise.base.inspect' || path === '/api/enterprise.deep-link.inspect') {
return { ok: true, json: async () => ({ base: SERVICE, insecurePrivateHttp: true }) }
const value = body.base ?? new URL(body.url).searchParams.get('server')
const base = normalizeEnterpriseServerUrl(value, { allowInsecurePrivateHttp: true })
return { ok: true, json: async () => ({ base, insecurePrivateHttp: isInsecurePrivateHttpOrigin(base) }) }
}
return { ok: true, json: async () => snapshot() }
}
Expand Down Expand Up @@ -161,6 +164,24 @@ async function mountEnterprise(initial, hooks = {}) {
return { text, click, requests, deliverLoginLink, flushTimer, document: dom.window.document }
}

it.each(['http://203.0.113.10:8080', 'http://bisheng.example.com'])('requires risk confirmation before starting HTTP sign-in: %s', async (base) => {
const { text, click, requests } = await mountEnterprise({ ...idleAccount(), base })
await click('在浏览器中登录')
expect(text()).toContain('登录信息会明文传输')
expect(text()).toContain(base)
expect(requests.some(item => item.path === '/api/enterprise.login.start')).toBe(false)
await click('确认为可信来源')
expect(requests.find(item => item.path === '/api/enterprise.login.start').body).toEqual({ base, confirmInsecurePrivateHttp: true })
})

it('starts loopback HTTP sign-in without a risk prompt', async () => {
const base = 'http://127.0.0.1:17860'
const { text, click, requests } = await mountEnterprise({ ...idleAccount(), base })
await click('在浏览器中登录')
expect(text()).not.toContain('登录信息会明文传输')
expect(requests.find(item => item.path === '/api/enterprise.login.start').body).toEqual({ base })
})

it('drops the trusted-source prompt after sign-in and shows it again only on the next browser login', async () => {
const { text, click, requests, deliverLoginLink } = await mountEnterprise(idleAccount())
expect(document.querySelector('input[type="url"]').value).toBe(SERVICE)
Expand Down
Loading