-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
191 lines (159 loc) · 5.63 KB
/
validation.js
File metadata and controls
191 lines (159 loc) · 5.63 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
/**
* Input validation and sanitization utilities
*/
// Speed validation
export function validateSpeed(speed) {
const parsed = parseFloat(speed);
if (isNaN(parsed)) return { valid: false, error: 'Invalid number' };
if (parsed < 0.05) return { valid: false, error: 'Speed must be at least 0.05x' };
if (parsed > 16) return { valid: false, error: 'Speed must be at most 16x' };
return { valid: true, value: parsed };
}
// Speed array validation
export function validateSpeedArray(speeds) {
if (!Array.isArray(speeds)) return { valid: false, error: 'Must be an array' };
const validatedSpeeds = [];
const errors = [];
for (let i = 0; i < speeds.length; i++) {
const result = validateSpeed(speeds[i]);
if (result.valid) {
validatedSpeeds.push(result.value);
} else {
errors.push(`Item ${i + 1}: ${result.error}`);
}
}
// Remove duplicates and sort
const uniqueSpeeds = [...new Set(validatedSpeeds)].sort((a, b) => a - b);
return {
valid: errors.length === 0,
value: uniqueSpeeds,
errors
};
}
// Keyboard shortcut validation
export function validateShortcut(key) {
if (typeof key !== 'string') return { valid: false, error: 'Must be a string' };
const sanitized = key.trim().toLowerCase();
if (sanitized.length === 0) return { valid: false, error: 'Cannot be empty' };
if (sanitized.length > 1) return { valid: false, error: 'Must be a single character' };
// Disallowed keys
const disallowedKeys = ['control', 'alt', 'shift', 'meta', 'tab', 'escape', 'enter', 'backspace', 'delete'];
if (disallowedKeys.includes(sanitized)) {
return { valid: false, error: 'Control keys not allowed' };
}
return { valid: true, value: sanitized };
}
// Shortcut object validation
export function validateShortcuts(shortcuts) {
if (typeof shortcuts !== 'object' || shortcuts === null) {
return { valid: false, error: 'Must be an object' };
}
const validatedShortcuts = {};
const errors = [];
const requiredKeys = ['speedUp', 'speedDown', 'reset'];
for (const key of requiredKeys) {
if (!(key in shortcuts)) {
errors.push(`Missing required key: ${key}`);
continue;
}
const result = validateShortcut(shortcuts[key]);
if (result.valid) {
validatedShortcuts[key] = result.value;
} else {
errors.push(`${key}: ${result.error}`);
}
}
// Check for duplicates
const values = Object.values(validatedShortcuts);
if (values.length !== new Set(values).size) {
errors.push('All shortcuts must be unique');
}
return {
valid: errors.length === 0,
value: validatedShortcuts,
errors
};
}
// Boolean validation
export function validateBoolean(value) {
if (typeof value === 'boolean') return { valid: true, value };
if (value === 'true' || value === '1') return { valid: true, value: true };
if (value === 'false' || value === '0') return { valid: true, value: false };
return { valid: false, error: 'Must be true or false' };
}
// Complete settings validation
export function validateSettings(settings) {
if (typeof settings !== 'object' || settings === null) {
return { valid: false, error: 'Settings must be an object' };
}
const validatedSettings = {};
const errors = [];
// Validate customSpeeds
if ('customSpeeds' in settings) {
const result = validateSpeedArray(settings.customSpeeds);
if (result.valid) {
validatedSettings.customSpeeds = result.value;
} else {
errors.push(...result.errors.map(e => `Speeds: ${e}`));
}
}
// Validate shortcuts
if ('shortcuts' in settings) {
const result = validateShortcuts(settings.shortcuts);
if (result.valid) {
validatedSettings.shortcuts = result.value;
} else {
errors.push(...result.errors.map(e => `Shortcuts: ${e}`));
}
}
// Validate boolean settings
const booleanKeys = ['enableNumberShortcuts', 'showSpeedButtons', 'showShortcutHints'];
for (const key of booleanKeys) {
if (key in settings) {
const result = validateBoolean(settings[key]);
if (result.valid) {
validatedSettings[key] = result.value;
} else {
errors.push(`${key}: ${result.error}`);
}
}
}
return {
valid: errors.length === 0,
value: validatedSettings,
errors
};
}
// Sanitize HTML content (basic XSS prevention)
export function sanitizeHTML(str) {
if (typeof str !== 'string') return '';
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\//g, '/');
}
// Validate and sanitize text input
export function validateTextInput(input, options = {}) {
const {
maxLength = 1000,
allowEmpty = false,
trim = true
} = options;
if (typeof input !== 'string') {
return { valid: false, error: 'Must be text' };
}
let processed = trim ? input.trim() : input;
if (!allowEmpty && processed.length === 0) {
return { valid: false, error: 'Cannot be empty' };
}
if (processed.length > maxLength) {
return { valid: false, error: `Too long (max ${maxLength} characters)` };
}
return {
valid: true,
value: sanitizeHTML(processed)
};
}