Skip to content

Commit

Permalink
fix broken bot filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
negrel committed Jun 13, 2024
1 parent f08ca3d commit 8b9bef2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion pkg/handlers/events_pageviews.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ func ProvidePostEventsPageViews(

newSession := !equalBytes(referrerUri.Host(), pageView.PageUri.Host())
if newSession {
// Filter bot.
client := uaParserService.ParseUserAgent(utils.UnsafeString(userAgent))
if client.IsBot {
return fiber.NewError(fiber.StatusBadRequest, "bot session filtered")
}

sessionUuid, err := uuid.NewV7()
if err != nil {
return fmt.Errorf("failed to generate session uuid: %w", err)
Expand All @@ -62,7 +68,7 @@ func ProvidePostEventsPageViews(
pageView.Session = event.Session{
PageUri: &pageView.PageUri,
ReferrerUri: &referrerUri,
Client: uaParserService.ParseUserAgent(utils.UnsafeString(userAgent)),
Client: client,
CountryCode: ipGeolocatorService.FindCountryCodeForIP(c.IP()),
VisitorId: visitorId,
SessionUuid: sessionUuid,
Expand Down
3 changes: 2 additions & 1 deletion pkg/services/uaparser/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ type Client struct {
func (c Client) MarshalZerologObject(e *zerolog.Event) {
e.Str("browser_family", c.BrowserFamily).
Str("operating_system", c.OperatingSystem).
Str("device", c.Device)
Str("device", c.Device).
Bool("is_bot", c.IsBot)
}
13 changes: 13 additions & 0 deletions tests/bun/events/events_pageviews.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ test('internal traffic with no session associated is rejected', async () => {
expect(response.status).toBe(400)
})

test('robot user agent is rejected', async () => {
const response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
headers: {
Origin: 'https://mywebsite.localhost',
'X-Forwarded-For': faker.internet.ip(),
Referer: 'https://mywebsite.localhost/foo?bar=baz#qux',
'User-Agent': 'Googlebot'
}
})
expect(response.status).toBe(400)
})

test('registered domain in Origin header and valid referrer is accepted', async () => {
const response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
Expand Down
1 change: 0 additions & 1 deletion tests/bun/trusted-proxy/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ async function fetchMetrics (): Promise<string> {
}

async function parseMetrics (prometheusMetrics: string): Promise<Metrics> {
console.log(prometheusMetrics)
const metrics: Metrics = {
counter: {},
summary: {},
Expand Down

0 comments on commit 8b9bef2

Please sign in to comment.