mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
Cleanup data validation
This commit is contained in:
parent
8476b97f7d
commit
a83d105764
@ -67,6 +67,10 @@ export default async function credentialedProxyHandler(req, res, map) {
|
|||||||
|
|
||||||
let resultData = data;
|
let resultData = data;
|
||||||
|
|
||||||
|
if (resultData.error?.url) {
|
||||||
|
resultData.error.url = sanitizeErrorURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
if (status === 204 || status === 304) {
|
if (status === 204 || status === 304) {
|
||||||
return res.status(status).end();
|
return res.status(status).end();
|
||||||
}
|
}
|
||||||
@ -75,15 +79,11 @@ export default async function credentialedProxyHandler(req, res, map) {
|
|||||||
logger.error("HTTP Error %d calling %s", status, url.toString());
|
logger.error("HTTP Error %d calling %s", status, url.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!validateWidgetData(widget, endpoint, data)) {
|
if (status === 200) {
|
||||||
if (data.error && data.error.url) {
|
if (!validateWidgetData(widget, endpoint, resultData)) {
|
||||||
data.error.url = sanitizeErrorURL(url);
|
return res.status(500).json({error: {message: "Invalid data", url: sanitizeErrorURL(url), data: resultData}});
|
||||||
}
|
}
|
||||||
return res.status(500).json({error: {message: "Invalid data", url: sanitizeErrorURL(url), data}});
|
if (map) resultData = map(resultData);
|
||||||
}
|
|
||||||
|
|
||||||
if (status === 200 && map) {
|
|
||||||
resultData = map(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contentType) res.setHeader("Content-Type", contentType);
|
if (contentType) res.setHeader("Content-Type", contentType);
|
||||||
|
@ -39,15 +39,15 @@ export default async function genericProxyHandler(req, res, map) {
|
|||||||
|
|
||||||
let resultData = data;
|
let resultData = data;
|
||||||
|
|
||||||
if (!validateWidgetData(widget, endpoint, resultData)) {
|
if (resultData.error?.url) {
|
||||||
if (resultData.error && resultData.error.url) {
|
|
||||||
resultData.error.url = sanitizeErrorURL(url);
|
resultData.error.url = sanitizeErrorURL(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (status === 200) {
|
||||||
|
if (!validateWidgetData(widget, endpoint, resultData)) {
|
||||||
return res.status(status).json({error: {message: "Invalid data", url: sanitizeErrorURL(url), data: resultData}});
|
return res.status(status).json({error: {message: "Invalid data", url: sanitizeErrorURL(url), data: resultData}});
|
||||||
}
|
}
|
||||||
|
if (map) resultData = map(resultData);
|
||||||
if (status === 200 && map) {
|
|
||||||
resultData = map(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contentType) res.setHeader("Content-Type", contentType);
|
if (contentType) res.setHeader("Content-Type", contentType);
|
||||||
@ -58,7 +58,7 @@ export default async function genericProxyHandler(req, res, map) {
|
|||||||
|
|
||||||
if (status >= 400) {
|
if (status >= 400) {
|
||||||
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
|
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
|
||||||
return res.status(status).json({error: {message: "HTTP Error", url: sanitizeErrorURL(url), data}});
|
return res.status(status).json({error: {message: "HTTP Error", url: sanitizeErrorURL(url), resultData}});
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(status).send(resultData);
|
return res.status(status).send(resultData);
|
||||||
|
@ -1,24 +1,34 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
import widgets from "widgets/widgets";
|
import widgets from "widgets/widgets";
|
||||||
|
|
||||||
export default function validateWidgetData(widget, endpoint, data) {
|
export default function validateWidgetData(widget, endpoint, data) {
|
||||||
let valid = true;
|
let valid = true;
|
||||||
let dataParsed = data;
|
let dataParsed = data;
|
||||||
|
let error;
|
||||||
if (Buffer.isBuffer(data)) {
|
if (Buffer.isBuffer(data)) {
|
||||||
try {
|
try {
|
||||||
dataParsed = JSON.parse(data);
|
dataParsed = JSON.parse(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
error = e;
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataParsed && Object.entries(dataParsed).length) {
|
if (dataParsed && Object.entries(dataParsed).length) {
|
||||||
const validate = widgets[widget.type]?.mappings?.[endpoint]?.validate;
|
const mappings = widgets[widget.type]?.mappings;
|
||||||
validate?.forEach(key => {
|
if (mappings) {
|
||||||
|
const mapping = Object.values(mappings).find(m => m.endpoint === endpoint);
|
||||||
|
mapping?.validate?.forEach(key => {
|
||||||
if (dataParsed[key] === undefined) {
|
if (dataParsed[key] === undefined) {
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!valid) {
|
||||||
|
console.warn(`Invalid data for widget '${widget.type}' endpoint '${endpoint}':\nParse error: ${error ?? "none"}\nData: ${JSON.stringify(data, null, " ")}`);
|
||||||
|
}
|
||||||
|
|
||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user