Skip to content
Open
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
7 changes: 4 additions & 3 deletions packages/dsh-desktop-enterprise/desktop/enterprise-auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { EnterpriseFetch } from './platform-fetch'
import { assertEnterprisePublicRequestPath, validateEnterpriseAppId } from 'dsh-desktop-enterprise/auth-apps-host'
import { ENTERPRISE_APP_REQUEST_MAX_BYTES, assertEnterprisePublicRequestPath, validateEnterpriseAppId } from 'dsh-desktop-enterprise/auth-apps-host'
import { EnterpriseAuthError as PublicAuthError } from 'dsh-desktop-enterprise/auth'

export const AUTH_MAX_REQUEST_BYTES = 1024 * 1024
Expand Down Expand Up @@ -93,11 +93,12 @@ export function parseEnterpriseAuthRequest(input: unknown): EnterpriseAuthReques
let body: Uint8Array | undefined
if (value.body !== undefined) {
if (typeof value.body !== 'string' || method === 'GET' || method === 'HEAD') invalid()
if (value.body.length > AUTH_MAX_REQUEST_BYTES * 2) throw new EnterpriseAuthError('request_too_large', 413)
const limit = application ? ENTERPRISE_APP_REQUEST_MAX_BYTES : AUTH_MAX_REQUEST_BYTES
if (value.body.length > limit * 2) throw new EnterpriseAuthError('request_too_large', 413)
const decoded = Buffer.from(value.body, value.bodyEncoding === 'base64' ? 'base64' : 'utf8')
if (value.bodyEncoding === 'base64' && decoded.toString('base64') !== value.body) invalid()
body = decoded
if (body.byteLength > AUTH_MAX_REQUEST_BYTES) throw new EnterpriseAuthError('request_too_large', 413)
if (body.byteLength > limit) throw new EnterpriseAuthError('request_too_large', 413)
} else if (value.bodyEncoding !== undefined) invalid()
return { ...application, path, method, headers, ...(body === undefined ? {} : { body }), sessionVersion }
}
Expand Down
2 changes: 2 additions & 0 deletions packages/dsh-desktop-enterprise/enterprise-apps-host.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { validateEnterpriseAppId, assertEnterprisePublicRequestPath } from './enterprise-apps-contract.js'

export const ENTERPRISE_APP_REQUEST_MAX_BYTES: number

export interface EnterpriseAppContext {
base: string
userId: string
Expand Down
2 changes: 2 additions & 0 deletions packages/dsh-desktop-enterprise/enterprise-apps-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { validateEnterpriseAppId } from './enterprise-apps-contract.js'
export { createEnterpriseAppTokenCache } from './enterprise-app-token-cache.js'
export { validateEnterpriseAppId, assertEnterprisePublicRequestPath } from './enterprise-apps-contract.js'

export const ENTERPRISE_APP_REQUEST_MAX_BYTES = 3 * 1024 * 1024

const MAX_RESPONSE_BYTES = 64 * 1024
const TIMEOUT_MS = 15_000

Expand Down
2 changes: 2 additions & 0 deletions packages/dsh-desktop-enterprise/enterprise-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export function createEnterpriseAuthTransport(origin, capability) {
try { headers = Object.fromEntries(new Headers(init.headers)) }
catch { throw new EnterpriseAuthError('invalid_request', 'Invalid enterprise request headers.', 400) }
let body = init.body, bodyEncoding
// Bound the wire size even when JSON would escape every byte of application text.
if (appId !== undefined && typeof body === 'string') body = Buffer.from(body)
if (body != null && typeof body !== 'string') {
if (body instanceof ArrayBuffer) body = new Uint8Array(body)
if (!ArrayBuffer.isView(body)) throw new EnterpriseAuthError('invalid_request', 'Unsupported request body.', 400)
Expand Down
8 changes: 4 additions & 4 deletions packages/dsh-desktop-enterprise/web-auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EnterpriseAuthError } from './enterprise-auth-client.js'
import { resolveEnterpriseUrl } from './deep-link.js'
import { createEnterpriseAppTokenCache, resolveEnterpriseAppAccess, validateEnterpriseAppId, assertEnterprisePublicRequestPath } from './enterprise-apps-host.js'
import { ENTERPRISE_APP_REQUEST_MAX_BYTES, createEnterpriseAppTokenCache, resolveEnterpriseAppAccess, validateEnterpriseAppId, assertEnterprisePublicRequestPath } from './enterprise-apps-host.js'

const MAX_REQUEST = 1024 * 1024
const MAX_RESPONSE = 32 * 1024 * 1024
Expand All @@ -10,7 +10,7 @@ function failure(code, status = 403) {
return new EnterpriseAuthError(code, `Enterprise authorization failed (${code}).`, status)
}

function validateRequest(path, init) {
function validateRequest(path, init, bodyLimit) {
if (typeof path !== 'string' || path.length > 8192 || !path.startsWith('/') || path.startsWith('//') || /[\\#\s\x00-\x1f\x7f]/u.test(path) || /%5c|%0[ad]/iu.test(path)) throw failure('invalid_request', 400)
const origin = 'https://enterprise.invalid'
const url = new URL(path, origin)
Expand All @@ -33,7 +33,7 @@ function validateRequest(path, init) {
else if (body instanceof ArrayBuffer) body = Buffer.from(body)
else if (ArrayBuffer.isView(body)) body = Buffer.from(body.buffer, body.byteOffset, body.byteLength)
else throw failure('invalid_request', 400)
if (body.byteLength > MAX_REQUEST) throw failure('request_too_large', 413)
if (body.byteLength > bodyLimit) throw failure('request_too_large', 413)
}
return { path: url.pathname + url.search, method, headers, body }
}
Expand Down Expand Up @@ -83,7 +83,7 @@ export function createWebEnterpriseAuth({ getAccount, getSessionAuth, refreshSes
if (init.appBaseUrl !== undefined || init.appScopes !== undefined) throw failure('invalid_request', 400)
assertEnterprisePublicRequestPath(path)
}
const parsed = validateRequest(path, init)
const parsed = validateRequest(path, init, appId === undefined ? MAX_REQUEST : ENTERPRISE_APP_REQUEST_MAX_BYTES)
assertSession(grant, init.sessionVersion)
if (requests.size >= 8 || grant.controllers.size >= 4) throw failure('too_many_requests', 429)
const controller = new AbortController()
Expand Down
15 changes: 15 additions & 0 deletions test/enterprise-apps-integration.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ for (const mode of ['desktop', 'web']) describe(mode + ' enterprise application
expect(JSON.stringify(client.getState())).not.toContain('secret')
})

it.each([
['escaped text', size => '\0'.repeat(size)],
['binary', size => new Uint8Array(size).fill(255)]
])('enforces the 3 MiB application body limit for %s', async (_, makeBody) => {
const { client, calls } = await fixture(mode)
const app = client.app('xpie', { baseUrl: appBase })
const limit = 3 * 1024 * 1024
const post = body => app.request('/v1/upload', { method: 'POST', body })
await expect(post(makeBody(limit + 1))).rejects.toMatchObject({ errorCode: 'request_too_large', status: 413 })
expect(calls).toHaveLength(0)
const body = makeBody(limit)
expect(await (await post(body)).text()).toBe('business result')
expect(Buffer.from(calls.find(call => call.kind === 'business').init.body).equals(Buffer.from(body))).toBe(true)
})

it('reuses one exchange across sequential business requests and keeps permission checks online', async () => {
const f = await fixture(mode)
const app = f.client.app('xpie', { baseUrl: appBase })
Expand Down
Loading