Skip to content

Commit

Permalink
fix(core): Use timing safe function to compare runner auth tokens
Browse files Browse the repository at this point in the history
It is highly unlikely this could be utilized for anything, since these
endpoints are available only in local network.
  • Loading branch information
tomi committed Jan 7, 2025
1 parent ecabe34 commit f81338f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ describe('TaskRunnerAuthService', () => {

describe('isValidAuthToken', () => {
it('should be valid for the configured token', () => {
expect(authService.isValidAuthToken('random-secret'));
expect(authService.isValidAuthToken('random-secret')).toBe(true);
});

it('should be invalid for anything else', () => {
expect(authService.isValidAuthToken('!random-secret'));
expect(authService.isValidAuthToken('!random-secret')).toBe(false);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GlobalConfig } from '@n8n/config';
import { Service } from '@n8n/di';
import { randomBytes } from 'crypto';
import { randomBytes, timingSafeEqual } from 'crypto';

import { Time } from '@/constants';
import { CacheService } from '@/services/cache/cache.service';
Expand All @@ -9,6 +9,8 @@ const GRANT_TOKEN_TTL = 15 * Time.seconds.toMilliseconds;

@Service()
export class TaskRunnerAuthService {
private readonly authToken = Buffer.from(this.globalConfig.taskRunners.authToken);

constructor(
private readonly globalConfig: GlobalConfig,
private readonly cacheService: CacheService,
Expand All @@ -17,7 +19,10 @@ export class TaskRunnerAuthService {
) {}

isValidAuthToken(token: string) {
return token === this.globalConfig.taskRunners.authToken;
const tokenBuffer = Buffer.from(token);
if (tokenBuffer.length !== this.authToken.length) return false;

return timingSafeEqual(tokenBuffer, this.authToken);
}

/**
Expand Down

0 comments on commit f81338f

Please sign in to comment.