Skip to content

Commit

Permalink
Fixed the sanitizing / DOMXSS warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Lorenz <[email protected]>
  • Loading branch information
JanProgrammierung committed Oct 14, 2024
1 parent 6017a68 commit 2a7377e
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions next-app/src/components/DataSourcesComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ export default function DataSourcesComponent() {
);
}

function sanitizeString(str: string) {
return str.replace(/[^\w\s-]/gi, "");
}

function sanitizeURL(url: string) {
try {
const parsedURL = new URL(url);
// Only allow http and https protocols
if (parsedURL.protocol !== "http:" && parsedURL.protocol !== "https:") {
return "#";
}
return parsedURL.toString();
} catch {
// If URL is invalid, return a safe default
return "#";
}
}

useEffect(() => {
getData();
}, []);
Expand Down Expand Up @@ -245,7 +263,7 @@ export default function DataSourcesComponent() {
<CardHeader className="bg-muted">
<CardTitle className="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
<a
href={item.url}
href={sanitizeURL(item.url)}
target="_blank"
rel="noopener noreferrer"
className="text-xl text-primary hover:underline"
Expand All @@ -254,9 +272,9 @@ export default function DataSourcesComponent() {
</a>
<img
className="float-right w-62 h-12 object-scale-down object-right pl-2"
src={`/img/datasources/${
item.thumbnail.split("/").pop()?.split(".")[0]
}.png`}
src={`/img/datasources/${sanitizeString(
item.thumbnail.split("/").pop()?.split(".")[0] || ""
)}.png`}
alt={item.name}
/>
</CardTitle>
Expand Down

0 comments on commit 2a7377e

Please sign in to comment.