-
Notifications
You must be signed in to change notification settings - Fork 6
Switch to auth headers #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ import { ReviewHistoryController } from './review-history/reviewHistory.controll | |
import { ChallengeApiService } from 'src/shared/modules/global/challenge.service'; | ||
import { WebhookController } from './webhook/webhook.controller'; | ||
import { WebhookService } from './webhook/webhook.service'; | ||
import { GiteaSignatureGuard } from '../shared/guards/gitea-signature.guard'; | ||
import { GiteaWebhookAuthGuard } from '../shared/guards/gitea-webhook-auth.guard'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import statement has been updated to use |
||
|
||
@Module({ | ||
imports: [HttpModule, GlobalProvidersModule], | ||
|
@@ -36,7 +36,7 @@ import { GiteaSignatureGuard } from '../shared/guards/gitea-signature.guard'; | |
ReviewApplicationService, | ||
ChallengeApiService, | ||
WebhookService, | ||
GiteaSignatureGuard, | ||
GiteaWebhookAuthGuard, | ||
], | ||
}) | ||
export class ApiModule {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ import { | |
WebhookEventDto, | ||
WebhookResponseDto, | ||
} from '../../dto/webhook-event.dto'; | ||
import { GiteaSignatureGuard } from '../../shared/guards/gitea-signature.guard'; | ||
import { GiteaWebhookAuthGuard } from '../../shared/guards/gitea-webhook-auth.guard'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import statement has been updated to use |
||
import { LoggerService } from '../../shared/modules/global/logger.service'; | ||
|
||
@ApiTags('Webhooks') | ||
|
@@ -25,7 +25,7 @@ export class WebhookController { | |
|
||
@Post('gitea') | ||
@HttpCode(HttpStatus.OK) | ||
@UseGuards(GiteaSignatureGuard) | ||
@UseGuards(GiteaWebhookAuthGuard) | ||
@ApiOperation({ | ||
summary: 'Gitea Webhook Endpoint', | ||
description: | ||
|
@@ -42,8 +42,8 @@ export class WebhookController { | |
required: true, | ||
}) | ||
@ApiHeader({ | ||
name: 'X-Hub-Signature-256', | ||
description: 'HMAC-SHA256 signature for request verification', | ||
name: 'authorization', | ||
description: 'Authorization header for Gitea webhook', | ||
required: true, | ||
}) | ||
@ApiResponse({ | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
ForbiddenException, | ||
BadRequestException, | ||
InternalServerErrorException, | ||
} from '@nestjs/common'; | ||
import { Request } from 'express'; | ||
import { LoggerService } from '../modules/global/logger.service'; | ||
|
||
@Injectable() | ||
export class GiteaWebhookAuthGuard implements CanActivate { | ||
private readonly logger = LoggerService.forRoot('GiteaWebhookAuthGuard'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using dependency injection for the LoggerService instead of calling |
||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const request = context.switchToHttp().getRequest<Request>(); | ||
const delivery = request.headers['x-gitea-delivery'] as string; | ||
const event = request.headers['x-gitea-event'] as string; | ||
const authHeader = request.headers['authorization'] as string; | ||
|
||
// Check if GITEA_WEBHOOK_AUTH is configured | ||
const auth = process.env.GITEA_WEBHOOK_AUTH; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be more secure to use a configuration service or a secrets manager to handle sensitive environment variables like |
||
if (!auth) { | ||
this.logger.error( | ||
'GITEA_WEBHOOK_AUTH environment variable is not configured', | ||
); | ||
throw new InternalServerErrorException('Webhook auth not configured'); | ||
} | ||
|
||
if (!delivery) { | ||
this.logger.error('Missing X-Gitea-Delivery header'); | ||
throw new BadRequestException('Missing delivery header'); | ||
} | ||
|
||
if (!event) { | ||
this.logger.error('Missing X-Gitea-Event header'); | ||
throw new BadRequestException('Missing event header'); | ||
} | ||
|
||
try { | ||
// Validate the authorization header | ||
if (!authHeader) { | ||
this.logger.error('Missing Authorization header'); | ||
throw new BadRequestException('Missing authorization header'); | ||
} | ||
|
||
if (authHeader !== `Bearer ${auth}`) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a more secure method for comparing the authorization header, such as a constant-time comparison function, to prevent timing attacks. |
||
this.logger.error('Invalid authorization header'); | ||
throw new ForbiddenException('Invalid authorization'); | ||
} | ||
|
||
this.logger.log( | ||
`Valid webhook authorization verified for delivery ${delivery}, event ${event}`, | ||
); | ||
return true; | ||
} catch (error) { | ||
if ( | ||
error instanceof ForbiddenException || | ||
error instanceof BadRequestException | ||
) { | ||
throw error; | ||
} | ||
|
||
this.logger.error(`Error validating webhook signature: ${error.message}`); | ||
throw new InternalServerErrorException('Signature validation failed'); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider renaming the environment variable
GITEA_WEBHOOK_AUTH
to something more descriptive if it is intended to store an authentication header value, asAUTH
might be too generic and could lead to confusion.