Skip to content

Commit

Permalink
bug[website-builder]: [issue#1057 correct ts file and remove js]
Browse files Browse the repository at this point in the history
  • Loading branch information
aceppaluni committed Aug 13, 2024
1 parent eb2aa99 commit 0e7b5d6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 269 deletions.
1 change: 0 additions & 1 deletion .github/workflows/cron.stale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ jobs:
uses: BoundfoxStudios/[email protected]
with:
last-activity: 14

136 changes: 0 additions & 136 deletions index.js

This file was deleted.

47 changes: 19 additions & 28 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import crypto from "crypto";
import dotenv from "dotenv";

dotenv.config();

// waiting for replies
const app = express();
const port = process.env.PORT || '3333';
const port = process.env.PORT;
const secret = process.env.GITHUB_SECRET;
if(!secret) {
console.error("Error: GITHUB_SECRET environment variable is not set.");
Expand All @@ -20,18 +20,21 @@ let contributorsBuildRequired = false;
let documentationWebsiteBuildTime = 0;
let mindmapBuildTime = 0;
let contributorsBuildTime = 0;
// fixing TS errors when back

app.use(express.json());

app.post("/webhook", async (req: Request, res: Response) => {
console.log("Request received");
const signature = req.headers["x-hub-signature"] as string;

if(!signature) {
throw new Error("Please provide a valid signature")
}

const payload = JSON.stringify(req.body);
const hmac = crypto.createHmac("sha1", secret);
const hmac = crypto.createHmac("sha1", secret)

const calculatedSignature = `sha1=${hmac.update(payload).digest("hex")}`;
console.log("Calculated signature received", calculatedSignature) // need this to stay as number seems to change
const calculatedSignature = `sha1=${hmac.update(payload).digest("hex")}`

if (crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(calculatedSignature))) {
const {result, respMessage} = await getBranchStatus(req.body);
Expand All @@ -43,29 +46,19 @@ app.post("/webhook", async (req: Request, res: Response) => {
});

interface BranchStatus {
result: number | string;
respMessage: string ;
result: number;
respMessage: string;
};

app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});

const sanitize = (cmd: string) => {
const sanitized = cmd.replace(/[;&|`<>$(){}[\]]/g, '');

if(/[\r\n\t]/.test(cmd)) {
throw new Error("Invalid characters in command");
}

return sanitized;
};

const executeCmd = async (cmd: string) => {
try {
const {stdout, stderr} = await exec(sanitize(cmd));
const {stdout, stderr} = await exec(cmd);

Check warning

Code scanning / CodeQL

Indirect uncontrolled command line Medium

This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
This command depends on an unsanitized
environment variable
.
return stderr + "\n" + stdout;
} catch (error: any) {
} catch (error: unknown) {
console.error(`exec error: ${error}`);
throw new Error("Command execution failed. Check logs for details.");
};
Expand All @@ -90,7 +83,7 @@ const getBranchStatus = async (req: Request): Promise<BranchStatus> => {
};

const isUpdateRequired = () => {
const currentTime = Date.now();
const currentTime = Date.now()
const mindMapUpdateInterval = Number.parseInt(process.env.MINDMAP_UPDATE_TIME_INTERVAL ?? "10000");
const documentationWebsiteUpdateInterval = Number.parseInt(process.env.DOCUMENTATION_WEBSITE_UPDATE_TIME_INTERVAL ?? "10000");
isMindmapUpdated = (currentTime - mindmapBuildTime) / 1000 / 60 > mindMapUpdateInterval;
Expand All @@ -101,9 +94,6 @@ const isUpdateRequired = () => {
const buildProject = async (): Promise<{ status: number; message: string }> => {
const currentTime = Date.now();
const contributionUpdateTimeInterval = Number.parseInt(process.env.CONTRIBUTORS_UPDATE_TIME_INTERVAL ?? "10000");
if (!process.env.DOCUMENTATION_WEBSITE_PATH) {
console.log('error')
}
if (!isUpdateRequired()) {
if (contributorsBuildRequired || (currentTime - contributorsBuildTime) / 1000 / 60 > contributionUpdateTimeInterval) {
console.log("No update required, updating the contributors only");
Expand All @@ -120,6 +110,7 @@ const buildProject = async (): Promise<{ status: number; message: string }> => {
console.log("Building Mindmap");
await initiateBuild("npm run build", process.env.MINDMAP_PATH!, process.env.MINDMAP_DEST_PATH!);
mindmapBuildTime = currentTime;
contributorsBuildTime = currentTime;
isMindmapUpdated = false;
}

Expand All @@ -135,8 +126,8 @@ const buildProject = async (): Promise<{ status: number; message: string }> => {
};

const initiateBuild = async (command: string, projectPath: string, destPath: string) => {
await executeCmd(`cd ${sanitize(projectPath)}/ && git pull`);
await executeCmd(`cd ${sanitize(projectPath)}/ && npm ci`);
await executeCmd(`cd ${sanitize(projectPath)}/ && ${sanitize(command)}`);
await executeCmd(`cp -r ${sanitize(projectPath)}/dist/ ${sanitize(destPath)}/`);
await executeCmd(`cd ${(projectPath)}/ && git pull`);
await executeCmd(`cd ${(projectPath)}/ && npm ci`);
await executeCmd(`cd ${(projectPath)}/ && ${(command)}`);
await executeCmd(`cp -r ${(projectPath)}/dist/ ${(destPath)}/`);
};
Loading

0 comments on commit 0e7b5d6

Please sign in to comment.