Skip to content

Commit

Permalink
refactor(core): Make browserId check mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed Jul 19, 2024
1 parent 0542765 commit e12ab2a
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 40 deletions.
13 changes: 5 additions & 8 deletions packages/cli/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface AuthJwtPayload {
/** This hash is derived from email and bcrypt of password */
hash: string;
/** This is a client generated unique string to prevent session hijacking */
browserId?: string;
browserId: string;
}

interface IssuedJWT extends AuthJwtPayload {
Expand Down Expand Up @@ -83,7 +83,7 @@ export class AuthService {
res.clearCookie(AUTH_COOKIE_NAME);
}

issueCookie(res: Response, user: User, browserId?: string) {
issueCookie(res: Response, user: User, browserId: string) {
// TODO: move this check to the login endpoint in AuthController
// If the instance has exceeded its user quota, prevent non-owners from logging in
const isWithinUsersLimit = this.license.isWithinUsersLimit();
Expand All @@ -104,11 +104,11 @@ export class AuthService {
});
}

issueJWT(user: User, browserId?: string) {
issueJWT(user: User, browserId: string) {
const payload: AuthJwtPayload = {
id: user.id,
hash: this.createJWTHash(user),
browserId: browserId && this.hash(browserId),
browserId: this.hash(browserId),
};
return this.jwtService.sign(payload, {
expiresIn: this.jwtExpiration,
Expand Down Expand Up @@ -140,10 +140,7 @@ export class AuthService {
const endpoint = req.route ? `${req.baseUrl}${req.route.path}` : req.baseUrl;
if (req.method === 'GET' && skipBrowserIdCheckEndpoints.includes(endpoint)) {
this.logger.debug(`Skipped browserId check on ${endpoint}`);
} else if (
jwtPayload.browserId &&
(!req.browserId || jwtPayload.browserId !== this.hash(req.browserId))
) {
} else if (!jwtPayload.browserId || !req.browserId || jwtPayload.browserId !== this.hash(req.browserId)) {

Check failure on line 143 in packages/cli/src/auth/auth.service.ts

View workflow job for this annotation

GitHub Actions / Lint / Lint

Replace `!jwtPayload.browserId·||·!req.browserId·||·jwtPayload.browserId·!==·this.hash(req.browserId)` with `⏎↹↹↹!jwtPayload.browserId·||⏎↹↹↹!req.browserId·||⏎↹↹↹jwtPayload.browserId·!==·this.hash(req.browserId)⏎↹↹`
this.logger.warn(`browserId check failed on ${endpoint}`);
throw new AuthError('Unauthorized');
}
Expand Down
12 changes: 0 additions & 12 deletions packages/cli/src/auth/jwt.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cli/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export type APIRequest<
RequestBody = {},
RequestQuery = {},
> = express.Request<RouteParams, ResponseBody, RequestBody, RequestQuery> & {
browserId?: string;
browserId: string;
};

export type AuthlessRequest<
Expand Down
8 changes: 0 additions & 8 deletions packages/cli/src/services/hooks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ export class HooksService {
return await this.userService.inviteUsers(owner, attributes);
}

/**
* Set the n8n-auth cookie in the response to auto-login
* the user after instance is provisioned
*/
issueCookie(res: Response, user: AuthUser) {
return this.authService.issueCookie(res, user);
}

/**
* Find user in the instance
* 1. To know whether the instance owner is already setup
Expand Down
11 changes: 0 additions & 11 deletions packages/cli/test/unit/services/hooks.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ describe('HooksService', () => {
expect(userService.inviteUsers).toHaveBeenCalledWith(mockedUser, usersToInvite);
});

it('hooksService.issueCookie should call authService.issueCookie', async () => {
// ARRANGE
const res = mock<Response>();

// ACT
hooksService.issueCookie(res, mockedUser);

// ASSERT
expect(authService.issueCookie).toHaveBeenCalledWith(res, mockedUser);
});

it('hooksService.findOneUser should call authUserRepository.findOne', async () => {
// ARRANGE
const filter = { where: { id: '1' } };
Expand Down

0 comments on commit e12ab2a

Please sign in to comment.