@rebasepro/cli 0.10.1-canary.d8d45b2 → 0.10.1-canary.ff9ccd6
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/bundle.d.ts +35 -0
- package/dist/commands/cloud/bundle-deploy.d.ts +25 -0
- package/dist/fold-static.d.ts +46 -0
- package/dist/index.es.js +175 -2
- package/dist/index.es.js.map +1 -1
- package/package.json +7 -7
- package/templates/overlays/baas/backend/src/index.ts +7 -0
- package/templates/overlays/baas/backend/src/storage.ts +71 -0
- package/templates/template/backend/src/index.ts +7 -0
- package/templates/template/config/admin.d.ts +9 -0
- package/templates/template/config/collections/authors.ts +11 -9
- package/templates/template/config/collections/posts.ts +6 -4
- package/templates/template/config/collections/presets/ecommerce/categories.ts +7 -5
- package/templates/template/config/collections/presets/ecommerce/orders.ts +32 -20
- package/templates/template/config/collections/presets/ecommerce/products.ts +21 -13
- package/templates/template/config/collections/tags.ts +5 -3
- package/templates/template/config/collections/users.ts +34 -29
- package/templates/template/config/frontend-assets.d.ts +17 -0
- package/templates/template/config/index.ts +6 -0
- package/templates/template/config/package.json +26 -25
- package/templates/template/config/storage.ts +88 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { isPublicStoragePath, type StorageAuthorize } from "@rebasepro/types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Who may do what to files in storage.
|
|
5
|
+
*
|
|
6
|
+
* Storage is **not** under row-level security. Collections are: a request runs as
|
|
7
|
+
* `rebase_user` and Postgres decides row by row. Object storage has no equivalent,
|
|
8
|
+
* so keys share one flat namespace and this hook is the whole authorization model.
|
|
9
|
+
* Without it the server refuses to boot in production, because "authenticated"
|
|
10
|
+
* would be the only thing standing between a signed-in visitor and every file in
|
|
11
|
+
* the bucket — they could `GET /storage/list?prefix=` to enumerate every key, then
|
|
12
|
+
* read, overwrite or delete any of them.
|
|
13
|
+
*
|
|
14
|
+
* ## The model this starter ships
|
|
15
|
+
*
|
|
16
|
+
* A CMS's files are **shared content**, not private per-user documents: hero
|
|
17
|
+
* images, author pictures, product photos. The collections in this template upload
|
|
18
|
+
* to `author_pictures/`, `posts/hero/`, `product_images/` — paths named after the
|
|
19
|
+
* content, not after a user. So the boundary that matters here is not *whose file
|
|
20
|
+
* is this* but *who is allowed to change the library*:
|
|
21
|
+
*
|
|
22
|
+
* | operation | who |
|
|
23
|
+
* | --- | --- |
|
|
24
|
+
* | `read` of `public/…` | anyone, signed in or not |
|
|
25
|
+
* | `read` | any signed-in user — it is content, and the admin panel renders it |
|
|
26
|
+
* | `write`, `delete`, `list` | `admin` or `editor` only |
|
|
27
|
+
*
|
|
28
|
+
* Gating `list` is the important half. Enumeration is what turns "keys are hard to
|
|
29
|
+
* guess" into "keys are trivially known", and it is the first step of the attack
|
|
30
|
+
* the boot guard exists to prevent.
|
|
31
|
+
*
|
|
32
|
+
* ## If your app is multi-tenant, replace this
|
|
33
|
+
*
|
|
34
|
+
* Per-user or per-tenant files need an **ownership** check, and ownership lives in
|
|
35
|
+
* a row rather than in a key — so `ctx.data` hands you trusted, RLS-bypassing read
|
|
36
|
+
* access to answer it:
|
|
37
|
+
*
|
|
38
|
+
* ```ts
|
|
39
|
+
* export const storageAuthorize: StorageAuthorize = async ({ key, user, operation, data }) => {
|
|
40
|
+
* if (operation === "read" && isPublicStoragePath(key)) return true;
|
|
41
|
+
* if (!user) return false;
|
|
42
|
+
*
|
|
43
|
+
* // Writes land under the caller's own prefix.
|
|
44
|
+
* if (operation === "write") return key.startsWith(`users/${user.uid}/`);
|
|
45
|
+
* if (operation === "list") return key.startsWith(`users/${user.uid}/`);
|
|
46
|
+
*
|
|
47
|
+
* // Reads and deletes check the row that owns the object.
|
|
48
|
+
* const attachment = (await data?.collection("attachments").find({
|
|
49
|
+
* where: { storage_key: ["==", key] }, limit: 1
|
|
50
|
+
* }))?.data?.[0];
|
|
51
|
+
* return attachment?.owner_id === user.uid;
|
|
52
|
+
* };
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* Note that a prefix rule alone is not an access-control model for reads — it only
|
|
56
|
+
* works because a write *puts* the object there. Anything already in the bucket
|
|
57
|
+
* needs the row consulted.
|
|
58
|
+
*
|
|
59
|
+
* The two escape hatches, if you would rather not write a hook at all:
|
|
60
|
+
* `storagePublicRead: true` when the bucket genuinely is a public read-only CDN,
|
|
61
|
+
* or `storageInsecureAllowAnyAuthenticated: true` for a single-tenant app where
|
|
62
|
+
* every signed-in user is trusted with every file. Both are set on
|
|
63
|
+
* `initializeRebaseBackend` in `backend/src/index.ts`.
|
|
64
|
+
*/
|
|
65
|
+
export const storageAuthorize: StorageAuthorize = ({ key, user, operation }) => {
|
|
66
|
+
// The `public/` prefix is the framework's convention for world-readable
|
|
67
|
+
// objects, and it is what signed download URLs for public assets rely on.
|
|
68
|
+
if (operation === "read" && isPublicStoragePath(key)) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Everything else needs a caller. `user` is null on routes that allow
|
|
73
|
+
// unauthenticated access, so this is the anonymous denial.
|
|
74
|
+
if (!user) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Content is shared: any signed-in user may read it. A `viewer` has to be able
|
|
79
|
+
// to see the images the admin panel shows them.
|
|
80
|
+
if (operation === "read") {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Changing or enumerating the library is an editor's job. Matches the roles in
|
|
85
|
+
// `collections/users.ts` — widen this if you add your own.
|
|
86
|
+
const roles = user.roles ?? [];
|
|
87
|
+
return roles.includes("admin") || roles.includes("editor");
|
|
88
|
+
};
|