Skip to content

Commit 93acc31

Browse files
authored
Merge branch 'next' into zod4-m
2 parents 64f1796 + 941d8f2 commit 93acc31

40 files changed

Lines changed: 395 additions & 356 deletions

File tree

.changeset/tiny-books-scream.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@astrojs/netlify': minor
3+
'@astrojs/vercel': minor
4+
'@astrojs/node': minor
5+
'astro': minor
6+
---
7+
8+
Removes the `experimental.csp` flag and replaces it with a new configuration option `security.csp` - ([v6 upgrade guidance](https://v6.docs.astro.build/en/guides/upgrade-to/v6/#experimental-flags))

packages/astro/e2e/csp-client-only.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { testFactory } from './test-utils.js';
33

44
const test = testFactory(import.meta.url, {
55
root: './fixtures/client-only/',
6-
experimental: {
6+
security: {
77
csp: true,
88
},
99
});

packages/astro/e2e/fixtures/csp-server-islands/astro.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default defineConfig({
1010
adapter: nodejs({ mode: 'standalone' }),
1111
integrations: [react(), mdx()],
1212
trailingSlash: process.env.TRAILING_SLASH ?? 'always',
13-
experimental: {
13+
security: {
1414
csp: true
1515
}
1616
});

packages/astro/src/assets/fonts/vite-plugin-fonts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ export function fontsPlugin({ settings, sync, logger }: Options): Plugin {
188188
consumableMap = res.consumableMap;
189189

190190
// Handle CSP
191-
if (shouldTrackCspHashes(settings.config.experimental.csp)) {
192-
const algorithm = getAlgorithm(settings.config.experimental.csp);
191+
if (shouldTrackCspHashes(settings.config.security.csp)) {
192+
const algorithm = getAlgorithm(settings.config.security.csp);
193193

194194
// Generate a hash for each style we generate
195195
for (const { css } of internalConsumableMap.values()) {

packages/astro/src/assets/vite-plugin-assets.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { emitImageMetadata, hashTransform, propsToFilename } from './utils/node.
2222
import { getProxyCode } from './utils/proxy.js';
2323
import { makeSvgComponent } from './utils/svg.js';
2424
import { createPlaceholderURL, stringifyPlaceholderURL } from './utils/url.js';
25+
import { isAstroServerEnvironment } from '../environments.js';
2526

2627
const resolvedVirtualModuleId = '\0' + VIRTUAL_MODULE_ID;
2728

@@ -126,9 +127,9 @@ export default function assets({ fs, settings, sync, logger }: Options): vite.Pl
126127
config(_, env) {
127128
isBuild = env.command === 'build';
128129
},
129-
async resolveId(id, _importer, options) {
130+
async resolveId(id, _importer) {
130131
if (id === VIRTUAL_SERVICE_ID) {
131-
if (options?.ssr) {
132+
if (isAstroServerEnvironment(this.environment)) {
132133
return await this.resolve(settings.config.image.service.entrypoint);
133134
}
134135
return await this.resolve('astro/assets/services/noop');
@@ -230,7 +231,7 @@ export default function assets({ fs, settings, sync, logger }: Options): vite.Pl
230231
configResolved(viteConfig) {
231232
resolvedConfig = viteConfig;
232233
},
233-
async load(id, options) {
234+
async load(id) {
234235
if (assetRegex.test(id)) {
235236
if (!globalThis.astroAsset.referencedImages)
236237
globalThis.astroAsset.referencedImages = new Set();
@@ -260,7 +261,7 @@ export default function assets({ fs, settings, sync, logger }: Options): vite.Pl
260261
// We can only reliably determine if an image is used on the server, as we need to track its usage throughout the entire build.
261262
// Since you cannot use image optimization on the client anyway, it's safe to assume that if the user imported
262263
// an image on the client, it should be present in the final build.
263-
if (options?.ssr) {
264+
if (isAstroServerEnvironment(this.environment)) {
264265
if (id.endsWith('.svg')) {
265266
const contents = await fs.promises.readFile(imageMetadata.fsPath, {
266267
encoding: 'utf8',

packages/astro/src/content/vite-plugin-content-assets.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { hasContentFlag } from './utils.js';
2121
import { joinPaths, prependForwardSlash, slash } from '@astrojs/internal-helpers/path';
2222
import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../core/constants.js';
23+
import { isAstroServerEnvironment } from '../environments.js';
2324

2425
export function astroContentAssetPropagationPlugin({
2526
settings,
@@ -74,14 +75,14 @@ export function astroContentAssetPropagationPlugin({
7475
server.environments[ASTRO_VITE_ENVIRONMENT_NAMES.ssr] as RunnableDevEnvironment,
7576
);
7677
},
77-
async transform(_, id, options) {
78+
async transform(_, id) {
7879
if (hasContentFlag(id, PROPAGATED_ASSET_FLAG)) {
7980
const basePath = id.split('?')[0];
8081
let stringifiedLinks: string, stringifiedStyles: string;
8182

8283
// We can access the server in dev,
8384
// so resolve collected styles and scripts here.
84-
if (options?.ssr && devModuleLoader) {
85+
if (isAstroServerEnvironment(this.environment) && devModuleLoader) {
8586
if (!devModuleLoader.getModuleById(basePath)?.ssrModule) {
8687
await devModuleLoader.import(basePath);
8788
}

packages/astro/src/content/vite-plugin-content-virtual-mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from './consts.js';
2424
import { getDataStoreFile } from './content-layer.js';
2525
import { getContentPaths, isDeferredModule } from './utils.js';
26+
import { isAstroClientEnvironment } from '../environments.js';
2627

2728
interface AstroContentVirtualModPluginParams {
2829
settings: AstroSettings;
@@ -113,7 +114,7 @@ export function astroContentVirtualModPlugin({
113114
},
114115
async load(id) {
115116
if (id === RESOLVED_VIRTUAL_MODULE_ID) {
116-
const isClient = this.environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.client;
117+
const isClient = isAstroClientEnvironment(this.environment);
117118
const code = await generateContentEntryFile({
118119
settings,
119120
fs,

packages/astro/src/core/build/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ async function generatePath(
612612

613613
if (
614614
settings.adapter?.adapterFeatures?.experimentalStaticHeaders &&
615-
settings.config.experimental?.csp
615+
settings.config.security?.csp
616616
) {
617617
routeToHeaders.set(pathname, { headers: responseHeaders, route: integrationRoute });
618618
}

packages/astro/src/core/build/plugins/plugin-manifest.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,14 @@ async function buildManifest(
301301

302302
let csp: SSRManifestCSP | undefined = undefined;
303303

304-
if (shouldTrackCspHashes(settings.config.experimental.csp)) {
305-
const algorithm = getAlgorithm(settings.config.experimental.csp);
304+
if (shouldTrackCspHashes(settings.config.security.csp)) {
305+
const algorithm = getAlgorithm(settings.config.security.csp);
306306
const scriptHashes = [
307-
...getScriptHashes(settings.config.experimental.csp),
307+
...getScriptHashes(settings.config.security.csp),
308308
...(await trackScriptHashes(internals, settings, algorithm)),
309309
];
310310
const styleHashes = [
311-
...getStyleHashes(settings.config.experimental.csp),
311+
...getStyleHashes(settings.config.security.csp),
312312
...settings.injectedCsp.styleHashes,
313313
...(await trackStyleHashes(internals, settings, algorithm)),
314314
];
@@ -318,12 +318,12 @@ async function buildManifest(
318318
? 'adapter'
319319
: undefined,
320320
scriptHashes,
321-
scriptResources: getScriptResources(settings.config.experimental.csp),
321+
scriptResources: getScriptResources(settings.config.security.csp),
322322
styleHashes,
323-
styleResources: getStyleResources(settings.config.experimental.csp),
323+
styleResources: getStyleResources(settings.config.security.csp),
324324
algorithm,
325325
directives: getDirectives(settings),
326-
isStrictDynamic: getStrictDynamic(settings.config.experimental.csp),
326+
isStrictDynamic: getStrictDynamic(settings.config.security.csp),
327327
};
328328
}
329329

packages/astro/src/core/config/schemas/base.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export const ASTRO_CONFIG_DEFAULTS = {
8989
security: {
9090
checkOrigin: true,
9191
allowedDomains: [],
92+
csp: false,
9293
},
9394
env: {
9495
schema: {},
@@ -99,7 +100,6 @@ export const ASTRO_CONFIG_DEFAULTS = {
99100
experimental: {
100101
clientPrerender: false,
101102
contentIntellisense: false,
102-
csp: false,
103103
chromeDevtoolsWorkspace: false,
104104
svgo: false,
105105
},
@@ -433,6 +433,29 @@ export const AstroConfigSchema = z.object({
433433
)
434434
.optional()
435435
.default(ASTRO_CONFIG_DEFAULTS.security.allowedDomains),
436+
csp: z
437+
.union([
438+
z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.security.csp),
439+
z.object({
440+
algorithm: cspAlgorithmSchema,
441+
directives: z.array(allowedDirectivesSchema).optional(),
442+
styleDirective: z
443+
.object({
444+
resources: z.array(z.string()).optional(),
445+
hashes: z.array(cspHashSchema).optional(),
446+
})
447+
.optional(),
448+
scriptDirective: z
449+
.object({
450+
resources: z.array(z.string()).optional(),
451+
hashes: z.array(cspHashSchema).optional(),
452+
strictDynamic: z.boolean().optional(),
453+
})
454+
.optional(),
455+
}),
456+
])
457+
.optional()
458+
.default(ASTRO_CONFIG_DEFAULTS.security.csp),
436459
})
437460
.optional()
438461
.default(ASTRO_CONFIG_DEFAULTS.security),
@@ -483,29 +506,6 @@ export const AstroConfigSchema = z.object({
483506
.optional()
484507
.default(ASTRO_CONFIG_DEFAULTS.experimental.contentIntellisense),
485508
fonts: z.array(z.union([localFontFamilySchema, remoteFontFamilySchema])).optional(),
486-
csp: z
487-
.union([
488-
z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.csp),
489-
z.object({
490-
algorithm: cspAlgorithmSchema,
491-
directives: z.array(allowedDirectivesSchema).optional(),
492-
styleDirective: z
493-
.object({
494-
resources: z.array(z.string()).optional(),
495-
hashes: z.array(cspHashSchema).optional(),
496-
})
497-
.optional(),
498-
scriptDirective: z
499-
.object({
500-
resources: z.array(z.string()).optional(),
501-
hashes: z.array(cspHashSchema).optional(),
502-
strictDynamic: z.boolean().optional(),
503-
})
504-
.optional(),
505-
}),
506-
])
507-
.optional()
508-
.default(ASTRO_CONFIG_DEFAULTS.experimental.csp),
509509
chromeDevtoolsWorkspace: z
510510
.boolean()
511511
.optional()

0 commit comments

Comments
 (0)