fix(custom-widgets): address v2 merge review
This commit is contained in:
1 parent
6aceb2fd1f
commit
48ce83806d
122 files changed
+8534
-1447
No files matched your search
@@ -1,5 +1,101 @@
|
||||
/// <reference path="../pb_data/types.d.ts" />
|
||||
|
||||
const CUSTOM_WIDGET_SCHEMA = "homarr-custom-widget-v2";
|
||||
const CUSTOM_CSS_SCHEMA = "homarr-custom-css-v1";
|
||||
const MAX_CSS_LENGTH = 16_384;
|
||||
const MAX_CONTENT_LENGTH = 1_000_000;
|
||||
|
||||
const rejectRequest = (message) => {
|
||||
throw new BadRequestError(message);
|
||||
};
|
||||
|
||||
const requestBodyValue = (event, name) => {
|
||||
const body = event.requestInfo().body;
|
||||
return body && typeof body.get === "function" ? body.get(name) : body ? body[name] : undefined;
|
||||
};
|
||||
|
||||
const validateWidgetManifest = (content) => {
|
||||
let widget;
|
||||
try {
|
||||
widget = JSON.parse(content);
|
||||
} catch {
|
||||
rejectRequest("Widget content must be valid JSON");
|
||||
}
|
||||
if (!widget || Array.isArray(widget) || typeof widget !== "object") rejectRequest("Widget must be a JSON object");
|
||||
if (widget.$schema !== CUSTOM_WIDGET_SCHEMA) rejectRequest("Widget schema is not supported");
|
||||
if (typeof widget.name !== "string" || !widget.name.trim() || widget.name.length > 100)
|
||||
rejectRequest("Widget name is invalid");
|
||||
if (!widget.sources || Array.isArray(widget.sources) || typeof widget.sources !== "object" || !widget.sources.default)
|
||||
rejectRequest("Widget must define a default source");
|
||||
if (Object.keys(widget.sources).length > 8) rejectRequest("Widget has too many sources");
|
||||
for (const source of Object.values(widget.sources)) {
|
||||
if (!source || Array.isArray(source) || typeof source !== "object") rejectRequest("Widget source is invalid");
|
||||
if (typeof source.baseUrl !== "string" || !/^https?:\/\//.test(source.baseUrl))
|
||||
rejectRequest("Widget source URL is invalid");
|
||||
if (!["public", "private", "loopback"].includes(source.networkScope))
|
||||
rejectRequest("Widget source network scope is invalid");
|
||||
}
|
||||
if (!widget.requests || Array.isArray(widget.requests) || typeof widget.requests !== "object")
|
||||
rejectRequest("Widget requests are invalid");
|
||||
if (!widget.options || Array.isArray(widget.options) || typeof widget.options !== "object")
|
||||
rejectRequest("Widget options are invalid");
|
||||
if (typeof widget.template !== "string" || !widget.template.trim()) rejectRequest("Widget template is invalid");
|
||||
return JSON.stringify(widget);
|
||||
};
|
||||
|
||||
const validateAndNormalizeSubmission = (record) => {
|
||||
const type = record.getString("type");
|
||||
const content = record.getString("content");
|
||||
if (!content || content.length > MAX_CONTENT_LENGTH) rejectRequest("Submission content is invalid");
|
||||
record.set("title", record.getString("title").trim());
|
||||
record.set("description", record.getString("description").trim());
|
||||
record.set("changelog", record.getString("changelog").trim());
|
||||
if (type === "customCss") {
|
||||
if (!content.trim() || content.length > MAX_CSS_LENGTH) rejectRequest("Custom CSS is empty or too large");
|
||||
record.set("widgetSchema", CUSTOM_CSS_SCHEMA);
|
||||
return;
|
||||
}
|
||||
if (type !== "customWidget") rejectRequest("Submission type is invalid");
|
||||
record.set("content", validateWidgetManifest(content));
|
||||
record.set("widgetSchema", CUSTOM_WIDGET_SCHEMA);
|
||||
};
|
||||
|
||||
onBootstrap((event) => {
|
||||
event.next();
|
||||
const users = event.app.findCollectionByNameOrId("users");
|
||||
const clientId = String($os.getenv("GITHUB_CLIENT_ID") || "").trim();
|
||||
const clientSecret = String($os.getenv("GITHUB_CLIENT_SECRET") || "").trim();
|
||||
const configured = Boolean(clientId && clientSecret);
|
||||
users.oauth2.enabled = configured;
|
||||
users.oauth2.providers = configured ? [{ name: "github", clientId, clientSecret }] : [];
|
||||
event.app.save(users);
|
||||
});
|
||||
|
||||
onRecordCreateRequest((event) => {
|
||||
validateAndNormalizeSubmission(event.record);
|
||||
event.record.set("revision", 1);
|
||||
event.record.set("changelog", "");
|
||||
event.record.set("outdated", false);
|
||||
event.next();
|
||||
}, "submissions");
|
||||
|
||||
onRecordUpdateRequest((event) => {
|
||||
const original = event.record.original();
|
||||
const currentRevision = original.getInt("revision");
|
||||
const expectedRevision = Number(requestBodyValue(event, "expectedRevision"));
|
||||
if (!Number.isSafeInteger(expectedRevision) || expectedRevision !== currentRevision) {
|
||||
rejectRequest("Submission changed since it was read");
|
||||
}
|
||||
validateAndNormalizeSubmission(event.record);
|
||||
event.record.set("revision", currentRevision + 1);
|
||||
event.next();
|
||||
}, "submissions");
|
||||
|
||||
onRecordCreateRequest((event) => {
|
||||
event.record.set("status", "open");
|
||||
event.next();
|
||||
}, "reports");
|
||||
|
||||
onRecordAfterCreateSuccess((event) => {
|
||||
try {
|
||||
const votes = event.app.findCollectionByNameOrId("votes");
|
||||
@@ -81,7 +177,7 @@ onRecordAfterCreateSuccess((event) => {
|
||||
const submission = event.app.findRecordById("submissions", event.record.get("submission"));
|
||||
const reporter = event.app.findRecordById("users", event.record.get("reporter"));
|
||||
const publicOrigin = $os.getenv("WORKSHOP_PUBLIC_ORIGIN").replace(/\/$/, "");
|
||||
const admins = event.app.findAllRecords("users").filter((user) => user.getBool("isAdmin") && Boolean(user.email()));
|
||||
const admins = event.app.findRecordsByFilter("users", "isAdmin = true && email != ''", "", 100, 0);
|
||||
|
||||
if (!publicOrigin || admins.length === 0) {
|
||||
event.next();
|
||||
|
||||
Reference in new issue
Block a user