-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
190 lines (167 loc) · 5.21 KB
/
index.ts
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
// Typescript version
const validateCommand = (command: string, allowedCommands: string[]): boolean => {
return allowedCommands.includes(command.split(" ")[0]);
};
// SRI for CDN
const addSRItoCDNScript = (url: string, integrity: string): void => {
const script = document.createElement('script');
script.src = url;
script.integrity = integrity;
script.crossOrigin = 'anonymous';
document.head.appendChild(script);
};
// CSRF Prevention
const generateCSRFToken = (): string => {
const array = new Uint8Array(16);
window.crypto.getRandomValues(array);
const stringArray = Array.from(array, byte => String.fromCharCode(byte));
return btoa(stringArray.join(''));
};
const setCSRFToken = (form: HTMLFormElement): void => {
const token = generateCSRFToken();
const csrfInput = form.querySelector('input[name="_csrf"]') as HTMLInputElement;
csrfInput.value = token;
document.cookie = `_csrf=${token}; Secure; SameSite=Strict`;
};
// HTTPS Enforcement
const enforceHTTPS = (): void => {
if (window.location.protocol === 'http:') {
window.location.href = window.location.href.replace('http:', 'https:');
}
const meta = document.createElement('meta');
meta.httpEquiv = 'Content-Security-Policy';
meta.content = 'upgrade-insecure-requests';
document.head.appendChild(meta);
};
// XSS Prevention
const escapeHTML = (str: string): string => {
const escape: { [key: string]: string } = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return str.replace(/[&<>"']/g, (match) => escape[match]);
};
const sanitiseHTML = (str: string): string => {
const temp = document.createElement('div');
temp.innerHTML = str;
const scripts = temp.getElementsByTagName('script');
while (scripts.length) {
const script = scripts[0];
if (script && script.parentNode) {
script.parentNode.removeChild(script);
}
}
return temp.innerHTML;
};
const sanitiseInput = (input: string): string => {
const element = document.createElement('div');
element.innerText = input;
return element.innerHTML;
};
const isValidInput = (input: string): boolean => {
const xssPattern = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;
return !xssPattern.test(input);
};
// LDAP Injection Prevention
const escapeLDAP = (input: string): string => {
const escapeMap: { [char: string]: string } = {
'*': '\\2a',
'(': '\\28',
')': '\\29',
'\\': '\\5c',
'\0': '\\00',
'|': '\\7c'
};
return input.replace(/[*()\\\0|]/g, char => escapeMap[char]);
};
// SQL Injection Prevention
const escapeSQL = (input: string): string => {
return input.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, (char) => {
switch (char) {
case "\0": return "\\0";
case "\x08": return "\\b";
case "\x09": return "\\t";
case "\x1a": return "\\z";
case "\n": return "\\n";
case "\r": return "\\r";
case "\"":
case "'":
case "\\":
case "%": return "\\" + char;
default: return char;
}
});
};
// HTTP Parameter Pollution Prevention
const sanitiseParameters = (params: URLSearchParams): URLSearchParams => {
const uniqueParams = new URLSearchParams();
for (const [key, value] of params.entries()) {
if (!uniqueParams.has(key)) {
uniqueParams.append(key, value);
}
}
return uniqueParams;
};
const validateHTTPMethod = (method: string, allowedMethods: string[]): boolean => {
return allowedMethods.includes(method.toUpperCase());
};
// Header Injection Prevention
const sanitiseHeader = (header: string): string => {
return header.replace(/[\r\n]/g, '');
};
// XML Injection Prevention
const sanitiseXML = (input: string): string => {
return input.replace(/[<>&'"]/g, (char) => {
switch (char) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '\'': return ''';
case '"': return '"';
default: return char;
}
});
};
// SSL/TLS Validation
const validateSSLCertificate = (url: string): void => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status !== 200) {
console.error('Invalid SSL/TLS configuration');
}
};
xhr.send();
};
// CSP Setting
const setCSP = (directives: { [key: string]: string }): void => {
const csp = Object.entries(directives)
.map(([key, value]) => `${key} ${value}`)
.join('; ');
const meta = document.createElement('meta');
meta.httpEquiv = 'Content-Security-Policy';
meta.content = csp;
document.head.appendChild(meta);
};
export {
addSRItoCDNScript,
escapeHTML,
escapeLDAP,
escapeSQL,
enforceHTTPS,
generateCSRFToken,
isValidInput,
sanitiseHeader,
sanitiseHTML,
sanitiseInput,
sanitiseParameters,
sanitiseXML,
setCSRFToken,
setCSP,
validateCommand,
validateHTTPMethod,
validateSSLCertificate
};