Add more protections
This commit is contained in:
parent
450de36336
commit
aad45b338e
21
index.js
21
index.js
|
@ -20,12 +20,17 @@ app.get("/:domain/*", (req, res) => {
|
||||||
const domain = req.params.domain;
|
const domain = req.params.domain;
|
||||||
const path = req.params[0];
|
const path = req.params[0];
|
||||||
const args = req.query;
|
const args = req.query;
|
||||||
// Check that domain is equal or subdomain of allowedDomains
|
// If domain has any characters that arent alphanumeric, a period, or -, return 400
|
||||||
if (!allowedDomains.some((allowedDomain) => domain.endsWith(allowedDomain))) {
|
if (!/^[a-zA-Z0-9.-]+$/.test(domain)) {
|
||||||
res.status(403).send("Domain not allowed");
|
return res.status(400).send("Invalid domain");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if domain is valid subdomain of allowedDomains
|
||||||
|
if (!allowedDomains.some((allowedDomain) => domain.endsWith(allowedDomain))) {
|
||||||
|
return res.status(400).send("Invalid domain");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Make the request
|
// Make the request
|
||||||
axios.get(`https://${domain}/${path}`, {
|
axios.get(`https://${domain}/${path}`, {
|
||||||
params: args
|
params: args
|
||||||
|
@ -34,10 +39,18 @@ app.get("/:domain/*", (req, res) => {
|
||||||
res.status(response.status).send(response.data);
|
res.status(response.status).send(response.data);
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
// Send the error back
|
// Send the error back
|
||||||
|
if (!error.response) {
|
||||||
|
return res.status(500).send("An error occurred");
|
||||||
|
}
|
||||||
res.status(error.response.status).send(error.response.data);
|
res.status(error.response.status).send(error.response.data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get("/:domain", (req, res) => {
|
||||||
|
// redirect them to /:domain/
|
||||||
|
res.redirect(`/${req.params.domain}/`);
|
||||||
|
})
|
||||||
|
|
||||||
const port = process.env.SERVER_PORT || 3000;
|
const port = process.env.SERVER_PORT || 3000;
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Server is running on port ${port}`);
|
console.log(`Server is running on port ${port}`);
|
||||||
|
|
Loading…
Reference in a new issue