Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Sep 5, 2023
1 parent 67d0578 commit 6fd714a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
19 changes: 18 additions & 1 deletion src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
* currently not supported in the browser lib).
*/

import { _copyAndTruncateStrings, _info, loadScript } from '../utils'
import { _copyAndTruncateStrings, _info, _isBlockedUA, DEFAULT_BLOCKED_UA_STRS, loadScript } from '../utils'

function userAgentFor(botString) {
const randOne = (Math.random() + 1).toString(36).substring(7)
const randTwo = (Math.random() + 1).toString(36).substring(7)
return `Mozilla/5.0 (compatible; ${botString}/${randOne}; +http://a.com/bot/${randTwo})`
}

describe(`utils.js`, () => {
it('should have $host and $pathname in properties', () => {
Expand Down Expand Up @@ -208,4 +214,15 @@ describe('loadScript', () => {
new_script.onerror('uh-oh')
expect(callback).toHaveBeenCalledWith('uh-oh')
})

describe('user agent blocking', () => {
it.each(DEFAULT_BLOCKED_UA_STRS.concat('testington'))(
'blocks a bot based on the user agent %s',
(botString) => {
const randomisedUserAgent = userAgentFor(botString)

expect(_isBlockedUA(randomisedUserAgent, ['testington'])).toBe(true)
}
)
})
})
13 changes: 4 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export const _utf8Encode = function (string: string): string {
return utftext
}

const BLOCKED_UA_STRS = [
export const DEFAULT_BLOCKED_UA_STRS = [
'ahrefsbot',
'applebot',
'baiduspider',
Expand Down Expand Up @@ -503,18 +503,13 @@ const BLOCKED_UA_STRS = [
'storebot-google',
]

let botRegex: RegExp | null = null
// _.isBlockedUA()
// This is to block various web spiders from executing our JS and
// sending false capturing data
export const _isBlockedUA = function (ua: string, customBlockedUserAgents: string[]): boolean {
if (botRegex === null) {
// convert BLOCKED_UA_STRS to a regex like bot.php|hubspot|crawler|prerender etc.:
const joinedBots = BLOCKED_UA_STRS.concat(customBlockedUserAgents).join('|')
botRegex = new RegExp(joinedBots, 'i')
}

return !botRegex.test(ua)
return DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents).some((blockedUA) => {
return ua.includes(blockedUA)
})
}

/**
Expand Down

0 comments on commit 6fd714a

Please sign in to comment.