From 2283f6420403d6c915030c051099d3b24604d5ed Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 6 Aug 2024 00:45:28 +0530 Subject: [PATCH] regex guard around opengraph routes --- src/config.ts | 4 ++++ src/controllers/frontend.ts | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/config.ts b/src/config.ts index 56825cc..86cb05c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -250,6 +250,10 @@ class Conf { static get cronEnabled(): boolean { return optionalBooleanSchema.parse(Deno.env.get('CRON_ENABLED')) ?? true; } + static get opengraphRouteRegex(): string { + return Deno.env.get('OPENGRAPH_ROUTE_REGEX') || + 'googlebot|bingbot|yandex|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterestbot|slackbot|vkShare|W3C_Validator|whatsapp|mastodon|pleroma|Discordbot|AhrefsBot|SEMrushBot|MJ12bot|SeekportBot|Synapse|Matrix'; + } /** Path to the custom policy module. Must be an absolute path, https:, npm:, or jsr: URI. */ static get policy(): string { return Deno.env.get('DITTO_POLICY') || new URL('../data/policy.ts', import.meta.url).pathname; diff --git a/src/controllers/frontend.ts b/src/controllers/frontend.ts index e806b54..726cadf 100644 --- a/src/controllers/frontend.ts +++ b/src/controllers/frontend.ts @@ -18,10 +18,6 @@ const console = new Stickynotes('ditto:frontend'); /** Placeholder to find & replace with metadata. */ const META_PLACEHOLDER = '' as const; -/* - * TODO: implement caching for posts (LRUCache) - */ - async function buildTemplateOpts(params: PathParams, url: string): Promise { const store = await Storages.db(); const meta = await getInstanceMetadata(store); @@ -70,6 +66,10 @@ async function buildTemplateOpts(params: PathParams, url: string): Promise { try { const content = await Deno.readTextFile(new URL('../../public/index.html', import.meta.url)); + const shouldInject = new RegExp(Conf.opengraphRouteRegex, 'i'); + if (!shouldInject) { + return c.html(content); + } if (content.includes(META_PLACEHOLDER)) { const params = getPathParams(c.req.path); if (params) {