acebase-server 1.16.2 → 1.17.0
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 +180 -10
- package/build-vars.js +2 -1
- package/dist/cjs/auth.js +88 -88
- package/dist/cjs/cluster.js +56 -56
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +24 -24
- package/dist/cjs/logger.js +73 -73
- package/dist/cjs/middleware/404.js +14 -14
- package/dist/cjs/middleware/admin-only.js +28 -28
- package/dist/cjs/middleware/cache.js +14 -14
- package/dist/cjs/middleware/connection.js +49 -49
- package/dist/cjs/middleware/context.js +20 -20
- package/dist/cjs/middleware/cors.js +50 -50
- package/dist/cjs/middleware/swagger.js +17 -17
- package/dist/cjs/middleware/user.js +57 -57
- package/dist/cjs/oauth-providers/dropbox.js +109 -109
- package/dist/cjs/oauth-providers/facebook.js +85 -85
- package/dist/cjs/oauth-providers/google.js +119 -119
- package/dist/cjs/oauth-providers/index.js +26 -26
- package/dist/cjs/oauth-providers/instagram.js +80 -80
- package/dist/cjs/oauth-providers/oauth-provider.js +20 -20
- package/dist/cjs/oauth-providers/spotify.js +113 -113
- package/dist/cjs/routes/auth-change-password.js +87 -87
- package/dist/cjs/routes/auth-delete.js +52 -52
- package/dist/cjs/routes/auth-forgot-password.js +77 -77
- package/dist/cjs/routes/auth-reset-password.js +97 -97
- package/dist/cjs/routes/auth-signin.js +46 -46
- package/dist/cjs/routes/auth-signout.js +61 -61
- package/dist/cjs/routes/auth-signup.js +142 -142
- package/dist/cjs/routes/auth-state.js +25 -25
- package/dist/cjs/routes/auth-update.js +131 -131
- package/dist/cjs/routes/auth-verify-email.js +71 -71
- package/dist/cjs/routes/auth.js +57 -54
- package/dist/cjs/routes/data-exists.js +33 -33
- package/dist/cjs/routes/data-export.js +48 -48
- package/dist/cjs/routes/data-get.js +70 -66
- package/dist/cjs/routes/data-import.js +54 -54
- package/dist/cjs/routes/data-index-create.js +42 -42
- package/dist/cjs/routes/data-index-delete.js +33 -33
- package/dist/cjs/routes/data-index-list.js +31 -31
- package/dist/cjs/routes/data-query.js +80 -70
- package/dist/cjs/routes/data-reflect.js +97 -86
- package/dist/cjs/routes/data-schema-get.js +36 -36
- package/dist/cjs/routes/data-schema-set.js +30 -30
- package/dist/cjs/routes/data-schema-test.js +43 -43
- package/dist/cjs/routes/data-schemas-list.js +32 -32
- package/dist/cjs/routes/data-set.js +85 -75
- package/dist/cjs/routes/data-sync-changes.js +81 -67
- package/dist/cjs/routes/data-sync-mutations.js +81 -67
- package/dist/cjs/routes/data-transaction.js +159 -139
- package/dist/cjs/routes/data-update.js +86 -75
- package/dist/cjs/routes/data.js +62 -62
- package/dist/cjs/routes/docs.js +258 -251
- package/dist/cjs/routes/meta-info.js +63 -63
- package/dist/cjs/routes/meta-logs.js +49 -49
- package/dist/cjs/routes/meta-ping.js +11 -11
- package/dist/cjs/routes/meta-stats.js +27 -27
- package/dist/cjs/routes/meta.js +19 -19
- package/dist/cjs/routes/oauth2-init.js +54 -54
- package/dist/cjs/routes/oauth2-refresh.js +42 -42
- package/dist/cjs/routes/oauth2-signin.js +265 -265
- package/dist/cjs/routes/webmanager.js +26 -26
- package/dist/cjs/rules.js +380 -167
- package/dist/cjs/sandbox.js +28 -0
- package/dist/cjs/schema/user.js +24 -24
- package/dist/cjs/server.js +415 -407
- package/dist/cjs/settings/email.js +2 -2
- package/dist/cjs/settings/index.js +175 -172
- package/dist/cjs/shared/clients.js +26 -26
- package/dist/cjs/shared/email.js +2 -2
- package/dist/cjs/shared/env.js +2 -2
- package/dist/cjs/shared/error.js +33 -33
- package/dist/cjs/shared/http.js +28 -28
- package/dist/cjs/shared/password.js +45 -45
- package/dist/cjs/shared/rootpath.js +16 -16
- package/dist/cjs/shared/signin.js +143 -143
- package/dist/cjs/shared/simple-fetch.js +76 -76
- package/dist/cjs/shared/tokens.js +79 -79
- package/dist/cjs/shared/validate.js +61 -61
- package/dist/cjs/start.js +53 -53
- package/dist/cjs/test.js +10 -10
- package/dist/cjs/websocket/index.js +331 -319
- package/dist/cjs/websocket/manager.js +15 -15
- package/dist/cjs/websocket/socket.io.js +76 -76
- package/dist/esm/auth.js +75 -75
- package/dist/esm/cluster.js +54 -54
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +4 -4
- package/dist/esm/logger.js +51 -51
- package/dist/esm/middleware/404.js +10 -10
- package/dist/esm/middleware/admin-only.js +24 -24
- package/dist/esm/middleware/cache.js +10 -10
- package/dist/esm/middleware/connection.js +45 -45
- package/dist/esm/middleware/context.js +16 -16
- package/dist/esm/middleware/cors.js +44 -44
- package/dist/esm/middleware/swagger.js +13 -13
- package/dist/esm/middleware/user.js +44 -44
- package/dist/esm/oauth-providers/dropbox.js +90 -90
- package/dist/esm/oauth-providers/facebook.js +66 -66
- package/dist/esm/oauth-providers/google.js +96 -96
- package/dist/esm/oauth-providers/index.js +18 -18
- package/dist/esm/oauth-providers/instagram.js +61 -61
- package/dist/esm/oauth-providers/oauth-provider.js +16 -16
- package/dist/esm/oauth-providers/spotify.js +92 -92
- package/dist/esm/routes/auth-change-password.js +72 -72
- package/dist/esm/routes/auth-delete.js +37 -37
- package/dist/esm/routes/auth-forgot-password.js +62 -62
- package/dist/esm/routes/auth-reset-password.js +81 -81
- package/dist/esm/routes/auth-signin.js +33 -33
- package/dist/esm/routes/auth-signout.js +47 -47
- package/dist/esm/routes/auth-signup.js +127 -127
- package/dist/esm/routes/auth-state.js +12 -12
- package/dist/esm/routes/auth-update.js +116 -116
- package/dist/esm/routes/auth-verify-email.js +57 -57
- package/dist/esm/routes/auth.js +53 -50
- package/dist/esm/routes/data-exists.js +20 -20
- package/dist/esm/routes/data-export.js +35 -35
- package/dist/esm/routes/data-get.js +56 -52
- package/dist/esm/routes/data-import.js +41 -41
- package/dist/esm/routes/data-index-create.js +28 -28
- package/dist/esm/routes/data-index-delete.js +20 -20
- package/dist/esm/routes/data-index-list.js +18 -18
- package/dist/esm/routes/data-query.js +66 -56
- package/dist/esm/routes/data-reflect.js +83 -72
- package/dist/esm/routes/data-schema-get.js +23 -23
- package/dist/esm/routes/data-schema-set.js +17 -17
- package/dist/esm/routes/data-schema-test.js +29 -29
- package/dist/esm/routes/data-schemas-list.js +19 -19
- package/dist/esm/routes/data-set.js +70 -60
- package/dist/esm/routes/data-sync-changes.js +67 -53
- package/dist/esm/routes/data-sync-mutations.js +67 -53
- package/dist/esm/routes/data-transaction.js +142 -123
- package/dist/esm/routes/data-update.js +71 -60
- package/dist/esm/routes/data.js +58 -58
- package/dist/esm/routes/docs.js +252 -245
- package/dist/esm/routes/meta-info.js +59 -59
- package/dist/esm/routes/meta-logs.js +35 -35
- package/dist/esm/routes/meta-ping.js +7 -7
- package/dist/esm/routes/meta-stats.js +14 -14
- package/dist/esm/routes/meta.js +15 -15
- package/dist/esm/routes/oauth2-init.js +41 -41
- package/dist/esm/routes/oauth2-refresh.js +29 -29
- package/dist/esm/routes/oauth2-signin.js +251 -251
- package/dist/esm/routes/webmanager.js +22 -22
- package/dist/esm/rules.js +374 -163
- package/dist/esm/sandbox.js +12 -0
- package/dist/esm/schema/user.js +19 -19
- package/dist/esm/server.js +395 -387
- package/dist/esm/settings/email.js +1 -1
- package/dist/esm/settings/index.js +168 -165
- package/dist/esm/shared/clients.js +22 -22
- package/dist/esm/shared/email.js +1 -1
- package/dist/esm/shared/env.js +1 -1
- package/dist/esm/shared/error.js +25 -25
- package/dist/esm/shared/http.js +22 -22
- package/dist/esm/shared/password.js +38 -38
- package/dist/esm/shared/rootpath.js +13 -13
- package/dist/esm/shared/signin.js +128 -128
- package/dist/esm/shared/simple-fetch.js +59 -59
- package/dist/esm/shared/tokens.js +72 -72
- package/dist/esm/shared/validate.js +41 -41
- package/dist/esm/start.js +50 -50
- package/dist/esm/test.js +8 -8
- package/dist/esm/websocket/index.js +313 -302
- package/dist/esm/websocket/manager.js +11 -11
- package/dist/esm/websocket/socket.io.js +71 -71
- package/dist/types/auth.d.ts +3 -3
- package/dist/types/cluster.d.ts +1 -1
- package/dist/types/index.d.ts +4 -4
- package/dist/types/logger.d.ts +35 -35
- package/dist/types/middleware/404.d.ts +7 -7
- package/dist/types/middleware/admin-only.d.ts +19 -19
- package/dist/types/middleware/cache.d.ts +3 -3
- package/dist/types/middleware/connection.d.ts +7 -7
- package/dist/types/middleware/context.d.ts +3 -3
- package/dist/types/middleware/cors.d.ts +25 -25
- package/dist/types/middleware/swagger.d.ts +3 -3
- package/dist/types/middleware/user.d.ts +2 -2
- package/dist/types/oauth-providers/dropbox.d.ts +52 -52
- package/dist/types/oauth-providers/facebook.d.ts +41 -41
- package/dist/types/oauth-providers/google.d.ts +43 -43
- package/dist/types/oauth-providers/index.d.ts +10 -10
- package/dist/types/oauth-providers/instagram.d.ts +50 -50
- package/dist/types/oauth-providers/oauth-provider.d.ts +74 -74
- package/dist/types/oauth-providers/spotify.d.ts +53 -53
- package/dist/types/routes/auth-change-password.d.ts +20 -20
- package/dist/types/routes/auth-delete.d.ts +16 -16
- package/dist/types/routes/auth-forgot-password.d.ts +16 -16
- package/dist/types/routes/auth-reset-password.d.ts +23 -23
- package/dist/types/routes/auth-signin.d.ts +24 -24
- package/dist/types/routes/auth-signout.d.ts +14 -14
- package/dist/types/routes/auth-signup.d.ts +32 -32
- package/dist/types/routes/auth-state.d.ts +13 -13
- package/dist/types/routes/auth-update.d.ts +33 -33
- package/dist/types/routes/auth-verify-email.d.ts +21 -21
- package/dist/types/routes/auth.d.ts +6 -6
- package/dist/types/routes/data-exists.d.ts +9 -9
- package/dist/types/routes/data-export.d.ts +10 -10
- package/dist/types/routes/data-get.d.ts +14 -14
- package/dist/types/routes/data-import.d.ts +13 -13
- package/dist/types/routes/data-index-create.d.ts +51 -51
- package/dist/types/routes/data-index-delete.d.ts +17 -17
- package/dist/types/routes/data-index-list.d.ts +21 -21
- package/dist/types/routes/data-query.d.ts +49 -49
- package/dist/types/routes/data-reflect.d.ts +29 -29
- package/dist/types/routes/data-schema-get.d.ts +11 -11
- package/dist/types/routes/data-schema-set.d.ts +19 -19
- package/dist/types/routes/data-schema-test.d.ts +19 -19
- package/dist/types/routes/data-schemas-list.d.ts +11 -11
- package/dist/types/routes/data-set.d.ts +27 -27
- package/dist/types/routes/data-sync-changes.d.ts +25 -28
- package/dist/types/routes/data-sync-mutations.d.ts +25 -28
- package/dist/types/routes/data-transaction.d.ts +34 -34
- package/dist/types/routes/data-update.d.ts +27 -27
- package/dist/types/routes/data.d.ts +3 -3
- package/dist/types/routes/docs.d.ts +3 -3
- package/dist/types/routes/meta-info.d.ts +11 -11
- package/dist/types/routes/meta-logs.d.ts +20 -20
- package/dist/types/routes/meta-ping.d.ts +7 -7
- package/dist/types/routes/meta-stats.d.ts +13 -13
- package/dist/types/routes/meta.d.ts +3 -3
- package/dist/types/routes/oauth2-init.d.ts +13 -13
- package/dist/types/routes/oauth2-refresh.d.ts +17 -17
- package/dist/types/routes/oauth2-signin.d.ts +15 -15
- package/dist/types/routes/webmanager.d.ts +3 -3
- package/dist/types/rules.d.ts +73 -34
- package/dist/types/sandbox.d.ts +3 -0
- package/dist/types/schema/user.d.ts +85 -85
- package/dist/types/server.d.ts +117 -111
- package/dist/types/settings/email.d.ts +14 -14
- package/dist/types/settings/index.d.ts +195 -187
- package/dist/types/shared/clients.d.ts +46 -46
- package/dist/types/shared/email.d.ts +34 -34
- package/dist/types/shared/env.d.ts +47 -44
- package/dist/types/shared/error.d.ts +15 -15
- package/dist/types/shared/http.d.ts +22 -21
- package/dist/types/shared/password.d.ts +19 -19
- package/dist/types/shared/rootpath.d.ts +2 -2
- package/dist/types/shared/signin.d.ts +31 -31
- package/dist/types/shared/simple-fetch.d.ts +21 -21
- package/dist/types/shared/tokens.d.ts +24 -24
- package/dist/types/shared/validate.d.ts +22 -22
- package/dist/types/start.d.ts +1 -1
- package/dist/types/test.d.ts +1 -1
- package/dist/types/websocket/index.d.ts +6 -6
- package/dist/types/websocket/manager.d.ts +99 -99
- package/dist/types/websocket/socket.io.d.ts +10 -10
- package/package.json +4 -4
- package/src/routes/data-set.yaml +5 -5
- package/src/routes/data-update.yaml +4 -4
package/dist/esm/rules.js
CHANGED
|
@@ -1,164 +1,375 @@
|
|
|
1
|
-
import { PathInfo } from 'acebase-core';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
//
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
if (
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
let
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
1
|
+
import { PathInfo } from 'acebase-core';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import { executeSandboxed } from './sandbox.js';
|
|
4
|
+
import { AUTH_ACCESS_DEFAULT } from './settings/index.js';
|
|
5
|
+
export class AccessRuleValidationError extends Error {
|
|
6
|
+
constructor(result) {
|
|
7
|
+
super(result.message);
|
|
8
|
+
this.result = result;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class PathBasedRules {
|
|
12
|
+
stop() { throw new Error('not started yet'); }
|
|
13
|
+
constructor(rulesFilePath, defaultAccess, env) {
|
|
14
|
+
// Reads rules from a file and monitors it
|
|
15
|
+
// Check if there is a rules file, load it or generate default
|
|
16
|
+
this.codeRules = [];
|
|
17
|
+
this.db = env.db;
|
|
18
|
+
this.debug = env.debug;
|
|
19
|
+
const readRules = () => {
|
|
20
|
+
try {
|
|
21
|
+
// TODO: store in db itself under __rules__ or in separate rules.db storage file
|
|
22
|
+
const json = fs.readFileSync(rulesFilePath, 'utf-8');
|
|
23
|
+
const obj = JSON.parse(json);
|
|
24
|
+
if (typeof obj !== 'object' || typeof obj.rules !== 'object') {
|
|
25
|
+
throw new Error(`malformed rules object`);
|
|
26
|
+
}
|
|
27
|
+
return obj;
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
env.debug.error(`Failed to read rules from "${rulesFilePath}": ${err.message}`);
|
|
31
|
+
return defaultRules;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const defaultAccessRule = (def => {
|
|
35
|
+
switch (def) {
|
|
36
|
+
case AUTH_ACCESS_DEFAULT.ALLOW_AUTHENTICATED: {
|
|
37
|
+
return 'auth !== null';
|
|
38
|
+
}
|
|
39
|
+
case AUTH_ACCESS_DEFAULT.ALLOW_ALL: {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
case AUTH_ACCESS_DEFAULT.DENY_ALL: {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
default: {
|
|
46
|
+
env.debug.error(`Unknown defaultAccessRule "${def}"`);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
})(defaultAccess);
|
|
51
|
+
const defaultRules = {
|
|
52
|
+
rules: {
|
|
53
|
+
'.read': defaultAccessRule,
|
|
54
|
+
'.write': defaultAccessRule,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
let accessRules = defaultRules;
|
|
58
|
+
if (!fs.existsSync(rulesFilePath)) {
|
|
59
|
+
// Write defaults
|
|
60
|
+
fs.writeFileSync(rulesFilePath, JSON.stringify(defaultRules, null, 4));
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
accessRules = readRules();
|
|
64
|
+
}
|
|
65
|
+
// Convert string rules to functions that can be executed
|
|
66
|
+
const processRules = (path, parent, variables) => {
|
|
67
|
+
Object.keys(parent).forEach(key => {
|
|
68
|
+
const rule = parent[key];
|
|
69
|
+
if (['.read', '.write', '.validate'].includes(key) && typeof rule === 'string') {
|
|
70
|
+
let ruleCode = rule.includes('return ') ? rule : `return ${rule}`;
|
|
71
|
+
// Add `await`s to `value` and `exists` call expressions
|
|
72
|
+
ruleCode = ruleCode.replace(/(value|exists)\(/g, (m, fn) => `await ${fn}(`);
|
|
73
|
+
// Convert to function
|
|
74
|
+
// rule = eval(
|
|
75
|
+
// `(async (env) => {` +
|
|
76
|
+
// ` const { now, path, ${variables.join(', ')}, operation, data, auth, value, exists } = env;` +
|
|
77
|
+
// ` ${ruleCode};` +
|
|
78
|
+
// `})`);
|
|
79
|
+
// rule.getText = () => {
|
|
80
|
+
// return ruleCode;
|
|
81
|
+
// };
|
|
82
|
+
ruleCode = `(async () => {\n${ruleCode}\n})();`;
|
|
83
|
+
return parent[key] = ruleCode;
|
|
84
|
+
}
|
|
85
|
+
else if (key === '.schema') {
|
|
86
|
+
// Add schema
|
|
87
|
+
return env.db.schema.set(path, rule)
|
|
88
|
+
.catch(err => {
|
|
89
|
+
env.debug.error(`Error parsing ${path}/.schema: ${err.message}`);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else if (key.startsWith('$')) {
|
|
93
|
+
variables.push(key);
|
|
94
|
+
}
|
|
95
|
+
if (typeof rule === 'object') {
|
|
96
|
+
processRules(`${path}/${key}`, rule, variables.slice());
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
processRules('', accessRules.rules, []);
|
|
101
|
+
// Watch file for changes. watchFile will poll for changes every (default) 5007ms
|
|
102
|
+
const watchFileListener = () => {
|
|
103
|
+
// Reload access rules from file
|
|
104
|
+
const accessRules = readRules();
|
|
105
|
+
processRules('', accessRules.rules, []);
|
|
106
|
+
this.accessRules = accessRules;
|
|
107
|
+
// Re-add rules added by code
|
|
108
|
+
const codeRules = this.codeRules.splice(0);
|
|
109
|
+
for (const rule of codeRules) {
|
|
110
|
+
this.add(rule.path, rule.type, rule.callback);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
fs.watchFile(rulesFilePath, watchFileListener);
|
|
114
|
+
this.stop = () => {
|
|
115
|
+
fs.unwatchFile(rulesFilePath, watchFileListener);
|
|
116
|
+
};
|
|
117
|
+
process.on('SIGINT', this.stop);
|
|
118
|
+
this.authEnabled = env.authEnabled;
|
|
119
|
+
this.accessRules = accessRules;
|
|
120
|
+
}
|
|
121
|
+
async isOperationAllowed(user, path, operation, data) {
|
|
122
|
+
// Process rules, find out if signed in user is allowed to read/write
|
|
123
|
+
// Defaults to false unless a rule is found that tells us otherwise
|
|
124
|
+
const isPreFlight = typeof data === 'undefined';
|
|
125
|
+
const allow = { allow: true };
|
|
126
|
+
if (!this.authEnabled) {
|
|
127
|
+
// Authentication is disabled, anyone can do anything. Not really a smart thing to do!
|
|
128
|
+
return allow;
|
|
129
|
+
}
|
|
130
|
+
else if (user?.uid === 'admin') {
|
|
131
|
+
// Always allow admin access
|
|
132
|
+
// TODO: implement user.is_admin, so the default admin account can be disabled
|
|
133
|
+
return allow;
|
|
134
|
+
}
|
|
135
|
+
else if (path.startsWith('__')) {
|
|
136
|
+
// NEW: with the auth database now integrated into the main database,
|
|
137
|
+
// deny access to private resources starting with '__' for non-admins
|
|
138
|
+
return { allow: false, code: 'private', message: `Access to private resource "${path}" not allowed` };
|
|
139
|
+
}
|
|
140
|
+
const getFullPath = (path, relativePath) => {
|
|
141
|
+
if (relativePath.startsWith('/')) {
|
|
142
|
+
// Absolute path
|
|
143
|
+
return relativePath;
|
|
144
|
+
}
|
|
145
|
+
else if (!relativePath.startsWith('.')) {
|
|
146
|
+
throw new Error('Path must be either absolute (/) or relative (./ or ../)');
|
|
147
|
+
}
|
|
148
|
+
let targetPathInfo = PathInfo.get(path);
|
|
149
|
+
const trailKeys = PathInfo.getPathKeys(relativePath);
|
|
150
|
+
trailKeys.forEach(key => {
|
|
151
|
+
if (key === '.') { /* no op */ }
|
|
152
|
+
else if (key === '..') {
|
|
153
|
+
targetPathInfo = targetPathInfo.parent;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
targetPathInfo = targetPathInfo.child(key);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
return targetPathInfo.path;
|
|
160
|
+
};
|
|
161
|
+
const env = {
|
|
162
|
+
now: Date.now(),
|
|
163
|
+
auth: user || null,
|
|
164
|
+
operation,
|
|
165
|
+
vars: {},
|
|
166
|
+
context: typeof data?.context === 'object' && data.context !== null ? { ...data.context } : {},
|
|
167
|
+
};
|
|
168
|
+
const pathInfo = PathInfo.get(path);
|
|
169
|
+
const pathKeys = pathInfo.keys.slice();
|
|
170
|
+
let rule = this.accessRules.rules;
|
|
171
|
+
const rulePathKeys = [];
|
|
172
|
+
let currentPath = '';
|
|
173
|
+
let isAllowed = false;
|
|
174
|
+
while (rule) {
|
|
175
|
+
// Check read/write access or validate operation
|
|
176
|
+
const checkRules = [];
|
|
177
|
+
const applyRule = (rule) => {
|
|
178
|
+
if (rule && !checkRules.includes(rule)) {
|
|
179
|
+
checkRules.push(rule);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
if (['get', 'exists', 'query', 'reflect', 'export', 'transact'].includes(operation)) {
|
|
183
|
+
// Operations that require 'read' access
|
|
184
|
+
applyRule(rule['.read']);
|
|
185
|
+
}
|
|
186
|
+
if ('.write' in rule && ['update', 'set', 'delete', 'import', 'transact'].includes(operation)) {
|
|
187
|
+
// Operations that require 'write' access
|
|
188
|
+
applyRule(rule['.write']);
|
|
189
|
+
}
|
|
190
|
+
if (`.${operation}` in rule && !isPreFlight) {
|
|
191
|
+
// If there is a dedicated rule (eg ".update" or ".reflect") for this operation, use it.
|
|
192
|
+
applyRule(rule[`.${operation}`]);
|
|
193
|
+
}
|
|
194
|
+
const rulePath = PathInfo.get(rulePathKeys).path;
|
|
195
|
+
for (const rule of checkRules) {
|
|
196
|
+
if (typeof rule === 'boolean') {
|
|
197
|
+
if (!rule) {
|
|
198
|
+
return { allow: false, code: 'rule', message: `${operation} operation denied to path "${path}" by set rule`, rule, rulePath };
|
|
199
|
+
}
|
|
200
|
+
isAllowed = true; // return allow;
|
|
201
|
+
}
|
|
202
|
+
if (typeof rule === 'string' || typeof rule === 'function') {
|
|
203
|
+
try {
|
|
204
|
+
// Execute rule function
|
|
205
|
+
const ruleEnv = {
|
|
206
|
+
...env,
|
|
207
|
+
exists: async (target) => this.db.ref(getFullPath(currentPath, target)).exists(),
|
|
208
|
+
value: async (target, include) => {
|
|
209
|
+
const snap = await this.db.ref(getFullPath(currentPath, target)).get({ include });
|
|
210
|
+
return snap.val();
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
const result = typeof rule === 'function'
|
|
214
|
+
? await rule(ruleEnv)
|
|
215
|
+
: await executeSandboxed(rule, ruleEnv);
|
|
216
|
+
if (!['cascade', 'deny', 'allow', true, false].includes(result)) {
|
|
217
|
+
this.debug.warn(`rule for path ${rulePath} possibly returns an unintentional value (${JSON.stringify(result)}) which results in outcome "${result ? 'allow' : 'deny'}"`);
|
|
218
|
+
}
|
|
219
|
+
isAllowed = result === 'allow' || result === true;
|
|
220
|
+
if (!isAllowed && result !== 'cascade') {
|
|
221
|
+
return { allow: false, code: 'rule', message: `${operation} operation denied to path "${path}" by set rule`, rule, rulePath };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
catch (err) {
|
|
225
|
+
// If rule execution throws an exception, don't allow. Can happen when rule is "auth.uid === '...'", and auth is null because the user is not signed in
|
|
226
|
+
return { allow: false, code: 'exception', message: `${operation} operation denied to path "${path}" by set rule`, rule, rulePath, details: err };
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (isAllowed) {
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
// Proceed with next key in trail
|
|
234
|
+
if (pathKeys.length === 0) {
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
let nextKey = pathKeys.shift();
|
|
238
|
+
currentPath = PathInfo.get(currentPath).childPath(nextKey);
|
|
239
|
+
// if nextKey is '*' or '$something', rule[nextKey] will be undefined (or match a variable) so there is no
|
|
240
|
+
// need to change things here for usage of wildcard paths in subscriptions
|
|
241
|
+
if (typeof rule[nextKey] === 'undefined') {
|
|
242
|
+
// Check if current rule has a wildcard child
|
|
243
|
+
const wildcardKey = Object.keys(rule).find(key => key === '*' || key[0] === '$');
|
|
244
|
+
if (wildcardKey) {
|
|
245
|
+
env[wildcardKey] = nextKey;
|
|
246
|
+
env.vars[wildcardKey] = nextKey;
|
|
247
|
+
}
|
|
248
|
+
nextKey = wildcardKey;
|
|
249
|
+
}
|
|
250
|
+
nextKey && rulePathKeys.push(nextKey);
|
|
251
|
+
rule = rule[nextKey];
|
|
252
|
+
}
|
|
253
|
+
// Now dig deeper to check nested .validate rules
|
|
254
|
+
if (isAllowed && ['set', 'update'].includes(operation) && !isPreFlight) {
|
|
255
|
+
// validate rules start at current path being written to
|
|
256
|
+
const startRule = pathInfo.keys.reduce((rule, key) => {
|
|
257
|
+
if (typeof rule !== 'object' || rule === null) {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
if (key in rule) {
|
|
261
|
+
return rule[key];
|
|
262
|
+
}
|
|
263
|
+
if ('*' in rule) {
|
|
264
|
+
return rule['*'];
|
|
265
|
+
}
|
|
266
|
+
const variableKey = Object.keys(rule).find(key => typeof key === 'string' && key.startsWith('$'));
|
|
267
|
+
if (variableKey) {
|
|
268
|
+
return rule[variableKey];
|
|
269
|
+
}
|
|
270
|
+
return null;
|
|
271
|
+
}, this.accessRules.rules);
|
|
272
|
+
const getNestedRules = (target, rule) => {
|
|
273
|
+
if (!rule) {
|
|
274
|
+
return [];
|
|
275
|
+
}
|
|
276
|
+
const nested = Object.keys(rule).reduce((arr, key) => {
|
|
277
|
+
if (key === '.validate' && ['string', 'function'].includes(typeof rule[key])) {
|
|
278
|
+
arr.push({ target, validate: rule[key] });
|
|
279
|
+
}
|
|
280
|
+
if (!key.startsWith('.')) {
|
|
281
|
+
const nested = getNestedRules([...target, key], rule[key]);
|
|
282
|
+
arr.push(...nested);
|
|
283
|
+
}
|
|
284
|
+
return arr;
|
|
285
|
+
}, []);
|
|
286
|
+
return nested;
|
|
287
|
+
};
|
|
288
|
+
// Check all that apply for sent data (update requires a different strategy)
|
|
289
|
+
const checkRules = getNestedRules([], startRule);
|
|
290
|
+
for (const check of checkRules) {
|
|
291
|
+
// Keep going as long as rules validate
|
|
292
|
+
const targetData = check.target.reduce((data, key) => {
|
|
293
|
+
if (data !== null && typeof data === 'object' && key in data) {
|
|
294
|
+
return data[key];
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
}, data.value);
|
|
298
|
+
if (typeof targetData === 'undefined' && operation === 'update' && check.target.length >= 1 && check.target[0] in data) {
|
|
299
|
+
// Ignore, data for direct child path is not being set by update operation
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const validateData = typeof targetData === 'undefined' ? null : targetData;
|
|
303
|
+
if (validateData === null) {
|
|
304
|
+
// Do not validate deletes, this should be done by ".write" or ".delete" rule
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const validatePath = PathInfo.get(path).child(check.target).path;
|
|
308
|
+
const validateEnv = {
|
|
309
|
+
...env,
|
|
310
|
+
operation: operation === 'update' ? (check.target.length === 0 ? 'update' : 'set') : operation,
|
|
311
|
+
data: validateData,
|
|
312
|
+
exists: async (target) => this.db.ref(getFullPath(validatePath, target)).exists(),
|
|
313
|
+
value: async (target, include) => {
|
|
314
|
+
const snap = await this.db.ref(getFullPath(validatePath, target)).get({ include });
|
|
315
|
+
return snap.val();
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
try {
|
|
319
|
+
const result = await (async () => {
|
|
320
|
+
let result;
|
|
321
|
+
if (typeof check.validate === 'function') {
|
|
322
|
+
result = await check.validate(validateEnv);
|
|
323
|
+
}
|
|
324
|
+
else if (typeof check.validate === 'string') {
|
|
325
|
+
result = await executeSandboxed(check.validate, validateEnv);
|
|
326
|
+
}
|
|
327
|
+
else if (typeof check.validate === 'boolean') {
|
|
328
|
+
result = check.validate ? 'allow' : 'deny';
|
|
329
|
+
}
|
|
330
|
+
if (result === 'cascade') {
|
|
331
|
+
this.debug.warn(`Rule at path ${validatePath} returned "cascade", but ${validateEnv.operation} rules always cascade`);
|
|
332
|
+
}
|
|
333
|
+
else if (!['cascade', 'deny', 'allow', true, false].includes(result)) {
|
|
334
|
+
this.debug.warn(`${validateEnv.operation} rule for path ${validatePath} possibly returned an unintentional value (${JSON.stringify(result)}) which results in outcome "${result ? 'allow' : 'deny'}"`);
|
|
335
|
+
}
|
|
336
|
+
if (['cascade', 'deny', 'allow'].includes(result)) {
|
|
337
|
+
return result;
|
|
338
|
+
}
|
|
339
|
+
return result ? 'allow' : 'deny';
|
|
340
|
+
})();
|
|
341
|
+
if (result === 'deny') {
|
|
342
|
+
return { allow: false, code: 'rule', message: `${operation} operation denied to path "${path}" by set rule`, rule: check.validate, rulePath: validatePath };
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
catch (err) {
|
|
346
|
+
// If rule execution throws an exception, don't allow. Can happen when rule is "auth.uid === '...'", and auth is null because the user is not signed in
|
|
347
|
+
return { allow: false, code: 'exception', message: `${operation} operation denied to path "${path}" by set rule`, rule: check.validate, rulePath: validatePath, details: err };
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return isAllowed ? allow : { allow: false, code: 'no_rule', message: `No rules set for requested path "${path}", defaulting to false` };
|
|
352
|
+
}
|
|
353
|
+
add(rulePaths, ruleTypes, callback) {
|
|
354
|
+
const paths = Array.isArray(rulePaths) ? rulePaths : [rulePaths];
|
|
355
|
+
const types = Array.isArray(ruleTypes) ? ruleTypes : [ruleTypes];
|
|
356
|
+
for (const path of paths) {
|
|
357
|
+
const keys = PathInfo.getPathKeys(path);
|
|
358
|
+
let target = this.accessRules.rules;
|
|
359
|
+
for (const key of keys) {
|
|
360
|
+
if (!(key in target)) {
|
|
361
|
+
target[key] = {};
|
|
362
|
+
}
|
|
363
|
+
target = target[key];
|
|
364
|
+
if (typeof target !== 'object' || target === null) {
|
|
365
|
+
throw new Error(`Cannot add rule because value of key "${key}" is not an object`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (const type of types) {
|
|
369
|
+
target[`.${type}`] = callback;
|
|
370
|
+
this.codeRules.push({ path, type, callback });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
164
375
|
//# sourceMappingURL=rules.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext, runInContext } from 'vm';
|
|
2
|
+
export async function executeSandboxed(code, env) {
|
|
3
|
+
// Using eval to execute code is dangerous, so we have to make sure we run in a sandbox
|
|
4
|
+
// so no globally available objects are accessible.
|
|
5
|
+
const context = createContext(env);
|
|
6
|
+
const result = await runInContext(code, context, { filename: 'sandbox', timeout: 10000, displayErrors: true, breakOnSigint: true });
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
export function isCodeSafe(code) {
|
|
10
|
+
return /eval|prototype|require|import/.test(code); // Do not allow prototype access, require or import statements
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=sandbox.js.map
|
package/dist/esm/schema/user.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
export const getPublicAccountDetails = (account) => {
|
|
2
|
-
return {
|
|
3
|
-
uid: account.uid,
|
|
4
|
-
username: account.username,
|
|
5
|
-
email: account.email,
|
|
6
|
-
displayName: account.display_name,
|
|
7
|
-
picture: account.picture,
|
|
8
|
-
emailVerified: account.email_verified,
|
|
9
|
-
created: account.created?.toISOString(),
|
|
10
|
-
prevSignin: account.prev_signin?.toISOString(),
|
|
11
|
-
prevSigninIp: account.prev_signin_ip,
|
|
12
|
-
lastSignin: account.last_signin?.toISOString(),
|
|
13
|
-
lastSigninIp: account.last_signin_ip,
|
|
14
|
-
changePassword: account.change_password,
|
|
15
|
-
changePasswordRequested: account.change_password_requested?.toISOString(),
|
|
16
|
-
changePasswordBefore: account.change_password_before?.toISOString(),
|
|
17
|
-
settings: account.settings,
|
|
18
|
-
};
|
|
19
|
-
};
|
|
1
|
+
export const getPublicAccountDetails = (account) => {
|
|
2
|
+
return {
|
|
3
|
+
uid: account.uid,
|
|
4
|
+
username: account.username,
|
|
5
|
+
email: account.email,
|
|
6
|
+
displayName: account.display_name,
|
|
7
|
+
picture: account.picture,
|
|
8
|
+
emailVerified: account.email_verified,
|
|
9
|
+
created: account.created?.toISOString(),
|
|
10
|
+
prevSignin: account.prev_signin?.toISOString(),
|
|
11
|
+
prevSigninIp: account.prev_signin_ip,
|
|
12
|
+
lastSignin: account.last_signin?.toISOString(),
|
|
13
|
+
lastSigninIp: account.last_signin_ip,
|
|
14
|
+
changePassword: account.change_password,
|
|
15
|
+
changePasswordRequested: account.change_password_requested?.toISOString(),
|
|
16
|
+
changePasswordBefore: account.change_password_before?.toISOString(),
|
|
17
|
+
settings: account.settings,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
20
|
//# sourceMappingURL=user.js.map
|