@farm.js/integration-utils 0.1.61 → 0.1.62
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.
- package/dist/cookies.d.ts.map +1 -1
- package/dist/cookies.js +30 -6
- package/dist/dev-secret.d.ts +15 -0
- package/dist/dev-secret.d.ts.map +1 -0
- package/dist/dev-secret.js +85 -0
- package/package.json +2 -2
package/dist/cookies.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.d.ts","sourceRoot":"","sources":["../src/cookies.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,IAAI,CAAC;CAChB;AAED,8EAA8E;AAC9E,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CASlF;AAED,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,YAAY,EAAE,CAE3E;AAED,iFAAiF;AACjF,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK5E;AAED,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,oBAAyB,GACjC,MAAM,
|
|
1
|
+
{"version":3,"file":"cookies.d.ts","sourceRoot":"","sources":["../src/cookies.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,IAAI,CAAC;CAChB;AAED,8EAA8E;AAC9E,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CASlF;AAED,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,YAAY,EAAE,CAE3E;AAED,iFAAiF;AACjF,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK5E;AAED,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,oBAAyB,GACjC,MAAM,CA0CR;AAED,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,IAAI,CAAC,oBAAoB,EAAE,QAAQ,CAAM,GACjD,MAAM,CAKR"}
|
package/dist/cookies.js
CHANGED
|
@@ -18,14 +18,27 @@ export function getCookieValue(headers, name) {
|
|
|
18
18
|
null);
|
|
19
19
|
}
|
|
20
20
|
export function createRequestCookie(name, value, request, options = {}) {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const sameSite = options.sameSite || "Lax";
|
|
22
|
+
// Browsers silently discard a `__Host-` or `__Secure-` cookie without the
|
|
23
|
+
// attributes its name requires, and a SameSite=None cookie without Secure.
|
|
24
|
+
// Both a set and its deletion would then do nothing, so these come from the
|
|
25
|
+
// name and SameSite rather than from the caller.
|
|
26
|
+
const hostPrefixed = name.startsWith("__Host-");
|
|
27
|
+
const secure = hostPrefixed ||
|
|
28
|
+
name.startsWith("__Secure-") ||
|
|
29
|
+
sameSite === "None" ||
|
|
30
|
+
(options.secure ?? isHttpsRequest(request));
|
|
31
|
+
const path = hostPrefixed ? "/" : options.path || "/";
|
|
32
|
+
const domain = hostPrefixed ? undefined : options.domain;
|
|
33
|
+
const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`, `Path=${path}`];
|
|
24
34
|
if (options.maxAge != null) {
|
|
35
|
+
if (!Number.isFinite(options.maxAge)) {
|
|
36
|
+
throw new TypeError(`Cookie "${name}" maxAge must be a finite number of seconds.`);
|
|
37
|
+
}
|
|
25
38
|
parts.push(`Max-Age=${Math.max(0, Math.floor(options.maxAge))}`);
|
|
26
39
|
}
|
|
27
|
-
if (
|
|
28
|
-
parts.push(`Domain=${
|
|
40
|
+
if (domain) {
|
|
41
|
+
parts.push(`Domain=${domain}`);
|
|
29
42
|
}
|
|
30
43
|
if (options.expires) {
|
|
31
44
|
parts.push(`Expires=${options.expires.toUTCString()}`);
|
|
@@ -36,7 +49,7 @@ export function createRequestCookie(name, value, request, options = {}) {
|
|
|
36
49
|
if (secure) {
|
|
37
50
|
parts.push("Secure");
|
|
38
51
|
}
|
|
39
|
-
parts.push(`SameSite=${
|
|
52
|
+
parts.push(`SameSite=${sameSite}`);
|
|
40
53
|
return parts.join("; ");
|
|
41
54
|
}
|
|
42
55
|
export function clearRequestCookie(name, request, options = {}) {
|
|
@@ -45,3 +58,14 @@ export function clearRequestCookie(name, request, options = {}) {
|
|
|
45
58
|
maxAge: 0,
|
|
46
59
|
});
|
|
47
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Behind a TLS-terminating proxy the request URL is `http:`. Trusting
|
|
63
|
+
* `X-Forwarded-Proto: https` here only ever adds `Secure`, and only for the
|
|
64
|
+
* client that sent it, so it is safe without `trustProxy`.
|
|
65
|
+
*/
|
|
66
|
+
function isHttpsRequest(request) {
|
|
67
|
+
if (new URL(request.url).protocol === "https:")
|
|
68
|
+
return true;
|
|
69
|
+
const forwarded = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim();
|
|
70
|
+
return forwarded?.toLowerCase() === "https";
|
|
71
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A random development secret for an integration that signs or seals cookies, in place of a
|
|
3
|
+
* constant fallback that anyone reaching a dev server (a `farm preview` URL, `--host`) could use
|
|
4
|
+
* to forge sessions.
|
|
5
|
+
*
|
|
6
|
+
* Development generates 32 random bytes once per app in `<root>/.farm/<name>-secret`, which is
|
|
7
|
+
* gitignored and denied to the dev file server, and reuses it so sessions survive restarts. Tests,
|
|
8
|
+
* runtimes without a filesystem, and a directory that cannot be written get a random secret for
|
|
9
|
+
* the life of the process. Call it only after deciding the environment is development:
|
|
10
|
+
* production still needs a real one.
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolveDevelopmentSecret(name: string, options?: {
|
|
13
|
+
root?: string;
|
|
14
|
+
}): string;
|
|
15
|
+
//# sourceMappingURL=dev-secret.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dev-secret.d.ts","sourceRoot":"","sources":["../src/dev-secret.ts"],"names":[],"mappings":"AAYA;;;;;;;;;;GAUG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,MAAM,CAqC9F"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// No static node:* imports: auth0 and workos ship to edge targets, and only the development path
|
|
2
|
+
// touches the filesystem. Node loads its builtins on demand there through getBuiltinModule.
|
|
3
|
+
/** Set by Farm while it evaluates an app's farm.config, which can live outside the cwd. */
|
|
4
|
+
const FARM_CONFIG_ROOT = Symbol.for("farm.configRoot");
|
|
5
|
+
/** Secrets for a process that cannot keep one on disk, by file path. */
|
|
6
|
+
const ephemeralSecrets = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* A random development secret for an integration that signs or seals cookies, in place of a
|
|
9
|
+
* constant fallback that anyone reaching a dev server (a `farm preview` URL, `--host`) could use
|
|
10
|
+
* to forge sessions.
|
|
11
|
+
*
|
|
12
|
+
* Development generates 32 random bytes once per app in `<root>/.farm/<name>-secret`, which is
|
|
13
|
+
* gitignored and denied to the dev file server, and reuses it so sessions survive restarts. Tests,
|
|
14
|
+
* runtimes without a filesystem, and a directory that cannot be written get a random secret for
|
|
15
|
+
* the life of the process. Call it only after deciding the environment is development:
|
|
16
|
+
* production still needs a real one.
|
|
17
|
+
*/
|
|
18
|
+
export function resolveDevelopmentSecret(name, options = {}) {
|
|
19
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(name)) {
|
|
20
|
+
throw new TypeError(`Invalid development secret name ${JSON.stringify(name)}.`);
|
|
21
|
+
}
|
|
22
|
+
const fs = loadBuiltin("node:fs");
|
|
23
|
+
const path = loadBuiltin("node:path");
|
|
24
|
+
if (!fs || !path)
|
|
25
|
+
return getEphemeralSecret(name);
|
|
26
|
+
const configRoot = globalThis[FARM_CONFIG_ROOT];
|
|
27
|
+
const root = path.resolve(options.root ?? configRoot ?? process.cwd());
|
|
28
|
+
const file = path.join(root, ".farm", `${name}-secret`);
|
|
29
|
+
if (process.env.NODE_ENV === "test")
|
|
30
|
+
return getEphemeralSecret(file);
|
|
31
|
+
const existing = readSecretFile(fs, file);
|
|
32
|
+
if (existing)
|
|
33
|
+
return existing;
|
|
34
|
+
const generated = createSecret();
|
|
35
|
+
try {
|
|
36
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
37
|
+
// `wx` keeps a secret another process wrote first; that one is read back below.
|
|
38
|
+
fs.writeFileSync(file, `${generated}\n`, { flag: "wx", mode: 0o600 });
|
|
39
|
+
return generated;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error.code !== "EEXIST")
|
|
43
|
+
return getEphemeralSecret(file);
|
|
44
|
+
}
|
|
45
|
+
// Another process won the race, or the file holds no usable secret.
|
|
46
|
+
const raced = readSecretFile(fs, file);
|
|
47
|
+
if (raced)
|
|
48
|
+
return raced;
|
|
49
|
+
try {
|
|
50
|
+
fs.writeFileSync(file, `${generated}\n`, { mode: 0o600 });
|
|
51
|
+
return generated;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return getEphemeralSecret(file);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function loadBuiltin(id) {
|
|
58
|
+
try {
|
|
59
|
+
return globalThis.process?.getBuiltinModule?.(id);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function readSecretFile(fs, file) {
|
|
66
|
+
try {
|
|
67
|
+
const value = fs.readFileSync(file, "utf8").trim();
|
|
68
|
+
return value.length >= 32 ? value : undefined;
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function createSecret() {
|
|
75
|
+
const bytes = crypto.getRandomValues(new Uint8Array(32));
|
|
76
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
77
|
+
}
|
|
78
|
+
function getEphemeralSecret(key) {
|
|
79
|
+
let secret = ephemeralSecrets.get(key);
|
|
80
|
+
if (!secret) {
|
|
81
|
+
secret = createSecret();
|
|
82
|
+
ephemeralSecrets.set(key, secret);
|
|
83
|
+
}
|
|
84
|
+
return secret;
|
|
85
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/integration-utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.62",
|
|
4
4
|
"description": "Shared integration utilities for Farm.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"access": "public"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@farm.js/core": "0.1.
|
|
60
|
+
"@farm.js/core": "0.1.62"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",
|