Skip to content

feat: whitelist #3939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
renderError,
parseBoolean,
} from "../src/common/utils.js";
import { gistWhitelist } from "../src/common/whitelist.js";
import { isLocaleAvailable } from "../src/translations.js";
import { renderGistCard } from "../src/cards/gist-card.js";
import { fetchGist } from "../src/fetchers/gist-fetcher.js";
Expand All @@ -26,6 +27,19 @@ export default async (req, res) => {

res.setHeader("Content-Type", "image/svg+xml");

if (gistWhitelist && !gistWhitelist.includes(id)) {
return res.send(
renderError("This gist id is not whitelisted", "", {
title_color,
text_color,
bg_color,
border_color,
theme,
show_repo_link: false,
}),
);
}

if (locale && !isLocaleAvailable(locale)) {
return res.send(
renderError("Something went wrong", "Language not found", {
Expand Down
14 changes: 14 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { renderStatsCard } from "../src/cards/stats-card.js";
import { blacklist } from "../src/common/blacklist.js";
import { whitelist } from "../src/common/whitelist.js";
import {
clampValue,
CONSTANTS,
Expand Down Expand Up @@ -53,6 +54,19 @@ export default async (req, res) => {
);
}

if (whitelist && !whitelist.includes(username)) {
return res.send(
renderError("This username is not whitelisted", "", {
title_color,
text_color,
bg_color,
border_color,
theme,
show_repo_link: false,
}),
);
}

if (locale && !isLocaleAvailable(locale)) {
return res.send(
renderError("Something went wrong", "Language not found", {
Expand Down
14 changes: 14 additions & 0 deletions api/pin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { renderRepoCard } from "../src/cards/repo-card.js";
import { blacklist } from "../src/common/blacklist.js";
import { whitelist } from "../src/common/whitelist.js";
import {
clampValue,
CONSTANTS,
Expand Down Expand Up @@ -41,6 +42,19 @@ export default async (req, res) => {
);
}

if (whitelist && !whitelist.includes(username)) {
return res.send(
renderError("This username is not whitelisted", "", {
title_color,
text_color,
bg_color,
border_color,
theme,
show_repo_link: false,
}),
);
}

if (locale && !isLocaleAvailable(locale)) {
return res.send(
renderError("Something went wrong", "Language not found", {
Expand Down
14 changes: 14 additions & 0 deletions api/top-langs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { renderTopLanguages } from "../src/cards/top-languages-card.js";
import { blacklist } from "../src/common/blacklist.js";
import { whitelist } from "../src/common/whitelist.js";
import {
clampValue,
CONSTANTS,
Expand Down Expand Up @@ -48,6 +49,19 @@ export default async (req, res) => {
);
}

if (whitelist && !whitelist.includes(username)) {
return res.send(
renderError("This username is not whitelisted", "", {
title_color,
text_color,
bg_color,
border_color,
theme,
show_repo_link: false,
}),
);
}

if (locale && !isLocaleAvailable(locale)) {
return res.send(renderError("Something went wrong", "Locale not found"));
}
Expand Down
14 changes: 14 additions & 0 deletions api/wakatime.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
parseBoolean,
renderError,
} from "../src/common/utils.js";
import { whitelist } from "../src/common/whitelist.js";
import { fetchWakatimeStats } from "../src/fetchers/wakatime-fetcher.js";
import { isLocaleAvailable } from "../src/translations.js";

Expand Down Expand Up @@ -36,6 +37,19 @@ export default async (req, res) => {

res.setHeader("Content-Type", "image/svg+xml");

if (whitelist && !whitelist.includes(username)) {
return res.send(
renderError("This username is not whitelisted", "", {
title_color,
text_color,
bg_color,
border_color,
theme,
show_repo_link: false,
}),
);
}

if (locale && !isLocaleAvailable(locale)) {
return res.send(
renderError("Something went wrong", "Language not found", {
Expand Down
3 changes: 2 additions & 1 deletion src/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ const renderError = (message, secondaryMessage = "", options = {}) => {
bg_color,
border_color,
theme = "default",
show_repo_link = true,
} = options;

// returns theme based colors with proper overrides and defaults
Expand All @@ -388,7 +389,7 @@ const renderError = (message, secondaryMessage = "", options = {}) => {
ERROR_CARD_LENGTH - 1
}" height="99%" rx="4.5" fill="${bgColor}" stroke="${borderColor}"/>
<text x="25" y="45" class="text">Something went wrong!${
UPSTREAM_API_ERRORS.includes(secondaryMessage)
UPSTREAM_API_ERRORS.includes(secondaryMessage) || !show_repo_link
? ""
: " file an issue at https://tiny.one/readme-stats"
}</text>
Expand Down
10 changes: 10 additions & 0 deletions src/common/whitelist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const whitelist = process.env.WHITELIST
? process.env.WHITELIST.split(",")
: undefined;

const gistWhitelist = process.env.GIST_WHITELIST
? process.env.GIST_WHITELIST.split(",")
: undefined;

export { whitelist, gistWhitelist };
export default whitelist;