Skip to content

Commit

Permalink
Merge pull request #82 from rajnandan1/ip_api_fixes
Browse files Browse the repository at this point in the history
feat(api): Added API_IP_REGEX to match incoming IPs.
  • Loading branch information
rajnandan1 authored May 16, 2024
2 parents 55550ad + 4916a6b commit a267c40
Showing 1 changed file with 12 additions and 3 deletions.
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

0 comments on commit a267c40

Please sign in to comment.