Skip to content
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

feat(api): Added API_IP_REGEX to match incoming IPs. #82

Merged
merged 1 commit into from
May 16, 2024
Merged
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
15 changes: 12 additions & 3 deletions src/lib/server/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { GetStartTimeFromBody, GetEndTimeFromBody } from "../../../scripts/githu
import Randomstring from "randomstring";
const API_TOKEN = process.env.API_TOKEN;
const API_IP = process.env.API_IP;
const API_IP_REGEX = process.env.API_IP_REGEX;

const GetAllTags = function () {
let tags = [];
Expand Down Expand Up @@ -39,7 +40,7 @@ const CheckIfValidTag = function (tag) {
};
const auth = function (request) {
const authHeader = request.headers.get("authorization");
const authToken = authHeader.replace("Bearer ", "");
const authToken = authHeader?.replace("Bearer ", "");
let ip = "";
try {
//ip can be in x-forwarded-for or x-real-ip or remoteAddress
Expand All @@ -58,8 +59,16 @@ const auth = function (request) {
if (authToken !== API_TOKEN) {
return new Error("invalid token");
}
if (API_IP !== undefined && ip != "" && ip !== API_IP) {
return new Error("invalid ip");
if (API_IP !== undefined && ip != "") {
if (API_IP !== ip) {
return new Error(`invalid ip: ${ip}`);
}
}
if (API_IP_REGEX !== undefined && ip != "") {
const regex = new RegExp(API_IP_REGEX);
if (!regex.test(ip)) {
return new Error(`invalid ip regex: ${ip}`);
}
}
return null;
};
Expand Down