From 3598a74188b3f147a2276a4067a7cebffd47d435 Mon Sep 17 00:00:00 2001 From: Nayam Amarshe <25067102+NayamAmarshe@users.noreply.github.com> Date: Thu, 8 Feb 2024 03:59:19 +0530 Subject: [PATCH] Add skip safe browsing --- .env.local.example | 3 +- README.md | 17 +++++------ package.json | 4 ++- pages/api/create.js | 71 ++++++++++++++++++++++++--------------------- 4 files changed, 51 insertions(+), 44 deletions(-) diff --git a/.env.local.example b/.env.local.example index fb17b8d..5d5c58e 100644 --- a/.env.local.example +++ b/.env.local.example @@ -13,7 +13,8 @@ API_KEY="" SECRET_KEY="" # ENTER YOUR GOOGLE SAFE BROWSING API KEY HERE TO BLOCK MALICIOUS LINKS -SAFE_BROWSING_API_KEY="" +SKIP_SAFE_BROWSING=true +SAFE_BROWSING_API_KEY="" # NO NEED TO ENTER THIS IF SKIP_SAFE_BROWSING IS TRUE # ENTER YOUR WEBSITE DOMAIN NAME HERE, THE LAST SLASH IS IMPORTANT NEXT_PUBLIC_BASE_URL="http://website.com/" \ No newline at end of file diff --git a/README.md b/README.md index ed45821..3897d4d 100644 --- a/README.md +++ b/README.md @@ -55,17 +55,16 @@ npm run dev **DOCKER:** -``` +> [!IMPORTANT] +> Make sure the .env.local file is available before the build. + +```bash # BUILD AND RUN IMAGE USING NPM +npm run docker:build +# RUN IMAGE npm run docker -``` - -OR - -``` -# MANUALLY BUILD AND RUN IMAGE USING COMPOSE -sudo docker-compose build -sudo docker-compose up -d +# OR RUN IN DETACHED MODE +npm run docker:d ``` Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. diff --git a/package.json b/package.json index 6296720..538d869 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,9 @@ "build": "next build", "start": "next start", "lint": "next lint", - "docker": "docker-compose build && docker-compose up -d" + "docker:build": "docker-compose build", + "docker": "docker-compose up", + "docker:d": "docker-compose up -d" }, "dependencies": { "crypto-js": "^4.2.0", diff --git a/pages/api/create.js b/pages/api/create.js index e7bd981..a268aa0 100644 --- a/pages/api/create.js +++ b/pages/api/create.js @@ -52,43 +52,48 @@ export default async function handler(req, res) { }); } - try { - const response = await fetch( - "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=" + apiKey, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - client: { - clientId: "maglit-website", - clientVersion: "1.0.0", - }, - threatInfo: { - threatTypes: [ - "MALWARE", - "SOCIAL_ENGINEERING", - "UNWANTED_SOFTWARE", - "POTENTIALLY_HARMFUL_APPLICATION", - ], - platformTypes: ["ANY_PLATFORM"], - threatEntryTypes: ["URL"], - threatEntries: [{ url: `${link}` }], + if (process.env.SKIP_SAFE_BROWSING === "true") { + console.log("Skipping safe browsing check"); + } else { + try { + const response = await fetch( + "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=" + + apiKey, + { + method: "POST", + headers: { + "Content-Type": "application/json", }, - }), - }, - ); + body: JSON.stringify({ + client: { + clientId: "maglit-website", + clientVersion: "1.0.0", + }, + threatInfo: { + threatTypes: [ + "MALWARE", + "SOCIAL_ENGINEERING", + "UNWANTED_SOFTWARE", + "POTENTIALLY_HARMFUL_APPLICATION", + ], + platformTypes: ["ANY_PLATFORM"], + threatEntryTypes: ["URL"], + threatEntries: [{ url: `${link}` }], + }, + }), + }, + ); - const data = await response.json(); - console.log("🚀 => data:", data); + const data = await response.json(); + console.log("🚀 => data:", data); - if (data && data?.matches?.length > 0) { - // Handle error cases where the URL might not be checked by Safe Browsing - res.status(401).json({ message: "Malicious link entered!" }); + if (data && data?.matches?.length > 0) { + // Handle error cases where the URL might not be checked by Safe Browsing + res.status(401).json({ message: "Malicious link entered!" }); + } + } catch (error) { + res.status(500).json({ error: "Failed to check the URL." }); } - } catch (error) { - res.status(500).json({ error: "Failed to check the URL." }); } try {