From bba6f2f428fdbb67ab5baeb2f7d7053aac1d8268 Mon Sep 17 00:00:00 2001 From: amirhhashemi <87268103+amirhhashemi@users.noreply.github.com> Date: Sat, 1 Mar 2025 16:47:59 +0330 Subject: [PATCH 01/22] Improve security docs --- src/routes/solid-start/guides/security.mdx | 210 ++++++++++++++++++--- 1 file changed, 189 insertions(+), 21 deletions(-) diff --git a/src/routes/solid-start/guides/security.mdx b/src/routes/solid-start/guides/security.mdx index 7b6c2f8ab..dde11685f 100644 --- a/src/routes/solid-start/guides/security.mdx +++ b/src/routes/solid-start/guides/security.mdx @@ -2,35 +2,203 @@ title: Security --- -As a non-opinionated framework, SolidStart doesn't enforce any security practices, though it enables enables developers to implement them as needed. -It is important to know what are the requirements for your own app and implement the fitting security measures. -If at any point you are unsure about the security of your app, or how to achieve something within the constraints of SolidStart reach us on [Discord](https://discord.gg/solidjs). +This guide walks you through how to implement common security mechanisms in SolidStart. -Below you will find a few notes on how to establish some measures. +## XSS (Cross Site Scripting) -## Security Headers +Solid automatically escape values passed to JSX expressions to reduce the risk of XSS attacks. +However, this protection does not apply when using [`innerHTML`](https://docs.solidjs.com/reference/jsx-attributes/innerhtml-or-textcontent#innerhtml-or-textcontent). -Through the use of a [middleware](/solid-start/reference/server/create-middleware#example) it is possible to tab into the `onRequest` event handlers and make sure every request going through your servers have the proper security headers set. -With this, it is possible to setup headers like `Content-Security-Policy`, `X-Frame-Options`, `X-XSS-Protection`, `X-Content-Type-Options`, among others. +To protect your application from XSS attacks: -### Nonces +1. Avoid using [`innerHTML`](/reference/jsx-attributes/innerhtml-or-textcontent#innerhtml-or-textcontent) when possible. + If you must use it, sanitize the content before rendering with libraries such as [DOMPurify](https://github.com/cure53/DOMPurify). +2. Set a Content Security Policy (CSP). +3. Validate and sanitize user inputs. + Always validate form inputs on the server in addition to the client. +4. Use the `HttpOnly` and `Secure` attributes on cookies. -When using `Content-Security-Policy` it is possible to use nonces to allow inline scripts and styles to be executed. -SolidStart enables that smoothly in the [`entry-server.tsx`](/solid-start/reference/entrypoints/entry-server). +## CSP (Content Security Policy) -By passing generating the `nonce` within a middleware and storing it in the `request.locals` object, it is possible to use it in the `entry-server.tsx` to generate the `Content-Security-Policy` header. +To configure the `Content-Security-Policy` HTTP header, you can use a [middleware](/solid-start/advanced/middleware). -## Cross Request Forgery (CSRF) +### With nonce (recommended) -There are multiple ways to add CSRF Protection to your SolidStart app. -The quickest and most common way is to check the `request.referrer` header when the HTTP method is `POST`, `PUT`, `PATCH` or `DELETE`. -This can also be achieved through an `onRequest` [middleware](/solid-start/reference/server/create-middleware#example). +If you want to use a strict CSP with nonces: -## Cross Site Scripting (XSS) +1. Create a middleware that configures the CSP header, then register it to run on the [`onRequest`](/solid-start/advanced/middleware#onrequest) event. +2. Store the nonce in the [`locals`](/solid-start/advanced/middleware#locals) object. +3. Configure SolidStart to use the nonce in your [`entry-server.tsx`](/solid-start/reference/entrypoints/entry-server) file. -SolidStart automatically escape inserts and attributes in HTML. -The exception is when HTML is inserted via the `innerHTML` property, which bypasses the escaping. -Additionally, it's important to note that `