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

Normalize ipfs gateway links to CIDs #83309

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
34 changes: 28 additions & 6 deletions src/clean-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import punycode from "punycode/";
import PhishingDetector from "../src/detector";
import { Config, ExternalKey } from "./types";
import { Config } from "./types";

export const cleanConfig = (config: Config) => {
return cleanAllowlist(cleanBlocklist(config));
Expand Down Expand Up @@ -42,15 +42,17 @@ export const cleanAllowlist = (config: Config): Config => {
};

export const cleanBlocklist = (config: Config): Config => {
// when cleaning the blocklist, we want to remove domains that are:
// - already present on the blocklist through an equal or less specific match
// we also want to:
// when cleaning the blocklist, we want to:
// - remove domains that are already present on the blocklist through an equal or less specific match
// - convert all unicode domains to punycode
// - find IPFS subdomain or subpath gateway links and store only the CIDs
// - remove duplicate entries

const blocklistSet = new Set(config.blacklist);

const newBlocklist = Array.from(blocklistSet)
.filter((domain) => {
// Remove subdomains of blocklisted domains
const parts = domain.split(".");
for (let i = 1; i < parts.length - 1; i++) {
if (blocklistSet.has(parts.slice(i).join("."))) {
Expand All @@ -61,11 +63,31 @@ export const cleanBlocklist = (config: Config): Config => {
return true;
})
.map((domain) => {
return punycode.toASCII(domain);
// Convert to punycode
const punycodeDomain = punycode.toASCII(domain);
return punycodeDomain;
})
.map((domain) => {
// Extract CID from IPFS subdomain gateway links
const ipfsSubdomainMatch = domain.match(/^(.+)\.ipfs\..+$/);
if (ipfsSubdomainMatch) {
return ipfsSubdomainMatch[1];
}

// Extract CID from IPFS subpath gateway links
const ipfsSubpathMatch = domain.match(/^.+\/ipfs\/([^\/]+).*$/);
if (ipfsSubpathMatch) {
return ipfsSubpathMatch[1];
}

return domain;
});

// Remove duplicate entries
const uniqueBlocklist = Array.from(new Set(newBlocklist));

return {
...config,
blacklist: newBlocklist,
blacklist: uniqueBlocklist,
};
};
Loading