Przeglądaj źródła

Merge pull request #12250 from overleaf/ii-add-new-email-blocklist

[web] Prevent Blocklisted domains from appearing as suggestion

GitOrigin-RevId: b02bebe3e043cb264fbdbffce3f783b3af483e95
Jakob Ackermann 3 lat temu
rodzic
commit
223b8aa9d8

+ 4 - 1
services/web/frontend/js/features/settings/components/emails/add-email/input.tsx

@@ -84,6 +84,9 @@ function Input({ onChange, handleAddNewEmail }: InputProps) {
         setMatchedDomain(cachedDomain)
         setMatchedDomain(cachedDomain)
         return
         return
       }
       }
+      if (domainBlocklist.some(d => domain.endsWith(d))) {
+        return
+      }
       const query = `?hostname=${domain}&limit=1`
       const query = `?hostname=${domain}&limit=1`
       getJSON<Nullable<DomainInfo[]>>(`/institutions/domains${query}`, {
       getJSON<Nullable<DomainInfo[]>>(`/institutions/domains${query}`, {
         signal,
         signal,
@@ -92,7 +95,7 @@ function Input({ onChange, handleAddNewEmail }: InputProps) {
           if (!(data && data[0])) {
           if (!(data && data[0])) {
             return
             return
           }
           }
-          if (domainBlocklist.has(data[0].hostname)) {
+          if (domainBlocklist.some(d => data[0].hostname.endsWith(d))) {
             return
             return
           }
           }
           const hostname = data[0]?.hostname
           const hostname = data[0]?.hostname

+ 3 - 3
services/web/frontend/js/features/settings/domain-blocklist.ts

@@ -1,4 +1,4 @@
-const domainBlocklist = new Set(['overleaf.com'])
+const domainBlocklist = ['overleaf.com']
 const commonTLDs = [
 const commonTLDs = [
   'br',
   'br',
   'cn',
   'cn',
@@ -54,8 +54,8 @@ const commonDomains = [
 
 
 for (const domain of commonDomains) {
 for (const domain of commonDomains) {
   for (const tld of commonTLDs) {
   for (const tld of commonTLDs) {
-    domainBlocklist.add(`${domain}.${tld}`)
+    domainBlocklist.push(`${domain}.${tld}`)
   }
   }
 }
 }
 
 
-export default domainBlocklist
+export default domainBlocklist as ReadonlyArray<typeof domainBlocklist[number]>

+ 27 - 5
services/web/test/frontend/features/settings/components/emails/add-email-input.test.tsx

@@ -10,6 +10,7 @@ import fetchMock from 'fetch-mock'
 import Input, {
 import Input, {
   clearDomainCache,
   clearDomainCache,
 } from '../../../../../../frontend/js/features/settings/components/emails/add-email/input'
 } from '../../../../../../frontend/js/features/settings/components/emails/add-email/input'
+import domainBlocklist from '../../../../../../frontend/js/features/settings/domain-blocklist'
 
 
 const testInstitutionData = [
 const testInstitutionData = [
   { university: { id: 124 }, hostname: 'domain.edu' },
   { university: { id: 124 }, hostname: 'domain.edu' },
@@ -215,19 +216,40 @@ describe('<AddEmailInput/>', function () {
     })
     })
 
 
     describe('when there is a match for a blocklisted domain', function () {
     describe('when there is a match for a blocklisted domain', function () {
-      beforeEach(function () {
+      const [blockedDomain] = domainBlocklist
+
+      afterEach(function () {
+        clearDomainCache()
+        fetchMock.reset()
+      })
+
+      it('should not render the suggestion with blocked domain', async function () {
         const blockedInstitution = [
         const blockedInstitution = [
-          { university: { id: 1 }, hostname: 'overleaf.com' },
+          { university: { id: 1 }, hostname: blockedDomain },
         ]
         ]
         fetchMock.get('express:/institutions/domains', blockedInstitution)
         fetchMock.get('express:/institutions/domains', blockedInstitution)
         fireEvent.change(screen.getByRole('textbox'), {
         fireEvent.change(screen.getByRole('textbox'), {
-          target: { value: 'user@o' },
+          target: { value: `user@${blockedDomain.split('.')[0]}` },
         })
         })
+        await fetchMock.flush(true)
+        expect(screen.queryByText(`user@${blockedDomain}`)).to.be.null
       })
       })
 
 
-      it('should not render the suggestion', async function () {
+      it('should not render the suggestion with blocked domain having a subdomain', async function () {
+        const blockedInstitution = [
+          {
+            university: { id: 1 },
+            hostname: `subdomain.${blockedDomain}`,
+          },
+        ]
+        fetchMock.get('express:/institutions/domains', blockedInstitution)
+        fireEvent.change(screen.getByRole('textbox'), {
+          target: {
+            value: `user@subdomain.${blockedDomain.split('.')[0]}`,
+          },
+        })
         await fetchMock.flush(true)
         await fetchMock.flush(true)
-        expect(screen.queryByText('user@overleaf.com')).to.be.null
+        expect(screen.queryByText(`user@subdomain.${blockedDomain}`)).to.be.null
       })
       })
     })
     })