-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
149 lines (129 loc) · 5.65 KB
/
server.js
File metadata and controls
149 lines (129 loc) · 5.65 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
import { createServer } from 'node:http';
import { readFile, stat, readdir } from 'node:fs/promises';
import { join, extname, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const PORT = 3000;
const mimeTypes = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.webp': 'image/webp',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
const server = createServer(async (req, res) => {
// Handle CSP violation reports
if (req.method === 'POST' && req.url === '/csp-report') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
try {
// CSP reports come wrapped in a 'csp-report' object
const data = JSON.parse(body);
const report = data['csp-report'] || data;
// Ignore CSP violations from browser extensions
const sourceFile = report['source-file'] || '';
const blockedUri = report['blocked-uri'] || '';
// Filter out browser extensions - they shouldn't be reported
if (sourceFile.includes('moz-extension') ||
sourceFile.includes('chrome-extension') ||
sourceFile.includes('extension:') ||
blockedUri.includes('extension:')) {
// Silently ignore browser extension CSP violations
res.writeHead(204);
res.end();
return;
}
// Only log if there's actual violation data
if (report['document-uri'] || report['violated-directive']) {
console.error('\n❌ CSP Violation Detected:');
console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
if (report['document-uri']) {
console.error(`📋 Document URI: ${report['document-uri']}`);
}
if (report['blocked-uri']) {
console.error(`🚫 Blocked URI: ${report['blocked-uri']}`);
}
if (report['violated-directive']) {
console.error(`⚠️ Violation Type: ${report['violated-directive']}`);
}
if (report['original-policy']) {
console.error(`📊 Original Policy: ${report['original-policy']}`);
}
if (report['source-file']) {
console.error(`📄 Source File: ${report['source-file']}:${report['line-number']}`);
}
console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
}
} catch {
// Intentionally ignore parse errors
// No action needed: malformed CSP reports are discarded silently
}
});
res.writeHead(204);
res.end();
return;
}
res.setHeader(
'Content-Security-Policy',
"default-src 'self'; script-src 'self' 'sha256-BPfo8AlqKcpHrHgw86iS+3zmeiEyidPBzCRVDfmCeaM='; style-src 'self'; font-src 'self'; img-src 'self'; frame-src 'self'; report-uri /csp-report"
);
try {
let filePath = req.url === '/' ? '/index.html' : req.url;
// Retirer les query parameters
filePath = filePath.split('?')[0];
const fullPath = join(__dirname, filePath);
// Récupérer les stats du fichier pour la date de modification
const stats = await stat(fullPath);
if (stats.isDirectory()) {
// Lister le contenu du répertoire
const files = await readdir(fullPath);
let html = `<!DOCTYPE html>
<html>
<head><title>Index of ${filePath}</title></head>
<body>
<h1>Index of ${filePath}</h1>
<ul>`;
for (const file of files) {
const fileStats = await stat(join(fullPath, file));
const mtime = fileStats.mtime.toISOString();
html += `<li><a href="${filePath === '/' ? '' : filePath}/${file}">${file}</a> - ${mtime}</li>`;
}
html += `</ul></body></html>`;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(html);
return;
}
// Lire le fichier
const content = await readFile(fullPath);
const ext = extname(fullPath);
const contentType = mimeTypes[ext] || 'application/octet-stream';
// Envoyer la réponse avec l'en-tête Last-Modified
res.writeHead(200, {
'Content-Type': contentType,
'Last-Modified': stats.mtime.toUTCString(),
'Cache-Control': 'no-cache'
});
res.end(content);
} catch (error) {
if (error.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 Internal Server Error');
}
}
});
server.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});