fix(webhooks): update github webhook validation

This commit is contained in:
Mauricio Siu
2024-09-01 14:51:03 -06:00
parent e609714f1e
commit f05c811bdc
2 changed files with 106 additions and 96 deletions

View File

@@ -44,6 +44,7 @@ export const ShowGitProviders = () => {
</div> </div>
</CardContent> </CardContent>
</Card> </Card>
<div className="grid gap-4 sm:grid-cols-1 md:grid-cols-1">
{data?.map((gitProvider, index) => { {data?.map((gitProvider, index) => {
const isGithub = gitProvider.providerType === "github"; const isGithub = gitProvider.providerType === "github";
const isGitlab = gitProvider.providerType === "gitlab"; const isGitlab = gitProvider.providerType === "gitlab";
@@ -60,7 +61,7 @@ export const ShowGitProviders = () => {
className="space-y-4" className="space-y-4"
key={`${gitProvider.gitProviderId}-${index}`} key={`${gitProvider.gitProviderId}-${index}`}
> >
<Card className="flex sm:flex-row max-sm:gap-2 flex-col justify-between items-center p-4"> <Card className="flex sm:flex-row max-sm:gap-2 flex-col justify-between items-center p-4 h-full">
<div className="flex items-center space-x-4 w-full"> <div className="flex items-center space-x-4 w-full">
{gitProvider.providerType === "github" && ( {gitProvider.providerType === "github" && (
<GithubIcon className="w-6 h-6" /> <GithubIcon className="w-6 h-6" />
@@ -139,5 +140,6 @@ export const ShowGitProviders = () => {
); );
})} })}
</div> </div>
</div>
); );
}; };

View File

@@ -1,6 +1,6 @@
import { findAdmin } from "@/server/api/services/admin"; import { findAdmin } from "@/server/api/services/admin";
import { db } from "@/server/db"; import { db } from "@/server/db";
import { applications, compose } from "@/server/db/schema"; import { applications, compose, githubProvider } from "@/server/db/schema";
import type { DeploymentJob } from "@/server/queues/deployments-queue"; import type { DeploymentJob } from "@/server/queues/deployments-queue";
import { myQueue } from "@/server/queues/queueSetup"; import { myQueue } from "@/server/queues/queueSetup";
import { Webhooks } from "@octokit/webhooks"; import { Webhooks } from "@octokit/webhooks";
@@ -19,18 +19,26 @@ export default async function handler(
return; return;
} }
if (!admin.githubWebhookSecret) { const signature = req.headers["x-hub-signature-256"];
res.status(200).json({ message: "Github Webhook Secret not set" }); const github = req.body;
const githubResult = await db.query.githubProvider.findFirst({
where: eq(githubProvider.githubInstallationId, github.installation.id),
});
if (!githubResult) {
res.status(400).json({ message: "Github Installation not found" });
return; return;
} }
if (!githubResult.githubWebhookSecret) {
res.status(400).json({ message: "Github Webhook Secret not set" });
return;
}
const webhooks = new Webhooks({ const webhooks = new Webhooks({
secret: admin.githubWebhookSecret, secret: githubResult.githubWebhookSecret,
}); });
const signature = req.headers["x-hub-signature-256"];
const github = req.body;
const verified = await webhooks.verify( const verified = await webhooks.verify(
JSON.stringify(github), JSON.stringify(github),
signature as string, signature as string,