From a35c98bbd5fba3a997047468f7bf708169e4b638 Mon Sep 17 00:00:00 2001 From: Francis Dinh Date: Thu, 1 Dec 2022 11:34:11 -0500 Subject: [PATCH] server: encode non-ascii domains in punycode in matchHost --- packages/backend/src/misc/skipped-instances.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/misc/skipped-instances.ts b/packages/backend/src/misc/skipped-instances.ts index 081f883cb..ebedb50d3 100644 --- a/packages/backend/src/misc/skipped-instances.ts +++ b/packages/backend/src/misc/skipped-instances.ts @@ -1,3 +1,4 @@ +import { toASCII } from 'punycode/'; import { db } from '@/db/postgre.js'; import { fetchMeta } from '@/misc/fetch-meta.js'; import { Instance } from '@/models/entities/instance.js'; @@ -9,17 +10,19 @@ const deadThreshold = 7 * DAY; /** * Returns whether a given host matches a wildcard pattern. - * @param host punycoded instance host - * @param pattern wildcard pattern containing a punycoded instance host + * @param host instance host + * @param pattern wildcard pattern containing an instance host * @returns whether the post matches the pattern */ function matchHost(host: Instance['host'], pattern: string): boolean { // Escape all of the regex special characters. Pattern from: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping const escape = (str: string): string => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); - - const re = new RegExp('^' + pattern.split('*').map(escape).join('.*') + '$'); - return re.test(host); + const re = new RegExp('^' + pattern.split('*').map(toASCII).map(escape).join('.*') + '$'); + + // Encode the domain in punycode in case it uses non-ascii + const punycoded = toASCII(host); + return re.test(punycoded); } /** @@ -53,7 +56,7 @@ export async function skippedInstances(hosts: Array): Promise< deadTime.toISOString(), // don't check hosts again that we already know are suspended // also avoids adding duplicates to the list - hosts.filter(host => !skipped.some(blockedHost => matchHost(host, blockedHost)) && !host.includes(',')).join(','), + hosts.filter(host => !skipped.includes(host) && !host.includes(',')).join(','), ], ) .then(res => res.map(row => row.host)),