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
36 changes: 16 additions & 20 deletions packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from 'node:path'
import fsp from 'node:fs/promises'
import type { ServerResponse } from 'node:http'
import type { Connect } from 'dep-types/connect'
import colors from 'picocolors'
import type { ExistingRawSourceMap } from 'rollup'
Expand Down Expand Up @@ -34,6 +33,7 @@ import {
ERR_OUTDATED_OPTIMIZED_DEP,
NULL_BYTE_PLACEHOLDER,
} from '../../../shared/constants'
import type { ResolvedConfig } from '../../config'
import { checkLoadingAccess, respondWithAccessDenied } from './static'

const debugCache = createDebugger('vite:cache')
Expand All @@ -46,23 +46,9 @@ const rawRE = /[?&]raw\b/
const inlineRE = /[?&]inline\b/
const svgRE = /\.svg\b/

function deniedServingAccessForTransform(
id: string,
server: ViteDevServer,
res: ServerResponse,
next: Connect.NextFunction,
) {
function isServerAccessDeniedForTransform(config: ResolvedConfig, id: string) {
if (rawRE.test(id) || urlRE.test(id) || inlineRE.test(id) || svgRE.test(id)) {
const servingAccessResult = checkLoadingAccess(server.config, id)
if (servingAccessResult === 'denied') {
respondWithAccessDenied(id, server, res)
return true
}
if (servingAccessResult === 'fallback') {
next()
return true
}
servingAccessResult satisfies 'allowed'
return checkLoadingAccess(config, id) !== 'allowed'
}
return false
}
Expand Down Expand Up @@ -243,7 +229,7 @@ export function transformMiddleware(
allowId(id) {
return (
id.startsWith('\0') ||
!deniedServingAccessForTransform(id, server, res, next)
!isServerAccessDeniedForTransform(server.config, id)
)
},
})
Expand Down Expand Up @@ -317,8 +303,18 @@ export function transformMiddleware(
return next()
}
if (e?.code === ERR_DENIED_ID) {
// next() is called in ensureServingAccess
return
const id: string = e.id
const servingAccessResult = checkLoadingAccess(server.config, id)
if (servingAccessResult === 'denied') {
respondWithAccessDenied(id, server, res)
return true
}
if (servingAccessResult === 'fallback') {
next()
return true
}
servingAccessResult satisfies 'allowed'
throw new Error(`Unexpected access result for id ${id}`)
}
return next(e)
}
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ async function loadAndTransform(
if (options.allowId && !options.allowId(id)) {
const err: any = new Error(`Denied ID ${id}`)
err.code = ERR_DENIED_ID
err.id = id
throw err
}

Expand Down
Loading