Repository navigation
Expand file tree
/
Copy pathtest-browser-functionality.js
More file actions
260 lines (212 loc) · 7.82 KB
/
Copy pathtest-browser-functionality.js
File metadata and controls
260 lines (212 loc) · 7.82 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
#!/usr/bin/env node
/**
* Browser Functionality Test Script
* Tests all major features of the web browser application
*/
import http from 'http';
import https from 'https';
// Configuration
const FRONTEND_URL = 'http://localhost:3004';
const BACKEND_URL = 'http://localhost:3005';
// Test utilities
function makeRequest(url, timeout = 5000) {
return new Promise((resolve, reject) => {
const client = url.startsWith('https') ? https : http;
const req = client.get(url, { timeout }, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => resolve({
status: res.statusCode,
headers: res.headers,
data: data
}));
});
req.on('error', reject);
req.on('timeout', () => {
req.destroy();
reject(new Error('Request timeout'));
});
});
}
function test(description, testFn) {
console.log(`\n🧪 ${description}`);
return testFn().then(result => {
console.log(`✅ ${description} - PASSED`);
return result;
}).catch(error => {
console.log(`❌ ${description} - FAILED: ${error.message}`);
throw error;
});
}
// Test Suite
async function runTests() {
console.log('🚀 Starting Web Browser Functionality Tests');
console.log('='.repeat(50));
try {
// 1. Frontend Health Check
await test('Frontend server is running', async () => {
const response = await makeRequest(FRONTEND_URL);
if (response.status !== 200) {
throw new Error(`Expected 200, got ${response.status}`);
}
if (!response.data.includes('<!DOCTYPE html>')) {
throw new Error('Frontend not serving HTML');
}
return true;
});
// 2. Backend Health Check
await test('Backend server is running', async () => {
const response = await makeRequest(`${BACKEND_URL}/health`);
const data = JSON.parse(response.data);
if (!data.success || data.status !== 'healthy') {
throw new Error('Backend health check failed');
}
return true;
});
// 3. Security Headers Check
await test('Security headers are present', async () => {
const response = await makeRequest(`${BACKEND_URL}/health`);
const headers = response.headers;
const requiredHeaders = [
'content-security-policy',
'x-frame-options',
'x-content-type-options',
'x-xss-protection',
'referrer-policy'
];
for (const header of requiredHeaders) {
if (!headers[header]) {
throw new Error(`Missing security header: ${header}`);
}
}
return true;
});
// 4. Proxy Service - Valid Website
await test('Proxy service fetches valid websites', async () => {
const response = await makeRequest(`${BACKEND_URL}/api/proxy?url=https://example.com`);
const data = JSON.parse(response.data);
if (!data.success) {
throw new Error(`Proxy failed: ${data.error}`);
}
if (!data.html || !data.html.includes('example')) {
throw new Error('Proxy did not return expected content');
}
return true;
});
// 5. Proxy Service - GitHub Test
await test('Proxy service handles GitHub content', async () => {
const response = await makeRequest(`${BACKEND_URL}/api/proxy?url=https://github.com/microsoft/vscode`, 15000);
const data = JSON.parse(response.data);
if (!data.success) {
throw new Error(`GitHub proxy failed: ${data.error}`);
}
if (!data.html.includes('GitHub') || !data.html.includes('VS Code')) {
throw new Error('GitHub content not properly fetched');
}
// Check that content is sanitized (no script tags)
if (data.html.includes('<script>')) {
throw new Error('Content not properly sanitized');
}
return true;
});
// 6. SSRF Protection - Localhost Blocked
await test('SSRF protection blocks localhost', async () => {
const response = await makeRequest(`${BACKEND_URL}/api/proxy?url=http://localhost:3000`);
const data = JSON.parse(response.data);
if (data.success) {
throw new Error('SSRF protection failed - localhost was not blocked');
}
if (!data.error.includes('blocked')) {
throw new Error('SSRF error message incorrect');
}
return true;
});
// 7. SSRF Protection - Private IP Blocked
await test('SSRF protection blocks private IP ranges', async () => {
const response = await makeRequest(`${BACKEND_URL}/api/proxy?url=http://192.168.1.1`);
const data = JSON.parse(response.data);
if (data.success) {
throw new Error('SSRF protection failed - private IP was not blocked');
}
return true;
});
// 8. Protocol Validation
await test('Protocol validation blocks dangerous URLs', async () => {
const response = await makeRequest(`${BACKEND_URL}/api/proxy?url=javascript:alert(1)`);
const data = JSON.parse(response.data);
if (data.success) {
throw new Error('Protocol validation failed - javascript: URL was not blocked');
}
return true;
});
// 9. Content Sanitization
await test('Content is properly sanitized', async () => {
const response = await makeRequest(`${BACKEND_URL}/api/proxy?url=https://github.com/microsoft/vscode`);
const data = JSON.parse(response.data);
if (!data.success) {
throw new Error('Failed to fetch test content');
}
// Check for dangerous elements
const dangerousPatterns = ['<script', 'javascript:', 'onclick=', 'onerror='];
for (const pattern of dangerousPatterns) {
if (data.html.toLowerCase().includes(pattern)) {
throw new Error(`Dangerous content not sanitized: ${pattern}`);
}
}
return true;
});
// 10. Rate Limiting Check
await test('Rate limiting is implemented', async () => {
// Make multiple rapid requests
const promises = Array(10).fill().map(() =>
makeRequest(`${BACKEND_URL}/api/proxy?url=https://example.com`)
);
const responses = await Promise.allSettled(promises);
const successCount = responses.filter(r =>
r.status === 'fulfilled' && JSON.parse(r.value.data).success
).length;
// At least some requests should succeed
if (successCount === 0) {
throw new Error('All requests were rate limited');
}
return true;
});
// 11. CORS Configuration
await test('CORS configuration is secure', async () => {
const response = await makeRequest(`${BACKEND_URL}/health`);
const corsHeader = response.headers['access-control-allow-origin'];
// Should not be wildcard in production
if (corsHeader === '*') {
console.warn('⚠️ CORS allows all origins - consider restricting');
}
return true;
});
console.log('\n' + '='.repeat(50));
console.log('🎉 ALL TESTS PASSED! 🎉');
console.log('='.repeat(50));
console.log('\n📊 Test Summary:');
console.log('✅ Frontend: Operational');
console.log('✅ Backend: Operational');
console.log('✅ Security Headers: Configured');
console.log('✅ Proxy Service: Working');
console.log('✅ SSRF Protection: Active');
console.log('✅ Content Sanitization: Active');
console.log('✅ Rate Limiting: Active');
console.log('✅ CORS: Configured');
console.log('\n🌐 Browser is ready for use at: http://localhost:3004');
console.log('🔧 Backend API is running at: http://localhost:3005');
return true;
} catch (error) {
console.log('\n' + '='.repeat(50));
console.log('💥 TESTS FAILED 💥');
console.log('='.repeat(50));
console.log(`\n❌ Error: ${error.message}`);
console.log('\nPlease check the servers and try again.');
process.exit(1);
}
}
// Run tests
if (import.meta.url === `file://${process.argv[1]}`) {
runTests();
}
export { runTests };