librebot-tools 0.0.0-stage → 0.4.5
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/LICENSE +21 -0
- package/README.md +277 -2
- package/bin/librebot.js +262 -0
- package/package.json +38 -4
- package/schema/v1/bot-yaml-schema.json +193 -0
- package/schema/v1/skills-sources-schema.json +31 -0
- package/src/contract.d.ts +25 -0
- package/src/contract.js +23 -0
- package/src/errors.js +28 -0
- package/src/icon.js +130 -0
- package/src/index.d.ts +271 -0
- package/src/index.js +57 -0
- package/src/parse.js +702 -0
- package/src/scaffold.js +279 -0
- package/src/schema.js +14 -0
- package/src/skills-import.js +50 -0
- package/src/skills-lock.js +697 -0
- package/src/util.js +27 -0
- package/src/validate.js +835 -0
- package/test/assets.test.mjs +82 -0
- package/test/export.test.mjs +155 -0
- package/test/gitlab-preview.test.mjs +23 -0
- package/test/gitlab-triggers.test.mjs +53 -0
- package/test/icon.test.mjs +137 -0
- package/test/lock.test.mjs +92 -0
- package/test/malformed.test.mjs +79 -0
- package/test/readme.test.mjs +109 -0
- package/test/semantics.test.mjs +186 -0
- package/test/standard-skills.test.mjs +41 -0
- package/test/vendor.test.mjs +135 -0
- package/test/whatsapp-actions.test.mjs +31 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://librebot.dev/schema/bot-source/v1/bot-yaml-schema.json",
|
|
4
|
+
"title": "Librebot bot.yaml (source format v1)",
|
|
5
|
+
"description": "Consolidated bot definition. instructions.md carries the main prompt; knowledge/ and skills/ are file trees; knowledgeSources holds structured external references. Builtin tools are always on and must not be declared; local-execution MCP servers are unsupported.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"additionalProperties": false,
|
|
8
|
+
"required": ["formatVersion", "name", "description", "modelRoles"],
|
|
9
|
+
"properties": {
|
|
10
|
+
"formatVersion": { "const": 1 },
|
|
11
|
+
"name": { "type": "string", "minLength": 1, "maxLength": 100 },
|
|
12
|
+
"description": { "type": "string", "minLength": 1, "maxLength": 1000 },
|
|
13
|
+
"icon": {
|
|
14
|
+
"type": "string",
|
|
15
|
+
"minLength": 1,
|
|
16
|
+
"maxLength": 512,
|
|
17
|
+
"pattern": "^(\\./)?[^.\\\\/:\\s][^\\\\:\\s]*\\.([sS][vV][gG]|[pP][nN][gG]|[wW][eE][bB][pP])$",
|
|
18
|
+
"description": "Repository-relative path to an SVG, PNG, or WebP bot icon. For PNG and WebP, use a square image, ideally 512 × 512 pixels; transparent backgrounds are supported. For SVG, use a square viewBox. Icons are displayed at small sizes, so prefer simple artwork with adequate padding and avoid fine details or text."
|
|
19
|
+
},
|
|
20
|
+
"modelRoles": {
|
|
21
|
+
"type": "array",
|
|
22
|
+
"minItems": 1,
|
|
23
|
+
"maxItems": 4,
|
|
24
|
+
"items": {
|
|
25
|
+
"type": "object",
|
|
26
|
+
"additionalProperties": false,
|
|
27
|
+
"required": ["role", "label"],
|
|
28
|
+
"properties": {
|
|
29
|
+
"role": { "enum": ["default", "smol", "slow", "plan"] },
|
|
30
|
+
"label": { "type": "string", "minLength": 1, "maxLength": 100 },
|
|
31
|
+
"requirements": {
|
|
32
|
+
"type": "object",
|
|
33
|
+
"additionalProperties": false,
|
|
34
|
+
"properties": {
|
|
35
|
+
"tools": { "type": "boolean" },
|
|
36
|
+
"vision": { "type": "boolean" },
|
|
37
|
+
"reasoning": { "type": "boolean" },
|
|
38
|
+
"minContextWindow": { "type": "number", "exclusiveMinimum": 0 }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"workspace": {
|
|
45
|
+
"type": "array",
|
|
46
|
+
"maxItems": 10,
|
|
47
|
+
"items": {
|
|
48
|
+
"type": "object",
|
|
49
|
+
"additionalProperties": false,
|
|
50
|
+
"required": ["key", "path"],
|
|
51
|
+
"properties": {
|
|
52
|
+
"key": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$" },
|
|
53
|
+
"label": { "type": "string", "minLength": 1, "maxLength": 100 },
|
|
54
|
+
"path": { "type": "string", "pattern": "^\\./([^/.][^/]*)(/[^/.][^/]*)*$|^\\./$" }
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"triggers": {
|
|
59
|
+
"type": "array",
|
|
60
|
+
"maxItems": 20,
|
|
61
|
+
"items": {
|
|
62
|
+
"type": "object",
|
|
63
|
+
"additionalProperties": false,
|
|
64
|
+
"required": ["key", "label", "kind", "eventTypes", "deciderInstructions"],
|
|
65
|
+
"properties": {
|
|
66
|
+
"key": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$" },
|
|
67
|
+
"label": { "type": "string", "minLength": 1, "maxLength": 100 },
|
|
68
|
+
"description": { "type": "string", "maxLength": 2000 },
|
|
69
|
+
"kind": { "enum": ["gitrepo", "whatsapp"] },
|
|
70
|
+
"eventTypes": {
|
|
71
|
+
"type": "array",
|
|
72
|
+
"minItems": 1,
|
|
73
|
+
"maxItems": 20,
|
|
74
|
+
"uniqueItems": true,
|
|
75
|
+
"items": { "enum": ["issue.opened", "issue.updated", "issue.closed", "issue.reopened", "issue.comment.created", "issue.comment.updated", "change_request.opened", "change_request.updated", "change_request.closed", "change_request.reopened", "change_request.merged", "change_request.commits_updated", "change_request.checks_updated", "change_request.comment.created", "change_request.comment.updated", "change_request.approval_changed", "repository.pushed", "message.created", "reaction.changed"] }
|
|
76
|
+
},
|
|
77
|
+
"inputs": {
|
|
78
|
+
"type": "array",
|
|
79
|
+
"maxItems": 50,
|
|
80
|
+
"items": {
|
|
81
|
+
"type": "object",
|
|
82
|
+
"additionalProperties": false,
|
|
83
|
+
"required": ["key", "type"],
|
|
84
|
+
"properties": {
|
|
85
|
+
"key": { "type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,63}$" },
|
|
86
|
+
"label": { "type": "string", "maxLength": 100 },
|
|
87
|
+
"type": { "enum": ["string", "number", "boolean", "select"] },
|
|
88
|
+
"required": { "type": "boolean" },
|
|
89
|
+
"options": {
|
|
90
|
+
"type": "array",
|
|
91
|
+
"minItems": 1,
|
|
92
|
+
"maxItems": 50,
|
|
93
|
+
"items": { "type": "string", "minLength": 1, "maxLength": 200 }
|
|
94
|
+
},
|
|
95
|
+
"help": { "type": "string", "maxLength": 500 }
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
"deciderRole": { "enum": ["default", "smol", "slow", "plan"] },
|
|
100
|
+
"deciderInstructions": { "type": "string", "minLength": 1, "maxLength": 20000 },
|
|
101
|
+
"enabledByDefault": { "type": "boolean" }
|
|
102
|
+
},
|
|
103
|
+
"allOf": [{
|
|
104
|
+
"if": { "properties": { "kind": { "const": "whatsapp" } } },
|
|
105
|
+
"then": { "properties": { "eventTypes": { "items": { "enum": ["message.created", "reaction.changed"] } } } },
|
|
106
|
+
"else": { "properties": { "eventTypes": { "items": { "enum": ["issue.opened", "issue.updated", "issue.closed", "issue.reopened", "issue.comment.created", "issue.comment.updated", "change_request.opened", "change_request.updated", "change_request.closed", "change_request.reopened", "change_request.merged", "change_request.commits_updated", "change_request.checks_updated", "change_request.comment.created", "change_request.comment.updated", "change_request.approval_changed", "repository.pushed"] } } } }
|
|
107
|
+
}]
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
"actions": {
|
|
111
|
+
"type": "array",
|
|
112
|
+
"maxItems": 20,
|
|
113
|
+
"items": {
|
|
114
|
+
"type": "object",
|
|
115
|
+
"additionalProperties": false,
|
|
116
|
+
"required": ["key", "kind", "capabilities"],
|
|
117
|
+
"properties": {
|
|
118
|
+
"key": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$" },
|
|
119
|
+
"kind": { "enum": ["gitrepo", "whatsapp"] },
|
|
120
|
+
"label": { "type": "string", "minLength": 1, "maxLength": 100 },
|
|
121
|
+
"capabilities": {
|
|
122
|
+
"type": "array", "minItems": 1, "maxItems": 13, "uniqueItems": true,
|
|
123
|
+
"items": { "enum": ["read_issue", "push_branch", "propose_change", "comment_issue", "search_issues", "read_change_request", "read_repository", "update_issue_labels", "react_issue", "update_issue_body", "update_issue_title", "publish_attachments", "merge_change_request", "read_messages", "send_text", "set_reaction", "remove_reaction"] }
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
"allOf": [{
|
|
127
|
+
"if": { "properties": { "kind": { "const": "whatsapp" } } },
|
|
128
|
+
"then": { "properties": { "capabilities": { "maxItems": 4, "items": { "enum": ["read_messages", "send_text", "set_reaction", "remove_reaction"] } } } },
|
|
129
|
+
"else": { "properties": { "capabilities": { "items": { "enum": ["read_issue", "push_branch", "propose_change", "comment_issue", "search_issues", "read_change_request", "read_repository", "update_issue_labels", "react_issue", "update_issue_body", "update_issue_title", "publish_attachments", "merge_change_request"] } } } }
|
|
130
|
+
}]
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
"connectors": {
|
|
134
|
+
"type": "array",
|
|
135
|
+
"maxItems": 50,
|
|
136
|
+
"items": {
|
|
137
|
+
"type": "object",
|
|
138
|
+
"additionalProperties": false,
|
|
139
|
+
"required": ["label", "tools"],
|
|
140
|
+
"properties": {
|
|
141
|
+
"label": { "type": "string", "minLength": 1, "maxLength": 200 },
|
|
142
|
+
"description": { "type": "string", "maxLength": 1000 },
|
|
143
|
+
"required": { "type": "boolean" },
|
|
144
|
+
"tools": {
|
|
145
|
+
"type": "array",
|
|
146
|
+
"minItems": 1,
|
|
147
|
+
"maxItems": 100,
|
|
148
|
+
"items": { "type": "string", "minLength": 1, "maxLength": 200 }
|
|
149
|
+
},
|
|
150
|
+
"resources": {
|
|
151
|
+
"type": "array",
|
|
152
|
+
"items": {
|
|
153
|
+
"type": "object",
|
|
154
|
+
"additionalProperties": false,
|
|
155
|
+
"required": ["key", "label", "tools"],
|
|
156
|
+
"properties": {
|
|
157
|
+
"key": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$" },
|
|
158
|
+
"label": { "type": "string", "minLength": 1, "maxLength": 200 },
|
|
159
|
+
"tools": {
|
|
160
|
+
"type": "array",
|
|
161
|
+
"minItems": 1,
|
|
162
|
+
"items": { "type": "string", "minLength": 1, "maxLength": 200 }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
"endpoint": {
|
|
168
|
+
"type": "string",
|
|
169
|
+
"format": "uri",
|
|
170
|
+
"pattern": "^https://",
|
|
171
|
+
"description": "Remote MCP server identity. The runtime binds only this endpoint; local execution is unsupported."
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
"knowledgeSources": {
|
|
177
|
+
"type": "array",
|
|
178
|
+
"maxItems": 100,
|
|
179
|
+
"items": {
|
|
180
|
+
"type": "object",
|
|
181
|
+
"additionalProperties": false,
|
|
182
|
+
"required": ["id", "name", "kind", "url"],
|
|
183
|
+
"properties": {
|
|
184
|
+
"id": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$" },
|
|
185
|
+
"name": { "type": "string", "minLength": 1, "maxLength": 200 },
|
|
186
|
+
"kind": { "enum": ["resource", "repo"] },
|
|
187
|
+
"url": { "type": "string", "format": "uri", "maxLength": 500 },
|
|
188
|
+
"ref": { "type": "string", "maxLength": 256 }
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"title": "Librebot skill sources v1",
|
|
4
|
+
"description": "External skills installed locally and committed with skills/sources.lock and skills/.vendor/.",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"additionalProperties": false,
|
|
7
|
+
"required": ["version", "sources"],
|
|
8
|
+
"properties": {
|
|
9
|
+
"version": { "const": 1 },
|
|
10
|
+
"sources": {
|
|
11
|
+
"type": "array",
|
|
12
|
+
"maxItems": 50,
|
|
13
|
+
"items": {
|
|
14
|
+
"oneOf": [
|
|
15
|
+
{ "type": "string", "minLength": 1, "maxLength": 2000, "description": "A source locator accepted by npx skills." },
|
|
16
|
+
{
|
|
17
|
+
"type": "object",
|
|
18
|
+
"additionalProperties": false,
|
|
19
|
+
"required": ["name", "source"],
|
|
20
|
+
"properties": {
|
|
21
|
+
"name": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$" },
|
|
22
|
+
"source": { "type": "string", "minLength": 1, "maxLength": 2000, "description": "A standard skills source locator; credentials must not be embedded." },
|
|
23
|
+
"ref": { "type": "string", "minLength": 1, "maxLength": 256, "description": "Optional explicit GitHub repository ref; other sources encode ref selection in their source locator." },
|
|
24
|
+
"skills": { "type": "array", "maxItems": 50, "items": { "type": "string", "minLength": 1, "maxLength": 100, "pattern": "^[^-\\s].*$" } }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const FORMAT_VERSION: 1;
|
|
2
|
+
export const MODEL_ROLES: readonly ["default", "smol", "slow", "plan"];
|
|
3
|
+
export const REPO_CAPABILITIES: readonly [
|
|
4
|
+
"read_issue", "push_branch", "propose_change", "comment_issue",
|
|
5
|
+
"search_issues", "read_change_request", "read_repository",
|
|
6
|
+
"update_issue_labels", "react_issue", "update_issue_body", "update_issue_title",
|
|
7
|
+
"publish_attachments", "merge_change_request",
|
|
8
|
+
];
|
|
9
|
+
export const GITREPO_TRIGGER_EVENT_TYPES: readonly [
|
|
10
|
+
"issue.opened", "issue.updated", "issue.closed", "issue.reopened",
|
|
11
|
+
"issue.comment.created", "issue.comment.updated",
|
|
12
|
+
"change_request.opened", "change_request.updated", "change_request.closed",
|
|
13
|
+
"change_request.reopened", "change_request.merged", "change_request.commits_updated",
|
|
14
|
+
"change_request.checks_updated",
|
|
15
|
+
"change_request.comment.created", "change_request.comment.updated",
|
|
16
|
+
"change_request.approval_changed", "repository.pushed",
|
|
17
|
+
];
|
|
18
|
+
export const WHATSAPP_CAPABILITIES: readonly ["read_messages", "send_text", "set_reaction", "remove_reaction"];
|
|
19
|
+
export const WHATSAPP_TRIGGER_EVENT_TYPES: readonly ["message.created", "reaction.changed"];
|
|
20
|
+
|
|
21
|
+
export type ModelRoleId = typeof MODEL_ROLES[number];
|
|
22
|
+
export type RepoCapability = typeof REPO_CAPABILITIES[number];
|
|
23
|
+
export type GitrepoTriggerEventType = typeof GITREPO_TRIGGER_EVENT_TYPES[number];
|
|
24
|
+
export type WhatsAppCapability = typeof WHATSAPP_CAPABILITIES[number];
|
|
25
|
+
export type WhatsAppTriggerEventType = typeof WHATSAPP_TRIGGER_EVENT_TYPES[number];
|
package/src/contract.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Portable source-contract vocabulary. Dependency-free and browser-safe so
|
|
2
|
+
// platform UIs and servers can import it (`librebot-tools/contract`) instead
|
|
3
|
+
// of keeping their own copies of these lists.
|
|
4
|
+
|
|
5
|
+
export const FORMAT_VERSION = 1;
|
|
6
|
+
export const MODEL_ROLES = Object.freeze(["default", "smol", "slow", "plan"]);
|
|
7
|
+
export const REPO_CAPABILITIES = Object.freeze([
|
|
8
|
+
"read_issue", "push_branch", "propose_change", "comment_issue",
|
|
9
|
+
"search_issues", "read_change_request", "read_repository",
|
|
10
|
+
"update_issue_labels", "react_issue", "update_issue_body", "update_issue_title",
|
|
11
|
+
"publish_attachments", "merge_change_request",
|
|
12
|
+
]);
|
|
13
|
+
export const GITREPO_TRIGGER_EVENT_TYPES = Object.freeze([
|
|
14
|
+
"issue.opened", "issue.updated", "issue.closed", "issue.reopened",
|
|
15
|
+
"issue.comment.created", "issue.comment.updated",
|
|
16
|
+
"change_request.opened", "change_request.updated", "change_request.closed",
|
|
17
|
+
"change_request.reopened", "change_request.merged", "change_request.commits_updated",
|
|
18
|
+
"change_request.checks_updated",
|
|
19
|
+
"change_request.comment.created", "change_request.comment.updated",
|
|
20
|
+
"change_request.approval_changed", "repository.pushed",
|
|
21
|
+
]);
|
|
22
|
+
export const WHATSAPP_CAPABILITIES = Object.freeze(["read_messages", "send_text", "set_reaction", "remove_reaction"]);
|
|
23
|
+
export const WHATSAPP_TRIGGER_EVENT_TYPES = Object.freeze(["message.created", "reaction.changed"]);
|
package/src/errors.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Aggregated source-validation errors. Every failure carries the file,
|
|
2
|
+
// field, and cause so CLI, server, and UI surfaces can render it identically.
|
|
3
|
+
|
|
4
|
+
export class BotSourceError extends Error {
|
|
5
|
+
/** @type {Array<{ file: string; field: string; message: string }>} */
|
|
6
|
+
errors;
|
|
7
|
+
/** @type {Array<{ file: string; field: string; message: string }>} */
|
|
8
|
+
warnings;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {Array<{ file: string; field: string; message: string }>} errors
|
|
12
|
+
* @param {Array<{ file: string; field: string; message: string }>} [warnings]
|
|
13
|
+
*/
|
|
14
|
+
constructor(errors, warnings = []) {
|
|
15
|
+
super(
|
|
16
|
+
"invalid bot source: " +
|
|
17
|
+
errors.map((e) => `${e.file}:${e.field}: ${e.message}`).join("; "),
|
|
18
|
+
);
|
|
19
|
+
this.name = "BotSourceError";
|
|
20
|
+
this.errors = errors;
|
|
21
|
+
this.warnings = warnings;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** @param {unknown} value */
|
|
26
|
+
export function isRecord(value) {
|
|
27
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
28
|
+
}
|
package/src/icon.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { SaxesParser } from "saxes";
|
|
2
|
+
import { imageSize } from "image-size";
|
|
3
|
+
import { sha256Hex } from "./util.js";
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_ICON = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="6" width="16" height="14" rx="4"/><path d="M12 3v3M8 16h8"/><circle cx="8.5" cy="11" r=".75" fill="currentColor"/><circle cx="15.5" cy="11" r=".75" fill="currentColor"/></svg>';
|
|
6
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
7
|
+
const elements = new Set("svg g defs path rect circle ellipse line polyline polygon linearGradient radialGradient stop clipPath mask pattern use symbol title desc".split(" "));
|
|
8
|
+
const attributes = new Set("id version viewBox width height x y x1 y1 x2 y2 cx cy r rx ry d points fill fill-opacity fill-rule stroke stroke-width stroke-opacity stroke-linecap stroke-linejoin stroke-miterlimit stroke-dasharray stroke-dashoffset opacity transform gradientTransform gradientUnits spreadMethod offset stop-color stop-opacity clip-path clip-rule mask maskUnits maskContentUnits patternUnits patternContentUnits patternTransform preserveAspectRatio vector-effect color href".split(" "));
|
|
9
|
+
attributes.add("style");
|
|
10
|
+
const styleProperties = new Set("fill fill-opacity fill-rule stroke stroke-width stroke-opacity stroke-linecap stroke-linejoin stroke-miterlimit stroke-dasharray stroke-dashoffset opacity stop-color stop-opacity clip-path clip-rule mask vector-effect color".split(" "));
|
|
11
|
+
|
|
12
|
+
export function iconPath(value) {
|
|
13
|
+
if (typeof value !== "string" || !value || value.length > 512 || /[:\\\s\u0000-\u001f\u007f]/.test(value) || value.startsWith("/")) throw new Error("icon must be a repository-relative SVG, PNG, or WebP path (max 512 characters)");
|
|
14
|
+
const path = value.startsWith("./") ? value.slice(2) : value;
|
|
15
|
+
if (path.split("/").some((part) => !part || part === "." || part === ".." || part.startsWith(".")) || !/\.(svg|png|webp)$/i.test(path)) throw new Error("icon must be a repository-relative SVG, PNG, or WebP path without traversal");
|
|
16
|
+
return path;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function square(width, height) {
|
|
20
|
+
if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || width !== height || width > 4096) throw new Error("icon dimensions must be positive, square, and at most 4096 pixels");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function svgDimensions(bytes) {
|
|
24
|
+
const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
25
|
+
const parser = new SaxesParser({ xmlns: true });
|
|
26
|
+
let root = false;
|
|
27
|
+
let dimensions;
|
|
28
|
+
parser.on("doctype", () => { throw new Error("SVG DOCTYPE and entities are forbidden"); });
|
|
29
|
+
parser.on("processinginstruction", () => { throw new Error("SVG processing instructions are forbidden"); });
|
|
30
|
+
parser.on("error", (error) => { throw error; });
|
|
31
|
+
parser.on("opentag", (node) => {
|
|
32
|
+
if (node.uri !== SVG_NS || !elements.has(node.local)) throw new Error(`unsafe SVG element: ${node.name}`);
|
|
33
|
+
if (!root) {
|
|
34
|
+
root = true;
|
|
35
|
+
if (node.local !== "svg") throw new Error("icon must have an SVG root");
|
|
36
|
+
const values = node.attributes.viewBox?.value.trim().split(/[\s,]+/).map(Number);
|
|
37
|
+
if (!values || values.length !== 4 || !values.every(Number.isFinite)) throw new Error("SVG icon requires a square viewBox");
|
|
38
|
+
square(values[2], values[3]);
|
|
39
|
+
const width = node.attributes.width?.value;
|
|
40
|
+
const height = node.attributes.height?.value;
|
|
41
|
+
if (width !== undefined || height !== undefined) {
|
|
42
|
+
if (!(width === "100%" && height === "100%")) {
|
|
43
|
+
if (!width || !height || !/^\d+(?:\.\d+)?(?:px)?$/.test(width) || !/^\d+(?:\.\d+)?(?:px)?$/.test(height)) throw new Error("SVG viewport must have matching numeric dimensions or 100% dimensions");
|
|
44
|
+
square(Number(width.replace(/px$/, "")), Number(height.replace(/px$/, "")));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
dimensions = { width: values[2], height: values[3] };
|
|
48
|
+
}
|
|
49
|
+
for (const attr of Object.values(node.attributes)) {
|
|
50
|
+
if (attr.uri === "http://www.w3.org/2000/xmlns/") {
|
|
51
|
+
if (attr.value !== SVG_NS && attr.value !== "http://www.w3.org/1999/xlink") throw new Error("unsupported SVG namespace");
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if ((attr.uri && !(attr.uri === "http://www.w3.org/1999/xlink" && attr.local === "href")) || !attributes.has(attr.local)) throw new Error(`unsafe SVG attribute: ${attr.name}`);
|
|
55
|
+
const value = attr.value;
|
|
56
|
+
if (attr.local === "style") {
|
|
57
|
+
for (const declaration of value.split(";")) {
|
|
58
|
+
if (!declaration.trim()) continue;
|
|
59
|
+
const colon = declaration.indexOf(":");
|
|
60
|
+
const property = declaration.slice(0, colon).trim().toLowerCase();
|
|
61
|
+
if (colon < 0 || !styleProperties.has(property) || !declaration.slice(colon + 1).trim() || /[{}!]/.test(declaration)) throw new Error("unsafe SVG style declaration");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (attr.local === "href" && !/^#[A-Za-z_][\w:.-]*$/.test(value)) throw new Error("SVG references must be local fragments");
|
|
65
|
+
// Reject CSS escapes/comments and anything except literal local paint references.
|
|
66
|
+
if (/[\\@]|\/\*|[\u0000-\u001f]/.test(value)) throw new Error("unsafe SVG attribute value");
|
|
67
|
+
const withoutUrls = value.replace(/url\(\s*(['"]?)#[A-Za-z_][\w:.-]*\1\s*\)/gi, "");
|
|
68
|
+
if (/url\s*\(|(?:https?|data|javascript|file):|\/\//i.test(withoutUrls)) throw new Error("external SVG references are forbidden");
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
parser.write(text).close();
|
|
72
|
+
if (!dimensions) throw new Error("icon must contain SVG artwork");
|
|
73
|
+
return dimensions;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Check complete container framing, not just image-size's header dimensions.
|
|
77
|
+
function validateRasterContainer(bytes, ext) {
|
|
78
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
79
|
+
const tag = (offset) => String.fromCharCode(...bytes.subarray(offset, offset + 4));
|
|
80
|
+
if (ext === "png") {
|
|
81
|
+
let offset = 8;
|
|
82
|
+
let header = false;
|
|
83
|
+
let data = false;
|
|
84
|
+
let ended = false;
|
|
85
|
+
while (offset + 12 <= bytes.length) {
|
|
86
|
+
const length = view.getUint32(offset);
|
|
87
|
+
const type = tag(offset + 4);
|
|
88
|
+
if (length > bytes.length - offset - 12) throw new Error("truncated PNG icon");
|
|
89
|
+
if (!header && (type !== "IHDR" || length !== 13)) throw new Error("PNG icon requires IHDR");
|
|
90
|
+
header = true;
|
|
91
|
+
if (type === "IDAT" && length > 0) data = true;
|
|
92
|
+
offset += length + 12;
|
|
93
|
+
if (type === "IEND") {
|
|
94
|
+
if (length !== 0 || offset !== bytes.length) throw new Error("invalid PNG end chunk");
|
|
95
|
+
ended = true;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (!header || !data || !ended) throw new Error("incomplete PNG icon");
|
|
100
|
+
} else {
|
|
101
|
+
if (bytes.length < 20 || tag(0) !== "RIFF" || tag(8) !== "WEBP" || view.getUint32(4, true) + 8 !== bytes.length) throw new Error("invalid WebP container length");
|
|
102
|
+
let offset = 12;
|
|
103
|
+
let image = false;
|
|
104
|
+
while (offset + 8 <= bytes.length) {
|
|
105
|
+
const length = view.getUint32(offset + 4, true);
|
|
106
|
+
if (length > bytes.length - offset - 8) throw new Error("truncated WebP icon");
|
|
107
|
+
const type = tag(offset);
|
|
108
|
+
if ((type === "VP8 " || type === "VP8L" || type === "ANMF") && length > 0) image = true;
|
|
109
|
+
offset += 8 + length + (length % 2);
|
|
110
|
+
}
|
|
111
|
+
if (!image || offset !== bytes.length) throw new Error("incomplete WebP icon");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function validateIcon(path, bytes) {
|
|
116
|
+
path = iconPath(path);
|
|
117
|
+
if (!(bytes instanceof Uint8Array) || !bytes.length) throw new Error("icon asset bytes are missing");
|
|
118
|
+
if (bytes.length > 256 * 1024) throw new Error("icon exceeds the 256 KiB limit");
|
|
119
|
+
const ext = path.slice(path.lastIndexOf(".") + 1).toLowerCase();
|
|
120
|
+
let dimensions;
|
|
121
|
+
if (ext === "svg") dimensions = svgDimensions(bytes);
|
|
122
|
+
else {
|
|
123
|
+
dimensions = imageSize(bytes);
|
|
124
|
+
if (dimensions.type !== ext) throw new Error("icon format does not match its file extension");
|
|
125
|
+
validateRasterContainer(bytes, ext);
|
|
126
|
+
square(dimensions.width, dimensions.height);
|
|
127
|
+
if (dimensions.images?.some((image) => image.width !== dimensions.width || image.height !== dimensions.height)) throw new Error("icon frames must have matching square dimensions");
|
|
128
|
+
}
|
|
129
|
+
return { path, bytes, mime: ext === "svg" ? "image/svg+xml" : `image/${ext}`, sha256: sha256Hex(bytes), size: bytes.length, width: dimensions.width, height: dimensions.height };
|
|
130
|
+
}
|