Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling and response messages to viewsource function #756

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions api/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,24 @@ app.get('/allscans', async (req, res) => {
});

app.get('/viewsource', async (req, res) => {
const resp = await fetch(req.query.url);
const source = await resp.text();
res.send(source);
const target = new URL(req.query.url);
const functionHost = '-sswlinkauditor-c1131.cloudfunctions.net';

// Disallow fetching from same host to prevent request forgery
if (target.hostname.includes(functionHost) || target.hostname === 'localhost') {
res.send('Cannot fetch from internal host');
return;
}

const resp = await fetch(target.href).catch((err) => {
res.send(`Failed to load source: ${err.message}`);
});
if (resp.ok) {
const source = await resp.text();
res.send(source);
} else {
res.send(`Failed to load source: ${resp.status} - ${resp.statusText}`);
}
});

app.get('/run/:runId', async (req, res) => {
Expand Down
Loading