Kaynağa Gözat

Merge pull request #15429 from overleaf/jel-token-hash-metrics

[web] Log mismatched link sharing token hash prefixes

GitOrigin-RevId: 5ba690f4209987fccef859c858686c465e972f62
Jessica Lawshe 3 yıl önce
ebeveyn
işleme
16e859e40a

+ 13 - 2
services/web/app/src/Features/TokenAccess/TokenAccessHandler.js

@@ -290,17 +290,28 @@ const TokenAccessHandler = {
   checkTokenHashPrefix(token, tokenHashPrefix, type) {
   checkTokenHashPrefix(token, tokenHashPrefix, type) {
     let hashPrefixStatus
     let hashPrefixStatus
 
 
+    if (tokenHashPrefix) {
+      tokenHashPrefix = tokenHashPrefix.replace('#', '')
+    }
+
     if (!tokenHashPrefix) {
     if (!tokenHashPrefix) {
       hashPrefixStatus = 'missing'
       hashPrefixStatus = 'missing'
     } else {
     } else {
-      const hashPrefix = TokenAccessHandler.createTokenHashPrefix(token)
-      if (hashPrefix === tokenHashPrefix.replace('#', '')) {
+      const expectedHashPrefix = TokenAccessHandler.createTokenHashPrefix(token)
+      if (expectedHashPrefix === tokenHashPrefix) {
         hashPrefixStatus = 'match'
         hashPrefixStatus = 'match'
       } else {
       } else {
         hashPrefixStatus = 'mismatch'
         hashPrefixStatus = 'mismatch'
       }
       }
     }
     }
 
 
+    if (hashPrefixStatus === 'mismatch') {
+      logger.info(
+        { tokenHashPrefix, hashPrefixStatus },
+        'mismatched token hash prefix'
+      )
+    }
+
     Metrics.inc('link-sharing.hash-check', {
     Metrics.inc('link-sharing.hash-check', {
       path: type,
       path: type,
       status: hashPrefixStatus,
       status: hashPrefixStatus,

+ 25 - 3
services/web/test/unit/src/TokenAccess/TokenAccessHandlerTests.js

@@ -654,7 +654,11 @@ describe('TokenAccessHandler', function () {
       const token = 'zxpxjrwdtsgd'
       const token = 'zxpxjrwdtsgd'
       const prefix = this.TokenAccessHandler.createTokenHashPrefix(token)
       const prefix = this.TokenAccessHandler.createTokenHashPrefix(token)
 
 
-      this.TokenAccessHandler.checkTokenHashPrefix(token, prefix, 'readOnly')
+      this.TokenAccessHandler.checkTokenHashPrefix(
+        token,
+        `#${prefix}`,
+        'readOnly'
+      )
 
 
       expect(this.Metrics.inc).to.have.been.calledWith(
       expect(this.Metrics.inc).to.have.been.calledWith(
         'link-sharing.hash-check',
         'link-sharing.hash-check',
@@ -667,10 +671,9 @@ describe('TokenAccessHandler', function () {
     it('sends "mismatch" to metrics when prefix does not match the prefix of the hash of the token', function () {
     it('sends "mismatch" to metrics when prefix does not match the prefix of the hash of the token', function () {
       const token = 'zxpxjrwdtsgd'
       const token = 'zxpxjrwdtsgd'
       const prefix = this.TokenAccessHandler.createTokenHashPrefix(token)
       const prefix = this.TokenAccessHandler.createTokenHashPrefix(token)
-
       this.TokenAccessHandler.checkTokenHashPrefix(
       this.TokenAccessHandler.checkTokenHashPrefix(
         'anothertoken',
         'anothertoken',
-        prefix,
+        `#${prefix}`,
         'readOnly'
         'readOnly'
       )
       )
 
 
@@ -681,6 +684,10 @@ describe('TokenAccessHandler', function () {
           status: 'mismatch',
           status: 'mismatch',
         }
         }
       )
       )
+      expect(this.logger.info).to.have.been.calledWith(
+        { tokenHashPrefix: prefix, hashPrefixStatus: 'mismatch' },
+        'mismatched token hash prefix'
+      )
     })
     })
     it('sends "missing" to metrics when prefix is undefined', function () {
     it('sends "missing" to metrics when prefix is undefined', function () {
       this.TokenAccessHandler.checkTokenHashPrefix(
       this.TokenAccessHandler.checkTokenHashPrefix(
@@ -689,6 +696,21 @@ describe('TokenAccessHandler', function () {
         'readOnly'
         'readOnly'
       )
       )
 
 
+      expect(this.Metrics.inc).to.have.been.calledWith(
+        'link-sharing.hash-check',
+        {
+          path: 'readOnly',
+          status: 'missing',
+        }
+      )
+    })
+    it('sends "missing" to metrics when URL hash is sent as "#" only', function () {
+      this.TokenAccessHandler.checkTokenHashPrefix(
+        'anothertoken',
+        '#',
+        'readOnly'
+      )
+
       expect(this.Metrics.inc).to.have.been.calledWith(
       expect(this.Metrics.inc).to.have.been.calledWith(
         'link-sharing.hash-check',
         'link-sharing.hash-check',
         {
         {