Skip to content

Commit 81ada07

Browse files
Potential fix for code scanning alert no. 89: Incomplete URL substring sanitization
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: Joaquim Verges <[email protected]>
1 parent 669932d commit 81ada07

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

packages/thirdweb/src/insight/get-nfts.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,22 @@ function replaceIPFSGateway(url?: string) {
338338
if (!url || typeof url !== "string") {
339339
return url;
340340
}
341-
if (url.includes("ipfscdn.io")) {
342-
const paths = url.split("/");
343-
const index = paths.findIndex((path) => path === "ipfs");
344-
if (index === -1) {
341+
try {
342+
const parsedUrl = new URL(url);
343+
if (parsedUrl.host === "ipfscdn.io") {
344+
const paths = parsedUrl.pathname.split("/");
345+
const index = paths.findIndex((path) => path === "ipfs");
346+
if (index === -1) {
347+
return url;
348+
}
349+
const ipfsHash = paths.slice(index + 1).join("/");
350+
if (ipfsHash) {
351+
return `ipfs://${ipfsHash}`;
352+
}
345353
return url;
346354
}
347-
const ipfsHash = paths.slice(index + 1).join("/");
348-
if (ipfsHash) {
349-
return `ipfs://${ipfsHash}`;
350-
}
355+
} catch {
356+
// If the URL is invalid, return it as is
351357
return url;
352358
}
353359
return url;

0 commit comments

Comments
 (0)