Skip to content

Commit

Permalink
Fix bot user agent detection (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c authored Oct 18, 2023
1 parent ad760ad commit b884051
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ describe('loadScript', () => {
expect(_isBlockedUA(randomisedUserAgent, ['testington'])).toBe(true)
}
)

it('should block googlebot desktop', () => {
expect(
_isBlockedUA(
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/W.X.Y.Z Safari/537.36',
[]
)
).toBe(true)
})

it('should block openai bot', () => {
expect(
_isBlockedUA(
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; GPTBot/1.0; +https://openai.com/gptbot)',
[]
)
).toBe(true)
})
})

describe('_isUrlMatchingRegex', () => {
Expand Down
13 changes: 9 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,17 @@ export const DEFAULT_BLOCKED_UA_STRS = [
// 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 {
return DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents).some((blockedUA) => {
if (ua.includes) {
return ua.includes(blockedUA)
if (!ua) {
return false
}
const uaLower = ua.toLowerCase()
return DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents || []).some((blockedUA) => {
const blockedUaLower = blockedUA.toLowerCase()
if (uaLower.includes) {
return uaLower.includes(blockedUaLower)
} else {
// IE 11 :/
return ua.indexOf(blockedUA) !== -1
return uaLower.indexOf(blockedUaLower) !== -1
}
})
}
Expand Down

0 comments on commit b884051

Please sign in to comment.