From 8ca747c02f990e4081e6d2722ca7bbf04da389f9 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Sat, 19 Aug 2023 23:36:47 +0100 Subject: [PATCH] Adds support for Google Safe Browsing in threats API endpoint --- api/threats.js | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/api/threats.js b/api/threats.js index edc987c..7d87ce1 100644 --- a/api/threats.js +++ b/api/threats.js @@ -2,6 +2,36 @@ const axios = require('axios'); const xml2js = require('xml2js'); const middleware = require('./_common/middleware'); +const getGoogleSafeBrowsingResult = async (url) => { + try { + const apiKey = process.env.GOOGLE_CLOUD_API_KEY; + const apiEndpoint = `https://safebrowsing.googleapis.com/v4/threatMatches:find?key=${apiKey}`; + + const requestBody = { + threatInfo: { + threatTypes: [ + 'MALWARE', 'SOCIAL_ENGINEERING', 'UNWANTED_SOFTWARE', 'POTENTIALLY_HARMFUL_APPLICATION', 'API_ABUSE' + ], + platformTypes: ["ANY_PLATFORM"], + threatEntryTypes: ["URL"], + threatEntries: [{ url }] + } + }; + + const response = await axios.post(apiEndpoint, requestBody); + if (response.data && response.data.matches) { + return { + unsafe: true, + details: response.data.matches + }; + } else { + return { unsafe: false }; + } + } catch (error) { + return { error: `Request failed: ${error.message}` }; + } +}; + const getUrlHausResult = async (url) => { let domain = new URL(url).hostname; return await axios({ @@ -52,10 +82,11 @@ const handler = async (url) => { const urlHaus = await getUrlHausResult(url); const phishTank = await getPhishTankResult(url); const cloudmersive = await getCloudmersiveResult(url); - if (urlHaus.error && phishTank.error && cloudmersive.error) { - throw new Error(`All requests failed - ${urlHaus.error} ${phishTank.error} ${cloudmersive.error}`); + const safeBrowsing = await getGoogleSafeBrowsingResult(url); + if (urlHaus.error && phishTank.error && cloudmersive.error && safeBrowsing.error) { + throw new Error(`All requests failed - ${urlHaus.error} ${phishTank.error} ${cloudmersive.error} ${safeBrowsing.error}`); } - return JSON.stringify({ urlHaus, phishTank, cloudmersive }); + return JSON.stringify({ urlHaus, phishTank, cloudmersive, safeBrowsing }); } catch (error) { throw new Error(error.message); }