Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow csp configuration to dynamically read env variables #10969

Open
raythurnvoid opened this issue Nov 2, 2023 · 2 comments
Open

Allow csp configuration to dynamically read env variables #10969

raythurnvoid opened this issue Nov 2, 2023 · 2 comments
Labels
feature / enhancement New feature or request

Comments

@raythurnvoid
Copy link

Describe the problem

I have an "adapter-node" app deployed somewhere, I have different environments (dev, qa, prod) and i would like to provide different values to the csp configuration based on the env variables set in each environment at runtime.

Describe the proposed solution

Maybe sveltekit may support "app.html" like syntax to read the env variables at runtime:

csp: {
	directives: {
		'frame-src': ['self', '%sveltekit.env.APP_PUBLIC_TRUSTED_DOMAIN%']
	}
}

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

@roelvanhintum
Copy link
Contributor

Late response, but for others running into this problem: You could work around this by replacing the env variable names in the hooks.server.ts handle function.

import type { Handle } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';

export const handle: Handle = async ({ event, resolve }) => {
	const response = await resolve(event);
	
	// Replace ENV variables in CSP
	const csp = response.headers.get('content-security-policy');
	response.headers.set(
		'Content-Security-Policy',
		csp?.replaceAll('APP_PUBLIC_TRUSTED_DOMAIN', env.APP_PUBLIC_TRUSTED_DOMAIN) || '',
	);
	
	return response;
};

@hhvrc
Copy link

hhvrc commented Dec 25, 2024

Issue #13208 if solved should allow for very dynamic CSP HTTP headers which would solve this

@eltigerchino eltigerchino added the feature / enhancement New feature or request label Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature / enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants