Repository navigation
Expand file tree
/
Copy pathstyle-data.js
More file actions
406 lines (370 loc) · 15.3 KB
/
Copy pathstyle-data.js
File metadata and controls
406 lines (370 loc) · 15.3 KB
1
2
3
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* StyleCraft v1.26.0 - shared import and storage data guard */
(function(global) {
if (global.StyleCraftData) return;
const MAX_IMPORT_BYTES = 8 * 1024 * 1024;
const APPLY_TYPES = new Set(['domain', 'url', 'url-prefix', 'regexp', 'wildcard']);
const BLOCKED_KEYS = new Set(['__proto__', 'prototype', 'constructor', 'toString', 'valueOf', 'hasOwnProperty']);
function isPlainObject(value) {
return !!value && typeof value === 'object' && !Array.isArray(value);
}
function cloneJson(value) {
return JSON.parse(JSON.stringify(value || {}));
}
function byteLength(value) {
const text = typeof value === 'string' ? value : JSON.stringify(value || '');
try { return new TextEncoder().encode(text).length; } catch { return text.length; }
}
function cleanKey(value) {
const key = String(value || '').trim();
if (!key || BLOCKED_KEYS.has(key)) return '';
return key;
}
function addRejected(rejected, path, reason) {
rejected.push({ path, reason });
}
function addTrustWarning(warnings, code, message, severity) {
warnings.push({ code, message, severity: severity || 'warning' });
}
function stripCssEscapes(text) {
return text.replace(/\\([0-9a-fA-F]{1,6})\s?/g, (_, hex) =>
String.fromCodePoint(parseInt(hex, 16))
).replace(/\\(.)/g, '$1');
}
function analyzeCssTrust(css, options) {
const text = String(css || '');
const normalized = stripCssEscapes(text);
const blockRemote = options && options.blockRemoteCss === true;
const warnings = [];
if (/url\(\s*(['"]?)\s*(?:javascript|vbscript):/i.test(normalized) || /@import\s+(?:url\()?\s*(['"]?)\s*(?:javascript|vbscript):/i.test(normalized)) {
addTrustWarning(warnings, 'blocked-scheme', 'Blocked javascript/vbscript URL in CSS.', 'block');
}
if (/url\(\s*(['"]?)\s*(?:https?:)?\/\//i.test(text)) {
addTrustWarning(warnings, 'remote-url', 'Remote url() fetch can disclose visited pages to a third party.', blockRemote ? 'block' : 'warning');
}
if (/@import\s+(?:url\()?\s*(['"]?)\s*(?:https?:)?\/\//i.test(text)) {
addTrustWarning(warnings, 'remote-import', 'Remote @import fetch can load third-party CSS.', blockRemote ? 'block' : 'warning');
}
if (/(?:input\s*\[[^\]]*type\s*=\s*['"]?password|textarea|select)\b/i.test(text)) {
addTrustWarning(warnings, 'sensitive-selector', 'Selector targets sensitive form controls.');
}
if (/position\s*:\s*fixed/i.test(text) && (/(?:z-index\s*:\s*(?:999|[1-9]\d{3,})|inset\s*:\s*0\b)/i.test(text) || /(?:top|left|right|bottom)\s*:\s*0\b/i.test(text))) {
addTrustWarning(warnings, 'overlay-risk', 'Fixed full-page overlay pattern can be deceptive.');
}
const blocked = warnings.some(item => item.severity === 'block');
return {
status: blocked ? 'blocked' : (warnings.length ? 'review' : 'trusted'),
warnings,
checkedAt: new Date().toISOString()
};
}
function trustSummary(trust) {
return (trust && trust.warnings && trust.warnings[0] && trust.warnings[0].message) || 'CSS trust check failed.';
}
function assertCssAllowed(css, options) {
const trust = analyzeCssTrust(css, options);
if (trust.status === 'blocked') throw new Error('Blocked CSS: ' + trustSummary(trust));
return trust;
}
function sanitizeMeta(value) {
if (!isPlainObject(value)) return undefined;
const out = {};
for (const [key, val] of Object.entries(value)) {
if (!cleanKey(key)) continue;
if (['string', 'number', 'boolean'].includes(typeof val)) out[key] = val;
}
return Object.keys(out).length ? out : undefined;
}
function sanitizePreprocessor(value, path, rejected) {
if (value === undefined) return undefined;
if (!isPlainObject(value)) {
addRejected(rejected, path, 'Preprocessor metadata must be an object.');
return undefined;
}
const syntax = ['css', 'scss', 'sass'].includes(value.syntax) ? value.syntax : '';
if (!syntax || typeof value.source !== 'string') {
addRejected(rejected, path, 'Preprocessor metadata needs css/scss/sass syntax and string source.');
return undefined;
}
return { syntax, source: value.source };
}
function sanitizeUserCssState(value) {
if (!isPlainObject(value)) return null;
const out = {};
const meta = sanitizeMeta(value.meta);
if (meta) out.meta = meta;
if (Array.isArray(value.variables)) {
out.variables = value.variables.filter(item => isPlainObject(item) && typeof item.name === 'string').map(item => ({
kind: typeof item.kind === 'string' ? item.kind : 'var',
type: typeof item.type === 'string' ? item.type : 'text',
name: item.name,
label: typeof item.label === 'string' ? item.label : item.name,
default: item.default,
value: item.value,
options: Array.isArray(item.options) ? item.options.filter(opt => isPlainObject(opt)).map(opt => ({
value: String(opt.value || ''),
label: String(opt.label || opt.value || ''),
default: opt.default === true
})) : []
}));
}
if (isPlainObject(value.values)) out.values = cloneJson(value.values);
if (Array.isArray(value.appliesTo)) out.appliesTo = value.appliesTo.filter(item => isPlainObject(item) && typeof item.type === 'string' && typeof item.value === 'string').map(item => ({ type: item.type, value: item.value }));
return Object.keys(out).length ? out : null;
}
function parseUserCssState(css, existingState) {
const parser = global.StyleCraftUserCSS;
if (!parser || typeof parser.parse !== 'function') return sanitizeUserCssState(existingState);
const parsed = parser.parse(css || '');
if (!parsed.hasMeta && !parsed.variables.length && !parsed.appliesTo.length) return sanitizeUserCssState(existingState);
const values = parser.mergeValues(parsed.variables, existingState && existingState.values);
return {
meta: parsed.meta,
variables: parsed.variables,
values,
appliesTo: parsed.appliesTo
};
}
function attachUserCssState(out, css, existingState) {
const usercss = parseUserCssState(css, existingState);
if (!usercss) return null;
out.usercss = usercss;
out.meta = Object.assign({}, usercss.meta || {}, out.meta || {});
if (usercss.meta && usercss.meta.updateURL && !out.sourceUrl) out.sourceUrl = usercss.meta.updateURL;
if (usercss.meta && usercss.meta.updateURL && out.meta && !out.meta.sourceUrl) out.meta.sourceUrl = usercss.meta.updateURL;
if (usercss.appliesTo && usercss.appliesTo.length && !out.appliesTo) out.appliesTo = usercss.appliesTo;
return usercss;
}
function sanitizeAppliesTo(value, path, rejected) {
if (value === undefined) return undefined;
if (!Array.isArray(value)) {
addRejected(rejected, path, 'Applies-to rules must be an array.');
return undefined;
}
const out = [];
value.forEach((rule, index) => {
const rulePath = path + '[' + index + ']';
if (!isPlainObject(rule)) {
addRejected(rejected, rulePath, 'Applies-to rule must be an object.');
return;
}
const type = APPLY_TYPES.has(String(rule.type || '').trim()) ? String(rule.type).trim() : 'domain';
const ruleValue = typeof rule.value === 'string' ? rule.value.trim() : '';
if (!ruleValue) {
addRejected(rejected, rulePath, 'Applies-to rule needs a value.');
return;
}
if (type === 'regexp') {
try { new RegExp(ruleValue); } catch {
addRejected(rejected, rulePath, 'Applies-to regexp is invalid.');
return;
}
}
out.push({ type, value: ruleValue });
});
return out.length ? out : undefined;
}
function sanitizeTheme(id, theme, path, rejected) {
if (!isPlainObject(theme)) {
addRejected(rejected, path, 'Theme entry must be an object.');
return null;
}
const rawCSS = typeof theme.rawCSS === 'string' ? theme.rawCSS : (typeof theme.css === 'string' ? theme.css : '');
const css = typeof theme.css === 'string' ? theme.css : rawCSS;
if (!rawCSS && !css) {
addRejected(rejected, path, 'Theme needs rawCSS or css text.');
return null;
}
const trust = analyzeCssTrust(rawCSS || css);
if (trust.status === 'blocked') {
addRejected(rejected, path, 'Blocked CSS: ' + trustSummary(trust));
return null;
}
const out = {
name: typeof theme.name === 'string' && theme.name.trim() ? theme.name : id,
css,
rawCSS,
enabled: theme.enabled !== false,
trust
};
const meta = sanitizeMeta(theme.meta);
if (meta) out.meta = meta;
attachUserCssState(out, rawCSS || css, theme.usercss);
for (const key of ['source', 'installedAt', 'updatedAt', 'sourceUrl', 'updateUrl']) {
if (typeof theme[key] === 'string') out[key] = theme[key];
}
const preprocessor = sanitizePreprocessor(theme.preprocessor, path + '.preprocessor', rejected);
if (preprocessor) out.preprocessor = preprocessor;
return out;
}
function sanitizeDomainEntry(domain, entry, rejected) {
if (!isPlainObject(entry)) {
addRejected(rejected, domain, 'Domain entry must be an object.');
return null;
}
const out = { themes: {}, customCSS: '', customEnabled: entry.customEnabled !== false };
if (entry.customCSS !== undefined) {
if (typeof entry.customCSS === 'string') out.customCSS = entry.customCSS;
else addRejected(rejected, domain + '.customCSS', 'Custom CSS must be a string.');
}
if (out.customCSS.trim()) {
const trust = analyzeCssTrust(out.customCSS);
if (trust.status === 'blocked') {
addRejected(rejected, domain + '.customCSS', 'Blocked CSS: ' + trustSummary(trust));
out.customCSS = '';
} else {
out.trust = trust;
}
}
attachUserCssState(out, out.customCSS, entry.usercss);
if (entry.themes !== undefined) {
if (!isPlainObject(entry.themes)) {
addRejected(rejected, domain + '.themes', 'Themes must be an object keyed by id.');
} else {
for (const [themeIdRaw, theme] of Object.entries(entry.themes)) {
const themeId = cleanKey(themeIdRaw);
if (!themeId) {
addRejected(rejected, domain + '.themes.' + themeIdRaw, 'Theme id is empty or unsafe.');
continue;
}
const cleanTheme = sanitizeTheme(themeId, theme, domain + '.themes.' + themeId, rejected);
if (cleanTheme) out.themes[themeId] = cleanTheme;
}
}
}
const appliesTo = sanitizeAppliesTo(entry.appliesTo, domain + '.appliesTo', rejected);
if (appliesTo) out.appliesTo = appliesTo;
const meta = sanitizeMeta(entry.meta);
if (meta) out.meta = Object.assign({}, out.meta || {}, meta);
if (typeof entry.sourceUrl === 'string') out.sourceUrl = entry.sourceUrl;
const preprocessor = sanitizePreprocessor(entry.preprocessor, domain + '.preprocessor', rejected);
if (preprocessor) out.preprocessor = preprocessor;
if (!out.customCSS.trim() && !Object.keys(out.themes).length) {
addRejected(rejected, domain, 'Domain has no valid custom CSS or themes.');
return null;
}
return out;
}
function normalizeStyleData(rawData) {
if (!isPlainObject(rawData)) throw new Error('Import data must be an object keyed by domain.');
const rejected = [];
const data = {};
let themeCount = 0;
for (const [domainRaw, entry] of Object.entries(rawData)) {
const domain = cleanKey(domainRaw);
if (!domain) {
addRejected(rejected, domainRaw, 'Domain key is empty or unsafe.');
continue;
}
const cleanEntry = sanitizeDomainEntry(domain, entry, rejected);
if (!cleanEntry) continue;
themeCount += Object.keys(cleanEntry.themes || {}).length;
data[domain] = cleanEntry;
}
return {
data,
rejected,
accepted: Object.keys(data).length,
themeCount
};
}
function extractStyleDataPayload(raw) {
if (isPlainObject(raw) && isPlainObject(raw.stylecraft_data)) return raw.stylecraft_data;
if (isPlainObject(raw) && isPlainObject(raw.data)) return raw.data;
if (isPlainObject(raw) && isPlainObject(raw.styles)) return raw.styles;
return raw;
}
function summarizeMerge(existingData, incomingData, finalData, mode) {
const beforeKeys = new Set(Object.keys(existingData || {}));
let added = 0;
let replaced = 0;
for (const key of Object.keys(incomingData || {})) {
if (beforeKeys.has(key)) replaced++;
else added++;
}
return {
mode,
added,
replaced,
before: beforeKeys.size,
after: Object.keys(finalData || {}).length
};
}
function mergeStyleData(existingData, incomingData) {
const merged = cloneJson(existingData);
for (const [domain, entry] of Object.entries(incomingData || {})) {
if (!merged[domain]) {
merged[domain] = entry;
continue;
}
merged[domain] = Object.assign({}, merged[domain], entry, {
themes: Object.assign({}, merged[domain].themes || {}, entry.themes || {})
});
}
return merged;
}
function planStyleDataImport(rawData, existingData, options) {
const opts = Object.assign({ mode: 'replace', source: 'StyleCraft JSON' }, options || {});
if (byteLength(rawData) > MAX_IMPORT_BYTES) throw new Error('Import is too large for local storage safety limit.');
const normalized = normalizeStyleData(rawData);
if (!normalized.accepted) throw new Error('No valid style entries found; existing data was left unchanged.');
const mode = opts.mode === 'merge' ? 'merge' : 'replace';
const finalData = mode === 'merge'
? mergeStyleData(existingData || {}, normalized.data)
: normalized.data;
if (byteLength(finalData) > MAX_IMPORT_BYTES) throw new Error('Validated import exceeds local storage safety limit.');
return {
data: finalData,
incoming: normalized.data,
quarantine: makeQuarantine(opts.source, normalized.rejected),
summary: Object.assign(summarizeMerge(existingData || {}, normalized.data, finalData, mode), {
accepted: normalized.accepted,
rejected: normalized.rejected.length,
themes: normalized.themeCount,
source: opts.source
})
};
}
function planNativeImport(raw, existingData, options) {
return planStyleDataImport(extractStyleDataPayload(raw), existingData, options);
}
function makeQuarantine(source, rejected) {
return {
source,
timestamp: new Date().toISOString(),
rejected: rejected.map(item => ({
path: item.path,
reason: item.reason
}))
};
}
function createPreImportBackup(data, settings, source) {
return {
data: cloneJson(data),
settings: cloneJson(settings),
timestamp: new Date().toISOString(),
reason: 'pre-import',
source
};
}
function addBackup(backups, backup, limit) {
const list = Array.isArray(backups) ? backups.slice() : [];
list.unshift(backup);
while (list.length > (limit || 3)) list.pop();
return list;
}
function sanitizeSettings(value) {
return isPlainObject(value) ? cloneJson(value) : null;
}
global.StyleCraftData = {
MAX_IMPORT_BYTES,
normalizeStyleData,
extractStyleDataPayload,
planStyleDataImport,
planNativeImport,
createPreImportBackup,
addBackup,
sanitizeSettings,
analyzeCssTrust,
assertCssAllowed
};
})(typeof globalThis !== 'undefined' ? globalThis : window);