-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Disable cookie secure option when using vite dev --host #10438
Comments
Current implementation checks for kit/packages/kit/src/runtime/server/cookie.js Lines 33 to 38 in 8c4b74c
Why not check for the
Would there be security issues with this? |
Ran also into this because I'm encapsulating my project in a docker container, so
and then restore it to its original form afterwards
This works if using GNU |
I've settled on this workaround for the dev environment. import { sveltekit } from '@sveltejs/kit/vite';
import basicSsl from '@vitejs/plugin-basic-ssl';
import { defineConfig } from 'vitest/config';
export default defineConfig({
plugins: [
...(process.argv.includes('--host') ? [basicSsl()] : []), //
sveltekit(),
],
}); It conditionally loads the pnpm dev
# VITE v5.0.10 ready in 533 ms
#
# ➜ Local: http://localhost:5174/
# ➜ Network: use --host to expose
pnpm dev --host
# VITE v5.0.10 ready in 574 ms
#
# ➜ Local: https://localhost:5173/
# ➜ Network: https://172.30.1.84:5173/
# ➜ Network: https://100.116.137.49:5173/
# ➜ press h + enter to show help |
The above workaround does not work in Since they run the app in Node.js, all Node.js adapter related restrictions apply as well.
Here is the updated Note that the import { sveltekit } from '@sveltejs/kit/vite';
import basicSsl from '@vitejs/plugin-basic-ssl';
import { defineConfig } from 'vitest/config';
export default defineConfig(({ command, isPreview }) => {
const exposed = command === 'serve' && process.argv.includes('--host');
if (exposed && isPreview) {
console.error('🔴 In the preview mode, the Node.js server cannot determine the request URL.');
console.error('🔴 If the CSRF protection is enabled (default) POST form submits will fail.');
}
return {
plugins: [
//
...(exposed && !isPreview ? [basicSsl()] : []),
sveltekit(),
],
};
}); |
Vite does expose dev, preview server URLs in plugins:
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
sveltekit(),
{
name: 'urls',
configureServer: (server) => {
server.httpServer?.on('listening', () => {
setTimeout(() => {
console.log(server.resolvedUrls);
// { local: ['http://localhost:5173/'],
// network: ['http://192.168.0.2:5173/'] };
}, 10); // Setting this to 0 results in null
});
}
}
]
}); But this cannot be shared using And Would be nice if SvelteKit disables secure cookie in dev / preview servers. |
Is using the |
@eltigerchino It is a viable, shortterm workaround. import { dev } from '$app/environment';
export const load = ({ cookies }) => {
cookies.set('name', 'value', { path: '/', secure: !dev });
}; The limitations are:
Should I add |
you can have a secure dev server too, so this setting would have to be derived from the url ( secure = url.startsWith('https://') |
Updated workaround:
import { dev } from '$app/environment';
export const load = ({ cookies, url }) => {
cookies.set('name', 'value', { path: '/', secure: !dev || url.protocol === 'https:' });
}; |
Describe the problem
Thanks to the
localhost
exception, cookies can be set in the development environment with ease.https://kit.svelte.dev/docs/types#public-types-cookies
However, the exception is not applied if the Vite server is exposed over the network, and is accessed using an IP address.
npx vite dev --host # VITE v4.4.4 ready in 612 ms
Describe the proposed solution
Disable the
secure
option in thecookies.set()
API if it is a Vite dev server. (and possibly in the preview server)Alternatives considered
No response
Importance
nice to have
Additional Information
No response
The text was updated successfully, but these errors were encountered: