Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AnnoyingTechnology/13988f8a3131b0a26f9b4bc00b568334 to your computer and use it in GitHub Desktop.
Save AnnoyingTechnology/13988f8a3131b0a26f9b4bc00b568334 to your computer and use it in GitHub Desktop.
config.ts
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