A comprehensive guide to securing your SvelteKit application. Covering 10 vulnerability categories with framework-specific code examples and best practices.
Use `$env/static/private` and `$env/dynamic/private` for server-only secrets. Never import from `$env/static/public` for sensitive values.
SvelteKit has built-in CSRF protection for form actions. Ensure you are using form actions rather than custom API endpoints for state-changing operations.
Validate all data in `+server.ts` endpoints and `+page.server.ts` load functions. These are public-facing server endpoints.
Use hooks (`hooks.server.ts`) for global authentication and authorization checks before requests reach route handlers.
Configure security headers in `svelte.config.js` or through hooks. SvelteKit does not set security headers by default.
Be cautious with `event.locals` -- data set here is available to all subsequent handlers in the request pipeline.
Implement rate limiting in hooks or middleware, especially for form actions and API endpoints.
Use `+page.server.ts` load functions to keep data fetching on the server. Avoid exposing internal API URLs in client-side code.
Cross-Site Scripting (XSS)
A03:2021 - Injection · CWE-79
Cross-Site Request Forgery (CSRF)
A01:2021 - Broken Access Control · CWE-352
Insecure Direct Object References (IDOR)
A01:2021 - Broken Access Control · CWE-639
Sensitive Data Exposure
A02:2021 - Cryptographic Failures · CWE-200
Server-Side Request Forgery (SSRF)
A10:2021 - Server-Side Request Forgery · CWE-918
Automatically test your SvelteKit application for all 10 vulnerability categories. Get actionable results in minutes.
Start Free Scan