Skip to content

Commit

Permalink
Add skip safe browsing
Browse files Browse the repository at this point in the history
  • Loading branch information
NayamAmarshe committed Feb 7, 2024
1 parent 360cb9f commit 3598a74
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 38 additions & 33 deletions pages/api/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3598a74

Please sign in to comment.