-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.js
More file actions
792 lines (668 loc) · 27 KB
/
Copy pathvalidate.js
File metadata and controls
792 lines (668 loc) · 27 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// validate.js - Security validation utilities for Cyberdark
// Centralized input sanitization to prevent XSS and injection attacks
(function (global) {
'use strict';
// ============================================================================
// Color Validation
// ============================================================================
/**
* Validates and sanitizes hex color strings
* @param {string} color - Color value to validate
* @param {string} fallback - Fallback color if invalid
* @returns {string} Valid hex color or fallback
*/
function sanitizeHexColor(color, fallback = '#000000') {
if (typeof color !== 'string') return fallback;
// Strict hex color regex: #RGB or #RRGGBB
const hexRegex = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
if (hexRegex.test(color)) {
return color.toLowerCase();
}
return fallback;
}
/**
* Validates a palette of colors
* @param {Object} palette - Object with color1, color2, color3, color4
* @param {Object} defaults - Default palette
* @returns {Object} Sanitized palette
*/
function sanitizeColorPalette(palette, defaults) {
return {
color1: sanitizeHexColor(palette?.color1, defaults.color1),
color2: sanitizeHexColor(palette?.color2, defaults.color2),
color3: sanitizeHexColor(palette?.color3, defaults.color3),
color4: sanitizeHexColor(palette?.color4, defaults.color4)
};
}
// ============================================================================
// Numeric Validation
// ============================================================================
/**
* Validates and clamps numeric values to a range
* @param {any} value - Value to validate
* @param {number} min - Minimum allowed value
* @param {number} max - Maximum allowed value
* @param {number} defaultValue - Default if invalid
* @returns {number} Valid number in range
*/
function sanitizeNumericRange(value, min, max, defaultValue) {
if (value === null || value === undefined || value === '') {
return defaultValue;
}
const num = Number(value);
if (isNaN(num) || !isFinite(num)) {
return defaultValue;
}
return Math.max(min, Math.min(max, num));
}
/**
* Validates font size
* @param {any} fontSize - Font size value
* @returns {number} Valid font size (12-24)
*/
function sanitizeFontSize(fontSize) {
return sanitizeNumericRange(fontSize, 12, 24, 16);
}
/**
* Validates line height
* @param {any} lineHeight - Line height value
* @returns {number} Valid line height (1.0-2.2)
*/
function sanitizeLineHeight(lineHeight) {
return sanitizeNumericRange(lineHeight, 1.0, 2.2, 1.5);
}
// ============================================================================
// Domain Validation
// ============================================================================
/**
* Validates and sanitizes a domain name
* @param {string} domain - Domain to validate
* @returns {string|null} Valid domain or null
*/
function sanitizeDomain(domain) {
if (typeof domain !== 'string') return null;
// Trim whitespace
domain = domain.trim().toLowerCase();
// Empty string
if (!domain) return null;
// Max length check (253 chars for domain)
if (domain.length > 253) return null;
// Basic domain regex (allows subdomains, not full URL validation)
// Allows: example.com, sub.example.com, localhost
const domainRegex = /^([a-z0-9-]+\.)*[a-z0-9-]+\.[a-z]{2,}$|^localhost$/i;
if (domainRegex.test(domain)) {
return domain;
}
return null;
}
/**
* Validates and sanitizes an array of domains
* @param {Array|string} domains - Array or newline-separated string of domains
* @returns {Array} Array of valid domains
*/
function sanitizeDomainList(domains) {
let domainArray = [];
if (typeof domains === 'string') {
domainArray = domains.split(/\r?\n/);
} else if (Array.isArray(domains)) {
domainArray = domains;
} else {
return [];
}
return domainArray
.map(d => sanitizeDomain(d))
.filter(d => d !== null)
.slice(0, 1000); // Max 1000 domains to prevent DoS
}
// ============================================================================
// Time Validation
// ============================================================================
/**
* Validates time string in HH:MM format
* @param {string} time - Time string
* @param {string} fallback - Fallback time
* @returns {string} Valid time string
*/
function sanitizeTimeString(time, fallback = '00:00') {
if (typeof time !== 'string') return fallback;
// HH:MM format (00:00 to 23:59)
const timeRegex = /^([0-1][0-9]|2[0-3]):([0-5][0-9])$/;
if (timeRegex.test(time)) {
return time;
}
return fallback;
}
// ============================================================================
// Settings Object Validation
// ============================================================================
/**
* Validates colorBlindMode setting
* @param {any} mode - Color blind mode value
* @returns {string} Valid mode or 'none'
*/
function sanitizeColorBlindMode(mode) {
const validModes = ['none', 'protanopia', 'deuteranopia', 'tritanopia', 'achromatopsia'];
// Handle legacy boolean
if (mode === true) return 'protanopia';
if (mode === false) return 'none';
if (typeof mode === 'string' && validModes.includes(mode)) {
return mode;
}
return 'none';
}
/**
* Validates and sanitizes per-site overrides JSON
* SECURITY: Prevents prototype pollution
* @param {string|Object} overrides - Per-site overrides
* @returns {Object} Safe overrides object
*/
function sanitizePerSiteOverrides(overrides) {
let parsed = {};
// Parse if string
if (typeof overrides === 'string') {
try {
parsed = JSON.parse(overrides);
} catch (e) {
return Object.create(null);
}
} else if (typeof overrides === 'object' && overrides !== null) {
parsed = overrides;
} else {
return Object.create(null);
}
// Create clean object without prototype
const safe = Object.create(null);
// Validate each domain and its settings
for (const key in parsed) {
if (!parsed.hasOwnProperty(key)) continue;
// Skip prototype pollution attempts
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
continue;
}
const domain = sanitizeDomain(key);
if (!domain) continue;
// Validate settings object for this domain
const domainSettings = parsed[key];
if (typeof domainSettings !== 'object' || domainSettings === null) {
continue;
}
safe[domain] = Object.create(null);
// Validate individual properties
if (domainSettings.color1) safe[domain].color1 = sanitizeHexColor(domainSettings.color1, '#00ffff');
if (domainSettings.color2) safe[domain].color2 = sanitizeHexColor(domainSettings.color2, '#00ff00');
if (domainSettings.color3) safe[domain].color3 = sanitizeHexColor(domainSettings.color3, '#ff00ff');
if (domainSettings.color4) safe[domain].color4 = sanitizeHexColor(domainSettings.color4, '#ff0000');
if (domainSettings.textShadow !== undefined) safe[domain].textShadow = Boolean(domainSettings.textShadow);
if (domainSettings.highContrast !== undefined) safe[domain].highContrast = Boolean(domainSettings.highContrast);
if (domainSettings.focusOutline !== undefined) safe[domain].focusOutline = Boolean(domainSettings.focusOutline);
if (domainSettings.reducedMotion !== undefined) safe[domain].reducedMotion = Boolean(domainSettings.reducedMotion);
if (domainSettings.fontSize) safe[domain].fontSize = sanitizeFontSize(domainSettings.fontSize);
if (domainSettings.lineHeight) safe[domain].lineHeight = sanitizeLineHeight(domainSettings.lineHeight);
if (domainSettings.colorBlindMode) safe[domain].colorBlindMode = sanitizeColorBlindMode(domainSettings.colorBlindMode);
}
// Limit total number of overrides (prevent storage DoS)
const domains = Object.keys(safe);
if (domains.length > 100) {
const limited = Object.create(null);
domains.slice(0, 100).forEach(d => {
limited[d] = safe[d];
});
return limited;
}
return safe;
}
/**
* Validates complete settings object
* @param {Object} settings - Settings object to validate
* @param {Object} defaults - Default settings
* @returns {Object} Validated settings
*/
function validateSettingsObject(settings, defaults) {
if (!settings || typeof settings !== 'object') {
return Object.assign({}, defaults);
}
const validated = {
color1: sanitizeHexColor(settings.color1, defaults.color1),
color2: sanitizeHexColor(settings.color2, defaults.color2),
color3: sanitizeHexColor(settings.color3, defaults.color3),
color4: sanitizeHexColor(settings.color4, defaults.color4),
textShadow: Boolean(settings.textShadow),
highContrast: Boolean(settings.highContrast),
focusOutline: Boolean(settings.focusOutline),
reducedMotion: Boolean(settings.reducedMotion),
fontSize: sanitizeFontSize(settings.fontSize),
lineHeight: sanitizeLineHeight(settings.lineHeight),
colorBlindMode: sanitizeColorBlindMode(settings.colorBlindMode),
blacklist: sanitizeDomainList(settings.blacklist || []),
perSiteOverrides: sanitizePerSiteOverrides(settings.perSiteOverrides || {}),
debugMode: Boolean(settings.debugMode)
};
// Schedule validation
if (settings.schedule && typeof settings.schedule === 'object') {
validated.schedule = {
enabled: Boolean(settings.schedule.enabled),
start: sanitizeTimeString(settings.schedule.start, '20:00'),
end: sanitizeTimeString(settings.schedule.end, '06:00')
};
} else {
validated.schedule = defaults.schedule || { enabled: false, start: '20:00', end: '06:00' };
}
return validated;
}
// ============================================================================
// Storage Helpers
// ============================================================================
/**
* Calculates approximate size of object in bytes
* @param {any} obj - Object to measure
* @returns {number} Approximate size in bytes
*/
function calculateObjectSize(obj) {
const str = JSON.stringify(obj);
return new Blob([str]).size;
}
/**
* Checks if object fits in Chrome sync storage quota
* Quota: 100KB total, 8KB per item
* @param {Object} obj - Object to check
* @returns {boolean} True if fits in quota
*/
function fitsInSyncQuota(obj) {
const size = calculateObjectSize(obj);
return size < 8000; // 8KB limit with some margin
}
// ============================================================================
// HMAC Signing (Future Enhancement)
// ============================================================================
/**
* Simple hash function for settings integrity
* NOTE: This is NOT cryptographically secure, just a checksum
* For production, use Web Crypto API
* @param {Object} data - Data to hash
* @returns {string} Hash string
*/
async function calculateChecksum(data) {
const str = JSON.stringify(data);
// Use SubtleCrypto if available
if (typeof crypto !== 'undefined' && crypto.subtle) {
const encoder = new TextEncoder();
const dataBuffer = encoder.encode(str);
const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
// Fallback: simple checksum (not secure)
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32-bit integer
}
return hash.toString(16);
}
/**
* Signs settings object for export
* @param {Object} settings - Settings to sign
* @returns {Promise<Object>} Signed object with checksum
*/
async function signSettings(settings) {
const checksum = await calculateChecksum(settings);
return {
version: '1.0',
checksum: checksum,
data: settings
};
}
/**
* Verifies signed settings
* @param {Object} signedSettings - Signed settings object
* @returns {Promise<Object|null>} Settings if valid, null if invalid
*/
async function verifySettings(signedSettings) {
if (!signedSettings || typeof signedSettings !== 'object') {
return null;
}
// Legacy format (no signature)
if (!signedSettings.version && !signedSettings.checksum) {
return signedSettings; // Return as-is with warning
}
const { checksum, data } = signedSettings;
if (!data) return null;
const calculatedChecksum = await calculateChecksum(data);
if (checksum === calculatedChecksum) {
return data;
}
return null; // Checksum mismatch
}
// ============================================================================
// Bookmark Validation (Smart Bookmarks Feature)
// SECURITY: All web content is treated as potentially malicious
// ============================================================================
/**
* Maximum lengths for bookmark fields
* SECURITY: Prevents storage exhaustion and buffer-based attacks
*/
const BOOKMARK_LIMITS = Object.freeze({
TITLE_MAX: 500,
EXCERPT_MAX: 1000,
TEXT_MAX: 100000, // 100KB max per bookmark
URL_MAX: 2048,
SITE_NAME_MAX: 100,
WORD_COUNT_MAX: 1000000,
MAX_BOOKMARKS: 1000,
EMBEDDING_DIM: 384 // MiniLM output dimension
});
/**
* Sanitize text extracted from web pages
* SECURITY: Removes HTML tags, control characters, and enforces length limits
* @param {any} text - Raw extracted text (untrusted)
* @param {number} maxLength - Maximum allowed length
* @returns {string} - Safe plain text
*/
function sanitizeExtractedText(text, maxLength = BOOKMARK_LIMITS.TEXT_MAX) {
// Type check - reject non-strings
if (typeof text !== 'string') {
return '';
}
let safe = text;
// Strip any HTML tags (defense in depth - Readability should do this)
// SECURITY: Prevents XSS via stored content
safe = safe.replace(/<[^>]*>/g, '');
// Remove null bytes - can cause truncation attacks
safe = safe.replace(/\0/g, '');
// Remove control characters except newlines and tabs
// Range: \x00-\x08 (before tab), \x0b-\x0c (vertical tab, form feed),
// \x0e-\x1f (before space), \x7f (DEL)
safe = safe.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '');
// Normalize multiple whitespace to single space (preserve newlines)
safe = safe.replace(/[^\S\n]+/g, ' ');
// Remove excessive newlines (max 2 consecutive)
safe = safe.replace(/\n{3,}/g, '\n\n');
// Trim leading/trailing whitespace
safe = safe.trim();
// Enforce maximum length
// SECURITY: Prevents storage exhaustion
if (safe.length > maxLength) {
safe = safe.substring(0, maxLength);
}
return safe;
}
/**
* Validate and sanitize bookmark URL
* SECURITY: Only allows http/https, rejects dangerous URL schemes
* @param {any} url - URL to validate (untrusted)
* @returns {string|null} - Valid URL or null
*/
function sanitizeBookmarkUrl(url) {
if (typeof url !== 'string') {
return null;
}
// Trim whitespace
url = url.trim();
// Empty check
if (!url) {
return null;
}
// Length check
if (url.length > BOOKMARK_LIMITS.URL_MAX) {
return null;
}
try {
const parsed = new URL(url);
// SECURITY: Only allow http and https protocols
// Blocks: javascript:, data:, file:, chrome:, about:, etc.
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
return null;
}
// SECURITY: Double-check for javascript: injection attempts
// Some browsers may normalize URLs in unexpected ways
const lowercaseHref = parsed.href.toLowerCase();
if (lowercaseHref.includes('javascript:') ||
lowercaseHref.includes('data:') ||
lowercaseHref.includes('vbscript:')) {
return null;
}
// Return the normalized URL
return parsed.href;
} catch (e) {
// Invalid URL
return null;
}
}
/**
* Sanitize complete bookmark metadata object
* SECURITY: Validates all fields, uses Object.create(null) to prevent prototype pollution
* @param {any} metadata - Raw metadata object (untrusted)
* @returns {Object} - Safe metadata object
*/
function sanitizeBookmarkMetadata(metadata) {
// Create object without prototype chain
// SECURITY: Prevents prototype pollution attacks
const safe = Object.create(null);
// Handle null/undefined/non-object input
if (!metadata || typeof metadata !== 'object' || Array.isArray(metadata)) {
safe.title = 'Untitled';
safe.excerpt = '';
safe.url = '';
safe.siteName = '';
safe.wordCount = 0;
safe.extractedAt = Date.now();
return safe;
}
// Sanitize title
safe.title = sanitizeExtractedText(metadata.title, BOOKMARK_LIMITS.TITLE_MAX) || 'Untitled';
// Sanitize excerpt
safe.excerpt = sanitizeExtractedText(metadata.excerpt, BOOKMARK_LIMITS.EXCERPT_MAX) || '';
// Sanitize URL
safe.url = sanitizeBookmarkUrl(metadata.url) || '';
// Sanitize site name
safe.siteName = sanitizeExtractedText(metadata.siteName, BOOKMARK_LIMITS.SITE_NAME_MAX) || '';
// Sanitize word count (positive integer, max 1M)
safe.wordCount = sanitizeNumericRange(
metadata.wordCount,
0,
BOOKMARK_LIMITS.WORD_COUNT_MAX,
0
);
// Sanitize timestamp (must be valid Unix timestamp, not in far future)
const now = Date.now();
const maxFutureTime = now + 86400000; // 1 day in future max (clock skew tolerance)
const minPastTime = 0; // Unix epoch
safe.extractedAt = sanitizeNumericRange(
metadata.extractedAt,
minPastTime,
maxFutureTime,
now
);
return safe;
}
/**
* Validate embedding vector
* SECURITY: Ensures correct dimensions and no NaN/Infinity values (DoS prevention)
* @param {any} vector - Embedding vector to validate (untrusted)
* @returns {Float32Array|null} - Valid embedding or null
*/
function sanitizeEmbeddingVector(vector) {
// Must be Float32Array
if (!(vector instanceof Float32Array)) {
// Try to convert from regular array
if (Array.isArray(vector)) {
try {
vector = new Float32Array(vector);
} catch (e) {
return null;
}
} else {
return null;
}
}
// Check dimensions
if (vector.length !== BOOKMARK_LIMITS.EMBEDDING_DIM) {
return null;
}
// SECURITY: Check for NaN and Infinity values
// These could cause issues in vector similarity calculations
for (let i = 0; i < vector.length; i++) {
if (!Number.isFinite(vector[i])) {
return null;
}
}
return vector;
}
/**
* Validate bookmark message structure
* SECURITY: Validates all message fields before processing
* @param {any} message - Message to validate (untrusted)
* @param {Array<string>} allowedActions - List of allowed action types
* @returns {boolean} - True if valid
*/
function validateBookmarkMessage(message, allowedActions) {
// Must be object
if (!message || typeof message !== 'object' || Array.isArray(message)) {
return false;
}
// Must have action field
if (typeof message.action !== 'string') {
return false;
}
// Action must be in allowed list
if (!allowedActions.includes(message.action)) {
return false;
}
return true;
}
/**
* Validate message sender
* SECURITY: Ensures message is from this extension only
* @param {Object} sender - Chrome message sender object
* @param {string} extensionId - Expected extension ID
* @returns {boolean} - True if valid sender
*/
function validateMessageSender(sender, extensionId) {
// Must be object
if (!sender || typeof sender !== 'object') {
return false;
}
// Must match extension ID
if (sender.id !== extensionId) {
return false;
}
// If from tab, tab ID must be valid integer
if (sender.tab !== undefined) {
if (!sender.tab || typeof sender.tab.id !== 'number' ||
!Number.isInteger(sender.tab.id) || sender.tab.id < 0) {
return false;
}
}
return true;
}
/**
* Generate secure bookmark ID
* Uses crypto.randomUUID if available, falls back to secure random
* @returns {string} - UUID v4 string
*/
function generateBookmarkId() {
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
return crypto.randomUUID();
}
// Fallback using crypto.getRandomValues
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
// Set version (4) and variant (RFC 4122)
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
// Convert to hex string with dashes
const hex = Array.from(bytes, b => b.toString(16).padStart(2, '0')).join('');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}
/**
* Validate complete bookmark object for storage
* SECURITY: Final validation before persisting to IndexedDB
* @param {any} bookmark - Bookmark to validate (untrusted)
* @returns {Object} - Validated bookmark
* @throws {Error} - If validation fails
*/
function validateBookmarkForStorage(bookmark) {
if (!bookmark || typeof bookmark !== 'object') {
throw new Error('Invalid bookmark: must be an object');
}
// Sanitize metadata
const metadata = sanitizeBookmarkMetadata(bookmark);
// URL is required
if (!metadata.url) {
throw new Error('Invalid bookmark: URL is required');
}
// Create validated bookmark object
const validated = Object.create(null);
// Copy sanitized metadata
validated.id = bookmark.id && typeof bookmark.id === 'string'
? bookmark.id.substring(0, 36)
: generateBookmarkId();
validated.title = metadata.title;
validated.excerpt = metadata.excerpt;
validated.url = metadata.url;
validated.siteName = metadata.siteName;
validated.wordCount = metadata.wordCount;
validated.extractedAt = metadata.extractedAt;
// Validate embedding if present
if (bookmark.embedding !== undefined && bookmark.embedding !== null) {
const validEmbedding = sanitizeEmbeddingVector(bookmark.embedding);
if (!validEmbedding) {
throw new Error('Invalid bookmark: embedding has wrong dimensions or contains invalid values');
}
validated.embedding = validEmbedding;
}
// Validate compressed text if present
if (bookmark.compressedText !== undefined && bookmark.compressedText !== null) {
if (!(bookmark.compressedText instanceof Blob)) {
throw new Error('Invalid bookmark: compressedText must be a Blob');
}
// Size check (max 100KB compressed)
if (bookmark.compressedText.size > BOOKMARK_LIMITS.TEXT_MAX) {
throw new Error('Invalid bookmark: compressedText exceeds maximum size');
}
validated.compressedText = bookmark.compressedText;
}
// Add sync timestamp
validated.syncedAt = Date.now();
return validated;
}
// ============================================================================
// Export
// ============================================================================
// Attach to global scope
const CyberdarkValidate = {
// Existing exports
sanitizeHexColor,
sanitizeColorPalette,
sanitizeNumericRange,
sanitizeFontSize,
sanitizeLineHeight,
sanitizeDomain,
sanitizeDomainList,
sanitizeTimeString,
sanitizeColorBlindMode,
sanitizePerSiteOverrides,
validateSettingsObject,
calculateObjectSize,
fitsInSyncQuota,
calculateChecksum,
signSettings,
verifySettings,
// Bookmark exports (new)
BOOKMARK_LIMITS,
sanitizeExtractedText,
sanitizeBookmarkUrl,
sanitizeBookmarkMetadata,
sanitizeEmbeddingVector,
validateBookmarkMessage,
validateMessageSender,
generateBookmarkId,
validateBookmarkForStorage
};
global.CyberdarkValidate = CyberdarkValidate;
// Node.js export for testing
if (typeof module !== 'undefined' && module.exports) {
module.exports = CyberdarkValidate;
}
})(typeof window !== 'undefined' ? window : this);