Skip to content
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

refactor(core): Make browserId check mandatory #10120

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 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 @@ -141,8 +141,9 @@ export class AuthService {
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))
!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