-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create nosecone package for creating secure headers (#2237)
This implements our `nosecone` package and 2 adapters, Next.js and SvelteKit. These 2 frameworks have some of the best support for nonce-based CSPv3—although Next.js has the caveat of it only working in dynamic mode. Runtimes like Bun, Deno, and Node.js can use Nosecone directly to set headers on the responses, while adapters are needed for deeper integration. Using middleware works really well for Next.js because we can force the headers to be forwarded and it even detects the nonce from the `script-src` directive, which it adds to each `<script>` tag that webpack generates. For SvelteKit, we need to provide `csp` in the config so it'll add the CSP header, but we also use a hook to add our additional secure headers. Notably missing: - Removing X-Powered-By—the frameworks don't give us access to this, we should instead just recommend it be removed. - A Remix adapter—it needs a lot of logic to thread nonces throughout the application. - An Express adapter—most people are already using Helmet.
- Loading branch information
1 parent
c89aead
commit 1e8e73b
Showing
42 changed files
with
4,138 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ const aj = arcjet({ | |
], | ||
}); | ||
|
||
export default createMiddleware(aj); | ||
export default createMiddleware(aj); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createMiddleware } from "@nosecone/next"; | ||
|
||
export const config = { | ||
// matcher tells Next.js which routes to run the middleware on | ||
matcher: ["/(.*)"], | ||
}; | ||
|
||
export default createMiddleware(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
import { aj } from "$lib/server/arcjet"; | ||
import { error } from "@sveltejs/kit"; | ||
import type { RequestEvent } from "@sveltejs/kit"; | ||
import { createHook } from "@nosecone/sveltekit"; | ||
import { sequence } from "@sveltejs/kit/hooks"; | ||
|
||
export async function handle({ | ||
event, | ||
resolve, | ||
}: { | ||
event: RequestEvent; | ||
resolve: (event: RequestEvent) => Response | Promise<Response>; | ||
}): Promise<Response> { | ||
// Ignore routes that extend the Arcjet rules - they will call `.protect` themselves | ||
const filteredRoutes = ["/api/rate-limited", "/rate-limited"]; | ||
if (filteredRoutes.includes(event.url.pathname)) { | ||
// return - route will handle protection | ||
return resolve(event); | ||
} | ||
export const handle = sequence( | ||
createHook(), | ||
async ({ event, resolve }) => { | ||
// Ignore routes that extend the Arcjet rules - they will call `.protect` themselves | ||
const filteredRoutes = ["/api/rate-limited", "/rate-limited"]; | ||
if (filteredRoutes.includes(event.url.pathname)) { | ||
// return - route will handle protection | ||
return resolve(event); | ||
} | ||
|
||
// Ensure every other route is protected with shield | ||
const decision = await aj.protect(event); | ||
if (decision.isDenied()) { | ||
return error(403, "Forbidden"); | ||
} | ||
// Ensure every other route is protected with shield | ||
const decision = await aj.protect(event); | ||
if (decision.isDenied()) { | ||
return error(403, "Forbidden"); | ||
} | ||
|
||
// Continue with the route | ||
return resolve(event); | ||
} | ||
// Continue with the route | ||
return await resolve(event); | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/.turbo/ | ||
/coverage/ | ||
/node_modules/ | ||
*.d.ts | ||
*.js | ||
!*.config.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ["@arcjet/eslint-config"], | ||
}; |
Oops, something went wrong.