@nosecone/sveltekit 1.0.0-alpha.31 → 1.0.0-alpha.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +8 -9
  2. package/index.ts +0 -119
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nosecone/sveltekit",
3
- "version": "1.0.0-alpha.31",
3
+ "version": "1.0.0-alpha.32",
4
4
  "description": "Protect your SvelteKit application with secure headers",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://arcjet.com",
@@ -29,7 +29,6 @@
29
29
  "README.md",
30
30
  "*.js",
31
31
  "*.d.ts",
32
- "*.ts",
33
32
  "!*.config.js"
34
33
  ],
35
34
  "scripts": {
@@ -40,19 +39,19 @@
40
39
  "test": "node --test"
41
40
  },
42
41
  "dependencies": {
43
- "nosecone": "1.0.0-alpha.31"
42
+ "nosecone": "1.0.0-alpha.32"
44
43
  },
45
44
  "peerDependencies": {
46
45
  "@sveltejs/kit": ">=2"
47
46
  },
48
47
  "devDependencies": {
49
- "@arcjet/eslint-config": "1.0.0-alpha.31",
50
- "@arcjet/rollup-config": "1.0.0-alpha.31",
51
- "@arcjet/tsconfig": "1.0.0-alpha.31",
52
- "@rollup/wasm-node": "4.27.3",
53
- "@sveltejs/kit": "^2.8.0",
48
+ "@arcjet/eslint-config": "1.0.0-alpha.32",
49
+ "@arcjet/rollup-config": "1.0.0-alpha.32",
50
+ "@arcjet/tsconfig": "1.0.0-alpha.32",
51
+ "@rollup/wasm-node": "4.27.4",
52
+ "@sveltejs/kit": "^2.8.4",
54
53
  "@types/node": "18.18.0",
55
- "typescript": "5.6.3"
54
+ "typescript": "5.7.2"
56
55
  },
57
56
  "publishConfig": {
58
57
  "access": "public",
package/index.ts DELETED
@@ -1,119 +0,0 @@
1
- import nosecone, {
2
- CONTENT_SECURITY_POLICY_DIRECTIVES,
3
- QUOTED,
4
- defaults as baseDefaults,
5
- NoseconeValidationError,
6
- } from "nosecone";
7
- import type { CspDirectives, NoseconeOptions } from "nosecone";
8
- import type { Handle, KitConfig } from "@sveltejs/kit";
9
-
10
- export const defaults = {
11
- ...baseDefaults,
12
- directives: {
13
- ...baseDefaults.contentSecurityPolicy.directives,
14
- scriptSrc: ["'strict-dynamic'"],
15
- },
16
- } as const;
17
-
18
- // We export `nosecone` as the default so it can be used with `new Response()`
19
- export default nosecone;
20
-
21
- /**
22
- * Create a SvelteKit hook that sets secure headers on every request.
23
- *
24
- * @param options: Configuration to provide to Nosecone
25
- * @returns A SvelteKit hook that sets secure headers
26
- */
27
- export function createHook(options: NoseconeOptions = defaults): Handle {
28
- return async ({ event, resolve }) => {
29
- const response = await resolve(event);
30
-
31
- const headers = nosecone(options);
32
- for (const [headerName, headerValue] of Object.entries(headers)) {
33
- // Only add headers that aren't already set. For example, SvelteKit will
34
- // likely have added `Content-Security-Policy` if configured with `csp`
35
- if (!response.headers.has(headerName)) {
36
- response.headers.set(headerName, headerValue);
37
- }
38
- }
39
-
40
- return response;
41
- };
42
- }
43
-
44
- type SvelteKitCsp = Exclude<KitConfig["csp"], undefined>;
45
-
46
- export type ContentSecurityPolicyConfig = {
47
- mode?: SvelteKitCsp["mode"];
48
- directives?: CspDirectives;
49
- // TODO: Support `reportOnly`
50
- };
51
-
52
- function unquote(value?: string) {
53
- for (const [unquoted, quoted] of QUOTED) {
54
- if (value === quoted) {
55
- return unquoted;
56
- }
57
- }
58
-
59
- return value;
60
- }
61
-
62
- function resolveValue(v: (() => string) | string) {
63
- if (typeof v === "function") {
64
- return v();
65
- } else {
66
- return v;
67
- }
68
- }
69
-
70
- function directivesToSvelteKitConfig(
71
- directives: Readonly<CspDirectives>,
72
- ): SvelteKitCsp["directives"] {
73
- const sveltekitDirectives: SvelteKitCsp["directives"] = {};
74
- for (const [optionKey, optionValues] of Object.entries(directives)) {
75
- const key = CONTENT_SECURITY_POLICY_DIRECTIVES.get(
76
- // @ts-expect-error because we're validating this option key
77
- optionKey,
78
- );
79
- if (!key) {
80
- throw new NoseconeValidationError(
81
- `${optionKey} is not a Content-Security-Policy directive`,
82
- );
83
- }
84
-
85
- // Skip anything falsey
86
- if (!optionValues) {
87
- continue;
88
- }
89
-
90
- // TODO: What do we want to do if array is empty? I think they work differently for some directives
91
- const resolvedValues = Array.isArray(optionValues)
92
- ? new Set(optionValues.map(resolveValue))
93
- : new Set<string>();
94
-
95
- // TODO: Add validations for SvelteKit CSP directives
96
-
97
- const values = Array.from(resolvedValues);
98
-
99
- if (key === "upgrade-insecure-requests") {
100
- sveltekitDirectives[key] = true;
101
- } else {
102
- // @ts-ignore because we're mapping to SvelteKit options
103
- sveltekitDirectives[key] = values.map(unquote);
104
- }
105
- }
106
-
107
- return sveltekitDirectives;
108
- }
109
-
110
- export function csp(
111
- options: ContentSecurityPolicyConfig = { mode: "auto" },
112
- ): SvelteKitCsp {
113
- return {
114
- mode: options.mode ? options.mode : "auto",
115
- directives: directivesToSvelteKitConfig(
116
- options.directives ?? defaults.contentSecurityPolicy.directives,
117
- ),
118
- };
119
- }