fix: prevent webhook errors to be logged
This commit is contained in:
@@ -1486,13 +1486,17 @@ export function makeLabelForServices(type) {
|
|||||||
}
|
}
|
||||||
export function errorHandler({
|
export function errorHandler({
|
||||||
status = 500,
|
status = 500,
|
||||||
message = 'Unknown error.'
|
message = 'Unknown error.',
|
||||||
|
type = 'normal'
|
||||||
}: {
|
}: {
|
||||||
status: number;
|
status: number;
|
||||||
message: string | any;
|
message: string | any;
|
||||||
|
type?: string | null;
|
||||||
}) {
|
}) {
|
||||||
if (message.message) message = message.message;
|
if (message.message) message = message.message;
|
||||||
|
if (type === 'normal') {
|
||||||
Sentry.captureException(message);
|
Sentry.captureException(message);
|
||||||
|
}
|
||||||
throw { status, message };
|
throw { status, message };
|
||||||
}
|
}
|
||||||
export async function generateSshKeyPair(): Promise<{ publicKey: string; privateKey: string }> {
|
export async function generateSshKeyPair(): Promise<{ publicKey: string; privateKey: string }> {
|
||||||
|
@@ -281,7 +281,7 @@ export async function getApplicationFromDBWebhook(projectId: number, branch: str
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (applications.length === 0) {
|
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) => {
|
applications = applications.map((application: any) => {
|
||||||
application = decryptApplication(application);
|
application = decryptApplication(application);
|
||||||
@@ -303,8 +303,8 @@ export async function getApplicationFromDBWebhook(projectId: number, branch: str
|
|||||||
|
|
||||||
return applications;
|
return applications;
|
||||||
|
|
||||||
} catch ({ status, message }) {
|
} catch ({ status, message, type }) {
|
||||||
return errorHandler({ status, message })
|
return errorHandler({ status, message, type })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export async function saveApplication(request: FastifyRequest<SaveApplication>, reply: FastifyReply) {
|
export async function saveApplication(request: FastifyRequest<SaveApplication>, reply: FastifyReply) {
|
||||||
|
@@ -71,7 +71,7 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
|
|||||||
const githubEvent = request.headers['x-github-event']?.toString().toLowerCase();
|
const githubEvent = request.headers['x-github-event']?.toString().toLowerCase();
|
||||||
const githubSignature = request.headers['x-hub-signature-256']?.toString().toLowerCase();
|
const githubSignature = request.headers['x-hub-signature-256']?.toString().toLowerCase();
|
||||||
if (!allowedGithubEvents.includes(githubEvent)) {
|
if (!allowedGithubEvents.includes(githubEvent)) {
|
||||||
throw { status: 500, message: 'Event not allowed.' }
|
throw { status: 500, message: 'Event not allowed.', type: 'webhook' }
|
||||||
}
|
}
|
||||||
if (githubEvent === 'ping') {
|
if (githubEvent === 'ping') {
|
||||||
return { pong: 'cool' }
|
return { pong: 'cool' }
|
||||||
@@ -89,7 +89,7 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
|
|||||||
branch = body.pull_request.base.ref
|
branch = body.pull_request.base.ref
|
||||||
}
|
}
|
||||||
if (!projectId || !branch) {
|
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 applicationsFound = await getApplicationFromDBWebhook(projectId, branch);
|
||||||
const settings = await prisma.setting.findUnique({ where: { id: '0' } });
|
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');
|
const checksum = Buffer.from(githubSignature, 'utf8');
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
if (checksum.length !== digest.length || !crypto.timingSafeEqual(digest, checksum)) {
|
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 sourceBranch = body.pull_request.head.ref
|
||||||
const sourceRepository = body.pull_request.head.repo.full_name
|
const sourceRepository = body.pull_request.head.repo.full_name
|
||||||
if (!allowedActions.includes(pullmergeRequestAction)) {
|
if (!allowedActions.includes(pullmergeRequestAction)) {
|
||||||
throw { status: 500, message: 'Action not allowed.' }
|
throw { status: 500, message: 'Action not allowed.', type: 'webhook' }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (application.settings.previews) {
|
if (application.settings.previews) {
|
||||||
@@ -169,7 +169,7 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (!isRunning) {
|
if (!isRunning) {
|
||||||
throw { status: 500, message: 'Application not running.' }
|
throw { status: 500, message: 'Application not running.', type: 'webhook' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
@@ -258,8 +258,8 @@ export async function gitHubEvents(request: FastifyRequest<GitHubEvents>): Promi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch ({ status, message }) {
|
} catch ({ status, message, type }) {
|
||||||
return errorHandler({ status, message })
|
return errorHandler({ status, message, type })
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -44,7 +44,7 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
|
|||||||
const allowedActions = ['opened', 'reopen', 'close', 'open', 'update'];
|
const allowedActions = ['opened', 'reopen', 'close', 'open', 'update'];
|
||||||
const webhookToken = request.headers['x-gitlab-token'];
|
const webhookToken = request.headers['x-gitlab-token'];
|
||||||
if (!webhookToken && !isDev) {
|
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' } });
|
const settings = await prisma.setting.findUnique({ where: { id: '0' } });
|
||||||
if (objectKind === 'push') {
|
if (objectKind === 'push') {
|
||||||
@@ -96,10 +96,10 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
|
|||||||
const pullmergeRequestId = request.body.object_attributes.iid.toString();
|
const pullmergeRequestId = request.body.object_attributes.iid.toString();
|
||||||
const projectId = Number(id);
|
const projectId = Number(id);
|
||||||
if (!allowedActions.includes(action)) {
|
if (!allowedActions.includes(action)) {
|
||||||
throw { status: 500, message: 'Action not allowed.' }
|
throw { status: 500, message: 'Action not allowed.', type: 'webhook' }
|
||||||
}
|
}
|
||||||
if (isDraft) {
|
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);
|
const applicationsFound = await getApplicationFromDBWebhook(projectId, targetBranch);
|
||||||
if (applicationsFound && applicationsFound.length > 0) {
|
if (applicationsFound && applicationsFound.length > 0) {
|
||||||
@@ -114,11 +114,11 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (!isRunning) {
|
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) {
|
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 (
|
if (
|
||||||
action === 'opened' ||
|
action === 'opened' ||
|
||||||
@@ -189,7 +189,7 @@ export async function gitLabEvents(request: FastifyRequest<GitLabEvents>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch ({ status, message }) {
|
} catch ({ status, message, type }) {
|
||||||
return errorHandler({ status, message })
|
return errorHandler({ status, message, type })
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user