mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-10-16 19:59:26 +00:00
56 lines
2.1 KiB
JavaScript
56 lines
2.1 KiB
JavaScript
// Provenance: simonh1000/angular-http-server (fixed).
|
|||
|
|
// repo: simonh1000/angular-http-server
|
||
|
|
// commit: 8bafc9577161469f5dea01e0b98ea9c525d063e9
|
||
|
|
// parent: d171a771a92e4685b24fb77cb894a7b7b8024c5c
|
||
|
|
// commit_url: https://github.com/simonh1000/angular-http-server/commit/8bafc9577161469f5dea01e0b98ea9c525d063e9
|
||
|
|
// cve:
|
||
|
|
// license: Apache-2.0
|
||
|
|
// function: requestListener
|
||
|
|
// relpath: angular-http-server.js
|
||
|
|
// provenance: human
|
||
|
|
// mechanism: path_join_user_input
|
||
|
|
// upstream_start: 45
|
||
|
|
|
||
|
|
function requestListener(req, res) {
|
||
|
|
// Add CORS header if option chosen
|
||
|
|
if (argv.cors) {
|
||
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||
|
|
res.setHeader('Access-Control-Request-Method', '*');
|
||
|
|
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
|
||
|
|
res.setHeader('Access-Control-Allow-Headers', 'authorization, content-type');
|
||
|
|
// When the request is for CORS OPTIONS (rather than a page) return just the headers
|
||
|
|
if (req.method === 'OPTIONS') {
|
||
|
|
res.writeHead(200);
|
||
|
|
res.end();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Request is for a page instead
|
||
|
|
// Only interested in the part before any query params
|
||
|
|
var url = req.url.split('?')[0]
|
||
|
|
// Attaches path prefix with --path option
|
||
|
|
var possibleFilename = resolveUrl(url.slice(1)) || "dummy";
|
||
|
|
|
||
|
|
var safeFileName = path.normalize(possibleFilename).replace(/^(\.\.[\/\\])+/, '');
|
||
|
|
var safeFullFilename = path.join(__dirname, safeFileName);
|
||
|
|
|
||
|
|
fs.stat(safeFullFilename, function(err, stats) {
|
||
|
|
var fileBuffer;
|
||
|
|
if (!err && stats.isFile()) {
|
||
|
|
fileBuffer = fs.readFileSync(safeFullFilename);
|
||
|
|
let ct = mime.lookup(safeFullFilename);
|
||
|
|
log(`Sending ${safeFullFilename} with Content-Type ${ct}`);
|
||
|
|
res.writeHead(200, { 'Content-Type': ct });
|
||
|
|
|
||
|
|
} else {
|
||
|
|
log("Route %s, replacing with index.html", safeFullFilename);
|
||
|
|
fileBuffer = returnDistFile();
|
||
|
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||
|
|
}
|
||
|
|
|
||
|
|
res.write(fileBuffer);
|
||
|
|
res.end();
|
||
|
|
});
|
||
|
|
}
|