Skip to content

Commit

Permalink
feat(links): allow Safe Browsing threats to only be logged
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneRifle committed Aug 11, 2020
1 parent 446b7ff commit a57c194
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export const cspOnlyReportViolations =
process.env.CSP_ONLY_REPORT_VIOLATIONS === 'true'
export const cspReportUri = process.env.CSP_REPORT_URI

export const safeBrowsingLogOnly = process.env.SAFE_BROWSING_LOG_ONLY === 'true'

export const cloudmersiveKey: string | undefined = process.env.CLOUDMERSIVE_KEY
export const safeBrowsingKey: string | undefined = process.env.SAFE_BROWSING_KEY

Expand Down
20 changes: 12 additions & 8 deletions src/server/services/SafeBrowsingService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch from 'cross-fetch'
import { injectable } from 'inversify'
import { UrlThreatScanServiceInterface } from './interfaces/UrlThreatScanServiceInterface'
import { logger, safeBrowsingKey } from '../config'
import { logger, safeBrowsingKey, safeBrowsingLogOnly } from '../config'

const ENDPOINT = `https://safebrowsing.googleapis.com/v4/threatMatches:find?key=${safeBrowsingKey}`

Expand Down Expand Up @@ -44,21 +44,25 @@ export class SafeBrowsingService implements UrlThreatScanServiceInterface {
body: JSON.stringify(request),
})
if (!response.ok) {
throw new Error(
const error = new Error(
`Safe Browsing failure:\tError: ${response.statusText}\thttpResponse: ${response}\t body:${response.body}`,
)
if (safeBrowsingLogOnly) {
logger.error(error)
} else {
throw error
}
}
const result = await response.json()
if (result?.matches) {
const prefix = safeBrowsingLogOnly
? 'Considered threat by Safe Browsing but ignoring'
: 'Malicious link content'
logger.warn(
`Malicious link content: ${url} yields ${JSON.stringify(
result.matches,
null,
2,
)}`,
`${prefix}: ${url} yields ${JSON.stringify(result.matches, null, 2)}`,
)
}
return Boolean(result?.matches)
return safeBrowsingLogOnly && Boolean(result?.matches)
}
}

Expand Down

0 comments on commit a57c194

Please sign in to comment.