@otwdesign/db-next 1.4.0 → 1.4.2
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/README.md +6 -5
- package/dist/route.js +17 -34
- package/dist/route.js.map +1 -1
- package/dist/sign-in.d.ts +1 -3
- package/dist/sign-in.js +4 -9
- package/dist/sign-in.js.map +1 -1
- package/package.json +2 -2
- package/src/route.ts +19 -38
- package/src/sign-in.ts +4 -17
package/README.md
CHANGED
|
@@ -40,11 +40,12 @@ Set the same variables on your host (`vercel env add DB_ADMIN_PASSWORD`), and op
|
|
|
40
40
|
|
|
41
41
|
- **The password:** `/db/sign-in` asks for it and sets a session for 12 hours. Changing the
|
|
42
42
|
password ends every session; signing out forgets it in that browser. One under 16 characters
|
|
43
|
-
is refused.
|
|
44
|
-
|
|
45
|
-
connections it
|
|
46
|
-
|
|
47
|
-
password the slow way, so a cookie forged from a guess costs its maker
|
|
43
|
+
is refused. A wrong password waits half a second for its answer, and nothing is counted: nobody
|
|
44
|
+
can keep you out by sending wrong ones, and a guesser gets as many tries as it opens
|
|
45
|
+
connections. What stands against it is the password, so make it long and random, and put a
|
|
46
|
+
rate limit in front of `/db/sign-in` where your host has one. The session's key is made from
|
|
47
|
+
the password the slow way, so a cookie forged from a guess costs its maker more than a
|
|
48
|
+
sign-in does.
|
|
48
49
|
- **The app's own users:** those listed in `DB_ADMIN_USERS`, by email (or id), get in without the
|
|
49
50
|
password once they're signed in to the app. The audit log has who it was. With the list set and
|
|
50
51
|
no password, nobody else gets in.
|
package/dist/route.js
CHANGED
|
@@ -14,13 +14,12 @@ const postgres = {
|
|
|
14
14
|
modules: () => Promise.all([import('pg'), import('pg-cursor')]),
|
|
15
15
|
};
|
|
16
16
|
/**
|
|
17
|
-
* How long a wrong password waits for its answer
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
17
|
+
* How long a wrong password waits for its answer. Each waits by itself, and nothing is counted:
|
|
18
|
+
* a count of those waiting is one that anyone can fill, and while it was full the owner's right
|
|
19
|
+
* password was turned away with the rest. So a guesser gets as many tries as it opens
|
|
20
|
+
* connections, and what stands against it is the password's length.
|
|
21
21
|
*/
|
|
22
22
|
const WRONG_PASSWORD_MS = 500;
|
|
23
|
-
const WRONG_PASSWORDS_WAITING = 8;
|
|
24
23
|
/** How long a sign-in's form has to arrive, and how much of one is read. */
|
|
25
24
|
const FORM_MS = 2_000;
|
|
26
25
|
const FORM_BYTES = 4_096;
|
|
@@ -70,9 +69,6 @@ export function createDbRoute(options = {}) {
|
|
|
70
69
|
connections: connections ?? appConnections(env()),
|
|
71
70
|
authorize: appAuthorize ?? authorize,
|
|
72
71
|
}));
|
|
73
|
-
/** The sign-ins being looked at or waiting for their answer, and the wait of the last wrong one. */
|
|
74
|
-
let waiting = 0;
|
|
75
|
-
let lastRefusal = Promise.resolve();
|
|
76
72
|
async function signIn(request) {
|
|
77
73
|
const set = password();
|
|
78
74
|
const state = {
|
|
@@ -90,30 +86,17 @@ export function createDbRoute(options = {}) {
|
|
|
90
86
|
}
|
|
91
87
|
if (!sameOrigin(request))
|
|
92
88
|
return new Response('Sign in from this site.', { status: 403 });
|
|
93
|
-
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
// nothing waits, and its place is free again.
|
|
103
|
-
if (given === null)
|
|
104
|
-
return signInPage(state, 400);
|
|
105
|
-
if (!usable(set) || !samePassword(given, set)) {
|
|
106
|
-
const turn = lastRefusal.then(() => new Promise((resolve) => setTimeout(resolve, WRONG_PASSWORD_MS)));
|
|
107
|
-
lastRefusal = turn;
|
|
108
|
-
await turn;
|
|
109
|
-
console.warn(`otw-db: a sign-in at ${basePath} was refused`);
|
|
110
|
-
return signInPage({ ...state, wrong: usable(set) }, 401);
|
|
111
|
-
}
|
|
112
|
-
return redirect(basePath || '/', sessionCookie(await signSession(set, basePath), { basePath, secure: secure(request) }));
|
|
113
|
-
}
|
|
114
|
-
finally {
|
|
115
|
-
waiting -= 1;
|
|
89
|
+
const given = await passwordOf(request);
|
|
90
|
+
// No password came (a form that never arrived, or had none): nothing was guessed, so
|
|
91
|
+
// nothing waits.
|
|
92
|
+
if (given === null)
|
|
93
|
+
return signInPage(state, 400);
|
|
94
|
+
if (!usable(set) || !samePassword(given, set)) {
|
|
95
|
+
await new Promise((resolve) => setTimeout(resolve, WRONG_PASSWORD_MS));
|
|
96
|
+
console.warn(`otw-db: a sign-in at ${basePath} was refused`);
|
|
97
|
+
return signInPage({ ...state, wrong: usable(set) }, 401);
|
|
116
98
|
}
|
|
99
|
+
return redirect(basePath || '/', sessionCookie(await signSession(set, basePath), { basePath, secure: secure(request) }));
|
|
117
100
|
}
|
|
118
101
|
async function handle(request) {
|
|
119
102
|
const url = new URL(request.url);
|
|
@@ -142,9 +125,9 @@ export function createDbRoute(options = {}) {
|
|
|
142
125
|
}
|
|
143
126
|
/** The password a sign-in form posted, or null where there is none to read. */
|
|
144
127
|
async function passwordOf(request) {
|
|
145
|
-
// A form that never finishes arriving would
|
|
146
|
-
//
|
|
147
|
-
//
|
|
128
|
+
// A form that never finishes arriving would be waited on for as long as it liked; one of
|
|
129
|
+
// megabytes would be read whole and hashed. Two seconds, and a few kilobytes: a password and
|
|
130
|
+
// nothing else.
|
|
148
131
|
if (Number(request.headers.get('content-length') ?? 0) > FORM_BYTES)
|
|
149
132
|
return null;
|
|
150
133
|
const reader = request.body?.getReader();
|
package/dist/route.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.js","sourceRoot":"","sources":["../src/route.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,UAAU,GAMX,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EACL,QAAQ,EACR,YAAY,EACZ,cAAc,EACd,aAAa,EACb,WAAW,EACX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CAAC,gBAAgB,CAAC,CAAA;IAClD,IAAI,IAAI,IAAI,IAAI,KAAK,aAAa,IAAI,IAAI,KAAK,MAAM;QAAE,OAAO,KAAK,CAAA;IACnE,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;IAC5C,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IACxB,IAAI,IAAY,CAAA;IAChB,IAAI,CAAC;QACH,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAA;IAC7B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAA;IACd,CAAC;IACD,OAAO;QACL,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE;QAC9D,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC;QAC3B,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI;KAC1B,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;AAClB,CAAC"}
|
package/dist/sign-in.d.ts
CHANGED
|
@@ -9,11 +9,9 @@ export interface SignInState {
|
|
|
9
9
|
expired?: boolean;
|
|
10
10
|
/** The password just tried was wrong. */
|
|
11
11
|
wrong?: boolean;
|
|
12
|
-
/** Too many wrong passwords are waiting for their answer: this one wasn't looked at. */
|
|
13
|
-
busy?: boolean;
|
|
14
12
|
}
|
|
15
13
|
/** The page that asks for the password, or says why there is none to ask for. */
|
|
16
|
-
export declare function signInPage(state: SignInState, status?: number
|
|
14
|
+
export declare function signInPage(state: SignInState, status?: number): Response;
|
|
17
15
|
/** The page for someone already in: on to the database, or out again. */
|
|
18
16
|
export declare function signedInPage(basePath: string, who: string | null): Response;
|
|
19
17
|
/** Sends the browser on, with a cookie set or cleared on the way. */
|
package/dist/sign-in.js
CHANGED
|
@@ -37,7 +37,7 @@ const escape = (text) => text
|
|
|
37
37
|
.replaceAll('>', '>')
|
|
38
38
|
.replaceAll('"', '"')
|
|
39
39
|
.replaceAll("'", ''');
|
|
40
|
-
function page(status, body
|
|
40
|
+
function page(status, body) {
|
|
41
41
|
return new Response(`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="robots" content="noindex"><title>Sign in · OTW DB</title><style>${STYLE}</style></head><body><main>${body}</main></body></html>`, {
|
|
42
42
|
status,
|
|
43
43
|
headers: {
|
|
@@ -46,20 +46,15 @@ function page(status, body, headers = {}) {
|
|
|
46
46
|
'content-security-policy': CSP,
|
|
47
47
|
'x-content-type-options': 'nosniff',
|
|
48
48
|
'referrer-policy': 'same-origin',
|
|
49
|
-
...headers,
|
|
50
49
|
},
|
|
51
50
|
});
|
|
52
51
|
}
|
|
53
52
|
/** The page that asks for the password, or says why there is none to ask for. */
|
|
54
|
-
export function signInPage(state, status = 200
|
|
53
|
+
export function signInPage(state, status = 200) {
|
|
55
54
|
const action = escape(`${state.basePath}/sign-in`);
|
|
56
55
|
const parts = ['<h1>Sign in to the database</h1>'];
|
|
57
|
-
if (state.
|
|
58
|
-
parts.push('<p class="notice" role="alert">Too many wrong passwords have just been tried here. Wait a moment, then try again.</p>');
|
|
59
|
-
}
|
|
60
|
-
else if (state.wrong) {
|
|
56
|
+
if (state.wrong)
|
|
61
57
|
parts.push('<p class="notice" role="alert">That isn’t the password.</p>');
|
|
62
|
-
}
|
|
63
58
|
if (state.password === 'ready') {
|
|
64
59
|
parts.push(`<form method="post" action="${action}"><label for="password">Password</label><input id="password" name="password" type="password" autocomplete="current-password" required autofocus><button type="submit">Sign in</button></form>`);
|
|
65
60
|
}
|
|
@@ -73,7 +68,7 @@ export function signInPage(state, status = 200, headers = {}) {
|
|
|
73
68
|
? '<p class="notice" role="alert">Your sign-in to the app has run out. Open the app, then come back to this page.</p>'
|
|
74
69
|
: `<p class="muted">${state.password === 'ready' ? 'Or sign' : 'Sign'} in to the app as one of the users it lets in here, then open this page again: they need no password.</p>`);
|
|
75
70
|
}
|
|
76
|
-
return page(status, parts.join('')
|
|
71
|
+
return page(status, parts.join(''));
|
|
77
72
|
}
|
|
78
73
|
/** The page for someone already in: on to the database, or out again. */
|
|
79
74
|
export function signedInPage(basePath, who) {
|
package/dist/sign-in.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sign-in.js","sourceRoot":"","sources":["../src/sign-in.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C;;;;GAIG;AAEH,MAAM,GAAG,GAAG;IACV,oBAAoB;IACpB,2BAA2B;IAC3B,oBAAoB;IACpB,wBAAwB;IACxB,iBAAiB;CAClB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AAEZ,MAAM,KAAK,GAAG;;;;;;;;;;;;;;;;;;;CAmBb,CAAA;AAED,MAAM,MAAM,GAAG,CAAC,IAAY,EAAE,EAAE,CAC9B,IAAI;KACD,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC;KACxB,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC;KACvB,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC;KACvB,UAAU,CAAC,GAAG,EAAE,QAAQ,CAAC;KACzB,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;AAE7B,SAAS,IAAI,CAAC,MAAc,EAAE,IAAY
|
|
1
|
+
{"version":3,"file":"sign-in.js","sourceRoot":"","sources":["../src/sign-in.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C;;;;GAIG;AAEH,MAAM,GAAG,GAAG;IACV,oBAAoB;IACpB,2BAA2B;IAC3B,oBAAoB;IACpB,wBAAwB;IACxB,iBAAiB;CAClB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AAEZ,MAAM,KAAK,GAAG;;;;;;;;;;;;;;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a password sign-in or your app's own users, and the drivers Vercel needs. npx @otwdesign/db-next init sets it up.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "OTW SRL",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"pg": "^8.23.1",
|
|
54
54
|
"pg-cursor": "^2.22.1",
|
|
55
|
-
"@otwdesign/db-core": "1.4.
|
|
55
|
+
"@otwdesign/db-core": "1.4.2"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@supabase/ssr": "^0.12.7",
|
package/src/route.ts
CHANGED
|
@@ -73,13 +73,12 @@ const postgres: PostgresOptions = {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
|
-
* How long a wrong password waits for its answer
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
76
|
+
* How long a wrong password waits for its answer. Each waits by itself, and nothing is counted:
|
|
77
|
+
* a count of those waiting is one that anyone can fill, and while it was full the owner's right
|
|
78
|
+
* password was turned away with the rest. So a guesser gets as many tries as it opens
|
|
79
|
+
* connections, and what stands against it is the password's length.
|
|
80
80
|
*/
|
|
81
81
|
const WRONG_PASSWORD_MS = 500
|
|
82
|
-
const WRONG_PASSWORDS_WAITING = 8
|
|
83
82
|
/** How long a sign-in's form has to arrive, and how much of one is read. */
|
|
84
83
|
const FORM_MS = 2_000
|
|
85
84
|
const FORM_BYTES = 4_096
|
|
@@ -141,10 +140,6 @@ export function createDbRoute(options: DbRouteOptions = {}): DbRoute {
|
|
|
141
140
|
authorize: appAuthorize ?? authorize,
|
|
142
141
|
}))
|
|
143
142
|
|
|
144
|
-
/** The sign-ins being looked at or waiting for their answer, and the wait of the last wrong one. */
|
|
145
|
-
let waiting = 0
|
|
146
|
-
let lastRefusal: Promise<void> = Promise.resolve()
|
|
147
|
-
|
|
148
143
|
async function signIn(request: Request): Promise<Response> {
|
|
149
144
|
const set = password()
|
|
150
145
|
const state = {
|
|
@@ -159,33 +154,19 @@ export function createDbRoute(options: DbRouteOptions = {}): DbRoute {
|
|
|
159
154
|
return signInPage({ ...state, ...(expired ? { expired } : {}) })
|
|
160
155
|
}
|
|
161
156
|
if (!sameOrigin(request)) return new Response('Sign in from this site.', { status: 403 })
|
|
162
|
-
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// No password came (a form that never arrived, or had none): nothing was guessed, so
|
|
171
|
-
// nothing waits, and its place is free again.
|
|
172
|
-
if (given === null) return signInPage(state, 400)
|
|
173
|
-
if (!usable(set) || !samePassword(given, set)) {
|
|
174
|
-
const turn = lastRefusal.then(
|
|
175
|
-
() => new Promise<void>((resolve) => setTimeout(resolve, WRONG_PASSWORD_MS)),
|
|
176
|
-
)
|
|
177
|
-
lastRefusal = turn
|
|
178
|
-
await turn
|
|
179
|
-
console.warn(`otw-db: a sign-in at ${basePath} was refused`)
|
|
180
|
-
return signInPage({ ...state, wrong: usable(set) }, 401)
|
|
181
|
-
}
|
|
182
|
-
return redirect(
|
|
183
|
-
basePath || '/',
|
|
184
|
-
sessionCookie(await signSession(set, basePath), { basePath, secure: secure(request) }),
|
|
185
|
-
)
|
|
186
|
-
} finally {
|
|
187
|
-
waiting -= 1
|
|
157
|
+
const given = await passwordOf(request)
|
|
158
|
+
// No password came (a form that never arrived, or had none): nothing was guessed, so
|
|
159
|
+
// nothing waits.
|
|
160
|
+
if (given === null) return signInPage(state, 400)
|
|
161
|
+
if (!usable(set) || !samePassword(given, set)) {
|
|
162
|
+
await new Promise((resolve) => setTimeout(resolve, WRONG_PASSWORD_MS))
|
|
163
|
+
console.warn(`otw-db: a sign-in at ${basePath} was refused`)
|
|
164
|
+
return signInPage({ ...state, wrong: usable(set) }, 401)
|
|
188
165
|
}
|
|
166
|
+
return redirect(
|
|
167
|
+
basePath || '/',
|
|
168
|
+
sessionCookie(await signSession(set, basePath), { basePath, secure: secure(request) }),
|
|
169
|
+
)
|
|
189
170
|
}
|
|
190
171
|
|
|
191
172
|
async function handle(request: Request): Promise<Response> {
|
|
@@ -217,9 +198,9 @@ export function createDbRoute(options: DbRouteOptions = {}): DbRoute {
|
|
|
217
198
|
|
|
218
199
|
/** The password a sign-in form posted, or null where there is none to read. */
|
|
219
200
|
async function passwordOf(request: Request): Promise<string | null> {
|
|
220
|
-
// A form that never finishes arriving would
|
|
221
|
-
//
|
|
222
|
-
//
|
|
201
|
+
// A form that never finishes arriving would be waited on for as long as it liked; one of
|
|
202
|
+
// megabytes would be read whole and hashed. Two seconds, and a few kilobytes: a password and
|
|
203
|
+
// nothing else.
|
|
223
204
|
if (Number(request.headers.get('content-length') ?? 0) > FORM_BYTES) return null
|
|
224
205
|
const reader = request.body?.getReader()
|
|
225
206
|
if (!reader) return null
|
package/src/sign-in.ts
CHANGED
|
@@ -43,7 +43,7 @@ const escape = (text: string) =>
|
|
|
43
43
|
.replaceAll('"', '"')
|
|
44
44
|
.replaceAll("'", ''')
|
|
45
45
|
|
|
46
|
-
function page(status: number, body: string
|
|
46
|
+
function page(status: number, body: string): Response {
|
|
47
47
|
return new Response(
|
|
48
48
|
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="robots" content="noindex"><title>Sign in · OTW DB</title><style>${STYLE}</style></head><body><main>${body}</main></body></html>`,
|
|
49
49
|
{
|
|
@@ -54,7 +54,6 @@ function page(status: number, body: string, headers: Record<string, string> = {}
|
|
|
54
54
|
'content-security-policy': CSP,
|
|
55
55
|
'x-content-type-options': 'nosniff',
|
|
56
56
|
'referrer-policy': 'same-origin',
|
|
57
|
-
...headers,
|
|
58
57
|
},
|
|
59
58
|
},
|
|
60
59
|
)
|
|
@@ -71,25 +70,13 @@ export interface SignInState {
|
|
|
71
70
|
expired?: boolean
|
|
72
71
|
/** The password just tried was wrong. */
|
|
73
72
|
wrong?: boolean
|
|
74
|
-
/** Too many wrong passwords are waiting for their answer: this one wasn't looked at. */
|
|
75
|
-
busy?: boolean
|
|
76
73
|
}
|
|
77
74
|
|
|
78
75
|
/** The page that asks for the password, or says why there is none to ask for. */
|
|
79
|
-
export function signInPage(
|
|
80
|
-
state: SignInState,
|
|
81
|
-
status = 200,
|
|
82
|
-
headers: Record<string, string> = {},
|
|
83
|
-
): Response {
|
|
76
|
+
export function signInPage(state: SignInState, status = 200): Response {
|
|
84
77
|
const action = escape(`${state.basePath}/sign-in`)
|
|
85
78
|
const parts = ['<h1>Sign in to the database</h1>']
|
|
86
|
-
if (state.
|
|
87
|
-
parts.push(
|
|
88
|
-
'<p class="notice" role="alert">Too many wrong passwords have just been tried here. Wait a moment, then try again.</p>',
|
|
89
|
-
)
|
|
90
|
-
} else if (state.wrong) {
|
|
91
|
-
parts.push('<p class="notice" role="alert">That isn’t the password.</p>')
|
|
92
|
-
}
|
|
79
|
+
if (state.wrong) parts.push('<p class="notice" role="alert">That isn’t the password.</p>')
|
|
93
80
|
if (state.password === 'ready') {
|
|
94
81
|
parts.push(
|
|
95
82
|
`<form method="post" action="${action}"><label for="password">Password</label><input id="password" name="password" type="password" autocomplete="current-password" required autofocus><button type="submit">Sign in</button></form>`,
|
|
@@ -108,7 +95,7 @@ export function signInPage(
|
|
|
108
95
|
: `<p class="muted">${state.password === 'ready' ? 'Or sign' : 'Sign'} in to the app as one of the users it lets in here, then open this page again: they need no password.</p>`,
|
|
109
96
|
)
|
|
110
97
|
}
|
|
111
|
-
return page(status, parts.join('')
|
|
98
|
+
return page(status, parts.join(''))
|
|
112
99
|
}
|
|
113
100
|
|
|
114
101
|
/** The page for someone already in: on to the database, or out again. */
|