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

[web] Log mismatched link sharing token hash prefixes

GitOrigin-RevId: 5ba690f4209987fccef859c858686c465e972f62
This commit is contained in:
Jessica Lawshe 2023-10-26 08:18:05 -05:00 committed by Copybot
parent 2d552f27e2
commit 16e859e40a
2 changed files with 38 additions and 5 deletions

View file

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

View file

@ -654,7 +654,11 @@ describe('TokenAccessHandler', function () {
const token = 'zxpxjrwdtsgd'
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(
'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 () {
const token = 'zxpxjrwdtsgd'
const prefix = this.TokenAccessHandler.createTokenHashPrefix(token)
this.TokenAccessHandler.checkTokenHashPrefix(
'anothertoken',
prefix,
`#${prefix}`,
'readOnly'
)
@ -681,6 +684,10 @@ describe('TokenAccessHandler', function () {
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 () {
this.TokenAccessHandler.checkTokenHashPrefix(
@ -689,6 +696,21 @@ describe('TokenAccessHandler', function () {
'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(
'link-sharing.hash-check',
{