Skip to content

Commit

Permalink
FIX - Add check to public address of the application when checking if…
Browse files Browse the repository at this point in the history
… the client is GitHub
  • Loading branch information
Juansecu committed Feb 25, 2024
1 parent ed25533 commit a3ed3a4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/environment-variables.checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { shouldUseHttps } from './utils/get-protocol.util';

export function environmentVariablesChecker(): void {
if (process.env.PORT) checkPort();

checkHttpsConfig();
checkPublicHostAddress();
}

function checkHttpsConfig(): void {
Expand All @@ -19,3 +21,8 @@ function checkPort(): void {
if (isNaN(Number(process.env.PORT)))
throw new Error('PORT environment variable must be a number');
}

function checkPublicHostAddress(): void {
if (!process.env.PUBLIC_HOST_ADDRESS)
throw new Error('PUBLIC_HOST_ADDRESS environment variable must be set');
}
9 changes: 7 additions & 2 deletions src/middlewares/check-whether-client-is-github.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Logger } from 'winston';

import { ConsoleLogger } from '../loggers/console.logger';

import { getHostAddress } from '../utils/get-host-address.util';

export function checkIfClientIsGitHub(
request: Request,
response: Response,
Expand All @@ -17,7 +19,10 @@ export function checkIfClientIsGitHub(
if (process.env.NODE_ENV === 'production') {
consoleLogger.info(`Client host: ${request.get('host')}`);

if (request.get('host') !== 'api.github.com') {
if (
request.get('host') !== 'api.github.com' &&
request.get('host') !== getHostAddress()
) {
consoleLogger.error('Client is not GitHub');
response.status(403).send('Forbidden');
return;
Expand All @@ -36,7 +41,7 @@ export function checkIfClientIsGitHub(
return;
}

consoleLogger.verbose('Client is GitHub');
consoleLogger.info('Client is GitHub');

next();
}
3 changes: 2 additions & 1 deletion src/middlewares/logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextFunction, Request, Response } from 'express';
import { Logger } from 'winston';

import { ConsoleLogger } from '../loggers/console.logger';

export function logger(
Expand All @@ -9,7 +10,7 @@ export function logger(
): void {
const consoleLogger: Logger = ConsoleLogger.getLogger(logger.name);

consoleLogger.verbose(
consoleLogger.info(
`Request received: ${request.method} ${request.originalUrl} from ${request.ip}`
);

Expand Down
2 changes: 1 addition & 1 deletion src/utils/get-host-address.util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function getHostAddress(): string {
return process.env.PUBLIC_HOST_ADDRESS ?? 'localhost';
return process.env.PUBLIC_HOST_ADDRESS!;
}

0 comments on commit a3ed3a4

Please sign in to comment.