fix: prevent webhook errors to be logged

This commit is contained in:
Andras Bacsai 2022-11-29 14:50:24 +01:00
parent b461635834
commit a55720091c
4 changed files with 23 additions and 19 deletions

View File

@ -1486,13 +1486,17 @@ export function makeLabelForServices(type) {
}
export function errorHandler({
status = 500,
message = 'Unknown error.'
message = 'Unknown error.',
type = 'normal'
}: {
status: number;
message: string | any;
type?: string | null;
}) {
if (message.message) message = message.message;
Sentry.captureException(message);
if (type === 'normal') {
Sentry.captureException(message);
}
throw { status, message };
}
export async function generateSshKeyPair(): Promise<{ publicKey: string; privateKey: string }> {

View File

@ -281,7 +281,7 @@ export async function getApplicationFromDBWebhook(projectId: number, branch: str
}
});
if (applications.length === 0) {
throw { status: 500, message: 'Application not configured.' }
throw { status: 500, message: 'Application not configured.', type: 'webhook' }
}
applications = applications.map((application: any) => {
application = decryptApplication(application);
@ -303,8 +303,8 @@ export async function getApplicationFromDBWebhook(projectId: number, branch: str
return applications;
} catch ({ status, message }) {
return errorHandler({ status, message })
} catch ({ status, message, type }) {
return errorHandler({ status, message, type })
}
}
export async function saveApplication(request: FastifyRequest<SaveApplication>, reply: FastifyReply) {

View File

@ -71,7 +71,7 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
const githubEvent = request.headers['x-github-event']?.toString().toLowerCase();
const githubSignature = request.headers['x-hub-signature-256']?.toString().toLowerCase();
if (!allowedGithubEvents.includes(githubEvent)) {
throw { status: 500, message: 'Event not allowed.' }
throw { status: 500, message: 'Event not allowed.', type: 'webhook' }
}
if (githubEvent === 'ping') {
return { pong: 'cool' }
@ -89,7 +89,7 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
branch = body.pull_request.base.ref
}
if (!projectId || !branch) {
throw { status: 500, message: 'Cannot parse projectId or branch from the webhook?!' }
throw { status: 500, message: 'Cannot parse projectId or branch from the webhook?!', type: 'webhook' }
}
const applicationsFound = await getApplicationFromDBWebhook(projectId, branch);
const settings = await prisma.setting.findUnique({ where: { id: '0' } });
@ -107,7 +107,7 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
const checksum = Buffer.from(githubSignature, 'utf8');
//@ts-ignore
if (checksum.length !== digest.length || !crypto.timingSafeEqual(digest, checksum)) {
throw { status: 500, message: 'SHA256 checksum failed. Are you doing something fishy?' }
throw { status: 500, message: 'SHA256 checksum failed. Are you doing something fishy?', type: 'webhook' }
};
}
@ -157,7 +157,7 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
const sourceBranch = body.pull_request.head.ref
const sourceRepository = body.pull_request.head.repo.full_name
if (!allowedActions.includes(pullmergeRequestAction)) {
throw { status: 500, message: 'Action not allowed.' }
throw { status: 500, message: 'Action not allowed.', type: 'webhook' }
}
if (application.settings.previews) {
@ -169,7 +169,7 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
}
);
if (!isRunning) {
throw { status: 500, message: 'Application not running.' }
throw { status: 500, message: 'Application not running.', type: 'webhook' }
}
}
if (
@ -258,8 +258,8 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
}
}
}
} catch ({ status, message }) {
return errorHandler({ status, message })
} catch ({ status, message, type }) {
return errorHandler({ status, message, type })
}
}

View File

@ -44,7 +44,7 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
const allowedActions = ['opened', 'reopen', 'close', 'open', 'update'];
const webhookToken = request.headers['x-gitlab-token'];
if (!webhookToken && !isDev) {
throw { status: 500, message: 'Invalid webhookToken.' }
throw { status: 500, message: 'Invalid webhookToken.', type: 'webhook' }
}
const settings = await prisma.setting.findUnique({ where: { id: '0' } });
if (objectKind === 'push') {
@ -96,10 +96,10 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
const pullmergeRequestId = request.body.object_attributes.iid.toString();
const projectId = Number(id);
if (!allowedActions.includes(action)) {
throw { status: 500, message: 'Action not allowed.' }
throw { status: 500, message: 'Action not allowed.', type: 'webhook' }
}
if (isDraft) {
throw { status: 500, message: 'Draft MR, do nothing.' }
throw { status: 500, message: 'Draft MR, do nothing.', type: 'webhook' }
}
const applicationsFound = await getApplicationFromDBWebhook(projectId, targetBranch);
if (applicationsFound && applicationsFound.length > 0) {
@ -114,11 +114,11 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
}
);
if (!isRunning) {
throw { status: 500, message: 'Application not running.' }
throw { status: 500, message: 'Application not running.', type: 'webhook' }
}
}
if (!isDev && application.gitSource.gitlabApp.webhookToken !== webhookToken) {
throw { status: 500, message: 'Invalid webhookToken. Are you doing something nasty?!' }
throw { status: 500, message: 'Invalid webhookToken. Are you doing something nasty?!', type: 'webhook' }
}
if (
action === 'opened' ||
@ -189,7 +189,7 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
}
}
}
} catch ({ status, message }) {
return errorHandler({ status, message })
} catch ({ status, message, type }) {
return errorHandler({ status, message, type })
}
}