Created
December 27, 2024 09:30
-
-
Save AnnoyingTechnology/13988f8a3131b0a26f9b4bc00b568334 to your computer and use it in GitHub Desktop.
config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function modifyConfig(config: Config): Config { | |
// Extract allowed domains from config.docs[].startUrl | |
const allowedDomains = new Set<string>(); | |
(config.docs || []).forEach((doc) => { | |
if (doc.startUrl) { | |
try { | |
const parsedUrl = new URL(doc.startUrl); | |
allowedDomains.add(parsedUrl.hostname); | |
} catch (e) { | |
console.log(`Failed to parse startUrl: ${doc.startUrl}`, e); | |
} | |
} | |
}); | |
// Helper function to check if a domain is an AWS Bedrock domain | |
function isAWSBedrockDomain(hostname: string) { | |
// Adjust this check as needed if your AWS Bedrock domain pattern is different | |
return hostname.endsWith("bedrock.amazonaws.com"); | |
} | |
// Save the original fetch | |
const originalFetch = global.fetch || fetch; | |
// Replace the global fetch with our whitelist version | |
(global as any).fetch = async (url: string, init?: RequestInit) => { | |
let allowed = false; | |
try { | |
// Parse the URL to extract its hostname | |
const { hostname } = new URL(url); | |
// Allow request if it’s either an AWS Bedrock domain or in the config’s docs list | |
if (allowedDomains.has(hostname) || isAWSBedrockDomain(hostname)) { | |
allowed = true; | |
} | |
} catch (e) { | |
// If parsing failed, default to blocking; you can adjust this as needed | |
console.log(`Failed to parse URL: ${url}`, e); | |
} | |
if (!allowed) { | |
console.log(`Blocked request to ${url}`); | |
return new Response(`Request blocked by whitelist for ${url}`, { | |
status: 500, | |
}); | |
} | |
// Otherwise, pass along to the real fetch | |
return originalFetch(url, init); | |
}; | |
return config; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment