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

feat(service-bff): Integrate encryption for session cookies #17277

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

snaerth
Copy link
Member

@snaerth snaerth commented Dec 18, 2024

Integrate encryption for session cookies

What

Adds encryption for session IDs in the authentication flow to enhance security.

  • Updates tests to reflect changes in session handling since encrypted session is used consistently across controllers and services.
  • Updates CryptoService and introduces SessionCookieService to manage encryption and session cookie operations.
  • Remove session id from logs

Why

Currently if sid is leaked, then anyone could call the user endpoint and get information about the associated user.

TODO

  • SMOKE TEST
  • Update cache service spec
  • Create sessionCookie service test

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Formatting passes locally with my changes
  • I have rebased against main before asking for a review

Summary by CodeRabbit

Release Notes

  • Security Enhancements

    • Introduced enhanced session cookie management with encryption.
    • Improved handling of sensitive session identifiers across services.
  • New Features

    • Added SessionCookieService to centralize cookie management.
    • Introduced CryptoKeyService for cryptographic key management.
  • Improvements

    • Streamlined error logging and cookie handling.
    • Updated services to utilize centralized session cookie management.
    • Enhanced cryptography service for better key validation and management.
  • Testing

    • Expanded test coverage for cryptography and session management.

Implements the BffDoubleSessionModal to handle cases where a user 
has multiple active sessions. This modal provides options to switch 
users or keep the current session. 

Enhances error handling in the AuthService by replacing generic 
error messages with more specific ones and adding a new method 
to manage missing login attempts, improving user experience 
during login failures.
Update the BffError class to use a number type for the 
statusCode instead of a string. Modify the constructor 
and related methods to ensure consistent handling of status 
codes as numbers. This change improves type safety and 
prevents potential issues with string-to-number conversions.
Refactor error handling introducing a new enum for BFF error codes. 
Update error messages and status codes for clarity. 
Enhance user session management by ensuring proper handling of 
multiple active sessions. 
Implement a method to remove BFF error parameters from the URL 
to improve user experience. 
Adjust button click handlers to use arrow functions for consistency.
Enhance the cache retrieval logic in the AuthController test to 
improve readability. Update the documentation in AuthService to 
provide clearer explanations of scenarios where a login attempt 
cache entry may not be found. This includes specifying the 
conditions for cache expiration and the recovery process for 
login attempts, ensuring better understanding for future 
developers.
Remove unnecessary console logs during login redirection and 
directly set the window location for a cleaner flow. Enhance 
the button wrapper styles for better responsiveness and 
alignment in the BffDoubleSession component.
Adds encryption for session IDs in the authentication flow to enhance 
security. Updates tests to reflect changes in ID handling, during that encrypted session are used consistently across 
controllers and services. Introduces `CryptoService` and 
`SessionCookieService` to manage encryption and session cookie 
operations.
@snaerth snaerth requested a review from a team as a code owner December 18, 2024 10:10
Copy link
Contributor

coderabbitai bot commented Dec 18, 2024

Walkthrough

This pull request introduces a comprehensive refactoring of session management across multiple services and modules in the BFF (Backend for Frontend) application. The primary focus is on enhancing session security by implementing a new SessionCookieService that centralizes cookie handling and integrating CryptoKeyService for managing cryptographic keys. The changes span across authentication, proxy, user, and error services, introducing a more consistent and secure approach to managing session cookies.

Changes

File Change Summary
auth/auth.controller.spec.ts Enhanced session handling with SessionCookieService, updated session ID handling in tests.
auth/auth.module.ts Added SessionCookieService and CryptoKeyService to module providers.
auth/auth.service.ts Replaced direct cookie handling with SessionCookieService, updated session ID retrieval and management.
auth/token-refresh.service.spec.ts Updated test cases to use cacheKey instead of sid.
auth/token-refresh.service.ts Changed parameter names from sid to cacheKey in methods.
cache/cache.service.ts Introduced keyWithoutSid method to handle session ID in cache keys.
proxy/proxy.controller.spec.ts Integrated SessionCookieService for session ID handling in tests.
proxy/proxy.module.ts Added SessionCookieService and CryptoKeyService to module providers.
proxy/proxy.service.ts Updated session ID retrieval to use SessionCookieService.
user/user.controller.spec.ts Enhanced session handling with SessionCookieService, updated session ID handling in tests.
user/user.module.ts Added SessionCookieService and CryptoKeyService to module providers.
user/user.service.ts Updated session ID retrieval using SessionCookieService.
services/crypto.service.spec.ts Added CryptoKeyService as a provider in test setup.
services/crypto.service.ts Removed dependency on BffConfig, introduced CryptoKeyService for key management.
services/error.service.ts Updated error handling to use SessionCookieService for clearing session cookies.
services/sessionCookie.service.ts New service for centralized cookie management and encryption.
services/cryptoKey.service.ts New service for managing cryptographic keys.

Suggested labels

automerge, deploy-feature

Suggested reviewers

  • eirikurn
  • magnearun
  • thordurhhh

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Removes unnecessary cache checks for existing sessions in the 
authentication process. The code now directly returns a conflict 
response when a session ID is present, streamlining the logic. 
Additionally, it eliminates the `loginAttemptData` from the 
`updateTokenCache` method, as it is no longer needed, 
reducing complexity and improving maintainability.
Refactor the `onRetry` function to use `useCallback` for better 
performance and prevent unnecessary re-renders. 
Introduce a new 
`onKeepCurrentUser` function that removes BFF error parameters 
from the URL and calls `onRetry`.
Copy link

codecov bot commented Dec 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 35.73%. Comparing base (f962256) to head (adde472).

Additional details and impacted files

Impacted file tree graph

@@                  Coverage Diff                   @@
##           bff-double-session   #17277      +/-   ##
======================================================
+ Coverage               35.61%   35.73%   +0.12%     
======================================================
  Files                    6931     6941      +10     
  Lines                  149294   148337     -957     
  Branches                42744    42337     -407     
======================================================
- Hits                    53164    53007     -157     
+ Misses                  96130    95330     -800     
Flag Coverage Δ
air-discount-scheme-backend 48.23% <ø> (-0.02%) ⬇️
air-discount-scheme-web 0.00% <ø> (ø)
api 3.33% <ø> (ø)
api-catalogue-services 75.81% <ø> (ø)
api-domains-air-discount-scheme 37.96% <ø> (-0.03%) ⬇️
api-domains-assets 26.71% <ø> (ø)
api-domains-auth-admin 48.49% <ø> (ø)
api-domains-communications 39.42% <ø> (+<0.01%) ⬆️
api-domains-criminal-record 48.00% <ø> (-0.04%) ⬇️
api-domains-driving-license 44.84% <ø> (-0.02%) ⬇️
api-domains-education 31.24% <ø> (-0.04%) ⬇️
api-domains-health-insurance 35.42% <ø> (-0.05%) ⬇️
api-domains-mortgage-certificate 35.22% <ø> (-0.03%) ⬇️
api-domains-payment-schedule 42.09% <ø> (-0.03%) ⬇️
application-api-files 61.87% <ø> (-0.04%) ⬇️
application-core 75.78% <ø> (+0.05%) ⬆️
application-system-api 38.74% <ø> (+<0.01%) ⬆️
application-template-api-modules 27.82% <ø> (+0.15%) ⬆️
application-templates-accident-notification 28.82% <ø> (+1.21%) ⬆️
application-templates-car-recycling 3.12% <ø> (ø)
application-templates-criminal-record 25.86% <ø> (+0.02%) ⬆️
application-templates-driving-license 18.16% <ø> (+0.01%) ⬆️
application-templates-estate 13.73% <ø> (+0.07%) ⬆️
application-templates-example-payment 24.72% <ø> (+0.02%) ⬆️
application-templates-financial-aid 14.46% <ø> (+<0.01%) ⬆️
application-templates-general-petition 23.15% <ø> (+0.02%) ⬆️
application-templates-inheritance-report 6.59% <ø> (+<0.01%) ⬆️
application-templates-marriage-conditions 15.19% <ø> (+0.01%) ⬆️
application-templates-mortgage-certificate 43.70% <ø> (+0.02%) ⬆️
application-templates-parental-leave 29.95% <ø> (+<0.01%) ⬆️
application-types 6.51% <ø> (ø)
application-ui-components 1.22% <ø> (ø)
application-ui-shell 22.43% <ø> (+0.16%) ⬆️
auth-admin-web 2.43% <ø> (ø)
auth-nest-tools 31.52% <ø> (ø)
auth-shared 75.00% <ø> (ø)
clients-charge-fjs-v2 28.88% <ø> (ø)
clients-driving-license 40.87% <ø> (-0.04%) ⬇️
clients-driving-license-book 43.89% <ø> (-0.03%) ⬇️
clients-financial-statements-inao 49.67% <ø> (-0.04%) ⬇️
clients-license-client 1.26% <ø> (ø)
clients-middlewares 73.18% <ø> (-0.33%) ⬇️
clients-regulations 42.96% <ø> (-0.05%) ⬇️
clients-rsk-company-registry 31.18% <ø> (ø)
clients-rsk-personal-tax-return 38.32% <ø> (ø)
clients-smartsolutions 12.77% <ø> (ø)
clients-syslumenn 49.45% <ø> (-0.03%) ⬇️
clients-zendesk 50.46% <ø> (-0.12%) ⬇️
cms 0.39% <ø> (+<0.01%) ⬆️
cms-translations 38.76% <ø> (+<0.01%) ⬆️
content-search-index-manager 95.65% <ø> (ø)
content-search-toolkit 8.16% <ø> (ø)
contentful-apps 4.72% <ø> (+<0.01%) ⬆️
dokobit-signing 62.10% <ø> (-0.09%) ⬇️
email-service 60.07% <ø> (-0.08%) ⬇️
feature-flags 90.76% <ø> (ø)
file-storage 45.86% <ø> (-0.11%) ⬇️
financial-aid-backend 51.40% <ø> (-0.02%) ⬇️
financial-aid-shared 17.88% <ø> (ø)
icelandic-names-registry-backend 54.33% <ø> (ø)
infra-nest-server 48.06% <ø> (ø)
infra-tracing 69.94% <ø> (ø)
island-ui-core 30.93% <ø> (+0.51%) ⬆️
judicial-system-api 20.20% <ø> (-0.01%) ⬇️
judicial-system-audit-trail 68.94% <ø> (-0.09%) ⬇️
judicial-system-backend 55.84% <ø> (-0.15%) ⬇️
judicial-system-formatters 78.33% <ø> (ø)
judicial-system-message 66.48% <ø> (-0.13%) ⬇️
judicial-system-message-handler 48.16% <ø> (-0.18%) ⬇️
judicial-system-scheduler 70.92% <ø> (-0.24%) ⬇️
judicial-system-types 42.09% <ø> (ø)
judicial-system-web 27.94% <ø> (+0.21%) ⬆️
license-api 42.92% <ø> (-0.03%) ⬇️
localization 10.15% <ø> (ø)
logging 58.02% <ø> (ø)
message-queue 67.76% <ø> (ø)
nest-audit 65.78% <ø> (ø)
nest-aws 51.93% <ø> (ø)
nest-config 76.38% <ø> (-0.07%) ⬇️
nest-core 53.16% <ø> (ø)
nest-feature-flags 51.00% <ø> (-0.07%) ⬇️
nest-problem 45.64% <ø> (ø)
nest-sequelize 94.44% <ø> (ø)
nest-swagger 51.13% <ø> (ø)
nova-sms 61.42% <ø> (-0.10%) ⬇️
portals-admin-regulations-admin 1.80% <ø> (ø)
portals-core 19.81% <ø> (+0.22%) ⬆️
regulations 16.78% <ø> (ø)
residence-history 85.00% <ø> (ø)
services-auth-admin-api 52.56% <ø> (+0.01%) ⬆️
services-auth-delegation-api 58.45% <ø> (-0.10%) ⬇️
services-auth-ids-api 52.57% <ø> (+<0.01%) ⬆️
services-auth-public-api 49.41% <ø> (-0.02%) ⬇️
services-sessions 65.35% <ø> (+0.02%) ⬆️
services-university-gateway 49.45% <ø> (-0.02%) ⬇️
services-user-notification 46.55% <ø> (+0.03%) ⬆️
services-user-profile 56.87% <ø> (-0.10%) ⬇️
shared-components 29.89% <ø> (+0.41%) ⬆️
shared-form-fields 33.89% <ø> (+0.51%) ⬆️
shared-mocking 58.64% <ø> (ø)
shared-pii 92.85% <ø> (ø)
shared-problem 87.50% <ø> (ø)
shared-utils 27.69% <ø> (ø)
skilavottord-ws 24.26% <ø> (ø)
web 2.41% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

see 180 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f962256...adde472. Read the comment docs.

@datadog-island-is
Copy link

datadog-island-is bot commented Dec 18, 2024

Datadog Report

All test runs eff6265 🔗

10 Total Test Services: 0 Failed, 10 Passed
🔻 Test Sessions change in coverage: 14 decreased, 19 increased, 167 no change

Test Services
This report shows up to 10 services
Service Name Failed Known Flaky New Flaky Passed Skipped Total Time Code Coverage Change Test Service View
air-discount-scheme-backend 0 0 0 63 0 25.41s N/A Link
air-discount-scheme-web 0 0 0 2 0 7.86s N/A Link
api 0 0 0 4 0 3.36s N/A Link
api-catalogue-services 0 0 0 23 0 12.02s N/A Link
api-domains-air-discount-scheme 0 0 0 6 0 20.15s N/A Link
api-domains-assets 0 0 0 3 0 12.19s N/A Link
api-domains-auth-admin 0 0 0 18 0 13.13s N/A Link
api-domains-communications 0 0 0 5 0 36.66s N/A Link
api-domains-criminal-record 0 0 0 5 0 10.14s N/A Link
api-domains-driving-license 0 0 0 23 0 32.2s N/A Link

🔻 Code Coverage Decreases vs Default Branch (14)

This report shows up to 5 code coverage decreases.

  • judicial-system-scheduler - jest 76.59% (-0.21%) - Details
  • judicial-system-message-handler - jest 51.22% (-0.18%) - Details
  • judicial-system-xrd-api - jest 74.65% (-0.17%) - Details
  • judicial-system-backend - jest 59.48% (-0.14%) - Details
  • judicial-system-message - jest 68.19% (-0.13%) - Details

Base automatically changed from bff-double-session to main December 23, 2024 13:30
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
apps/services/bff/src/app/modules/auth/token-refresh.service.spec.ts (1)

Line range hint 42-255: Consider adding timeout configuration test.

The test suite is well-structured and provides good coverage. Consider adding a test case that verifies the timeout configuration for token refresh retries to ensure the system behaves correctly under different timeout settings.

Example test case:

it('should respect configured timeout value for refresh polling', async () => {
  // Arrange
  const customTimeout = 1000;
  await cacheService.save({
    key: refreshInProgressKey,
    value: true,
    ttl: 3000,
  });

  // Act
  const result = await service.refreshToken({
    sid: testSid,
    encryptedRefreshToken: testRefreshToken,
    pollingTimeout: customTimeout,
  });

  // Assert
  expect(mockLogger.warn).toHaveBeenCalled();
  expect(idsService.refreshToken).toHaveBeenCalledWith(testRefreshToken);
  expect(result).toEqual(mockTokenResponse);
}, customTimeout + 500); // Add buffer to prevent test timeout
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between be57698 and 97c05ba.

📒 Files selected for processing (16)
  • apps/services/bff/src/app/modules/auth/auth.controller.spec.ts (11 hunks)
  • apps/services/bff/src/app/modules/auth/auth.module.ts (1 hunks)
  • apps/services/bff/src/app/modules/auth/auth.service.ts (7 hunks)
  • apps/services/bff/src/app/modules/auth/token-refresh.service.spec.ts (1 hunks)
  • apps/services/bff/src/app/modules/auth/token-refresh.service.ts (2 hunks)
  • apps/services/bff/src/app/modules/cache/cache.service.ts (4 hunks)
  • apps/services/bff/src/app/modules/proxy/proxy.controller.spec.ts (10 hunks)
  • apps/services/bff/src/app/modules/proxy/proxy.module.ts (2 hunks)
  • apps/services/bff/src/app/modules/proxy/proxy.service.ts (3 hunks)
  • apps/services/bff/src/app/modules/user/user.controller.spec.ts (12 hunks)
  • apps/services/bff/src/app/modules/user/user.module.ts (2 hunks)
  • apps/services/bff/src/app/modules/user/user.service.ts (3 hunks)
  • apps/services/bff/src/app/services/crypto.service.spec.ts (2 hunks)
  • apps/services/bff/src/app/services/crypto.service.ts (3 hunks)
  • apps/services/bff/src/app/services/error.service.ts (3 hunks)
  • apps/services/bff/src/app/services/sessionCookie.service.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (16)
apps/services/bff/src/app/modules/proxy/proxy.module.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/user/user.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/auth/token-refresh.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/services/error.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/auth/auth.controller.spec.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/auth/auth.module.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/user/user.module.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/proxy/proxy.controller.spec.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/auth/auth.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/services/sessionCookie.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/user/user.controller.spec.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/proxy/proxy.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/services/crypto.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/auth/token-refresh.service.spec.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/services/crypto.service.spec.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/cache/cache.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
🔇 Additional comments (90)
apps/services/bff/src/app/modules/auth/token-refresh.service.spec.ts (1)

181-181: Security improvement: Removed session ID from log message.

The simplification of the warning message aligns with the PR's security objectives by preventing sensitive session IDs from appearing in logs.

Let's verify that this logging pattern is consistently applied across other test files:

✅ Verification successful

Let me check the context of those remaining occurrences to ensure they're not related to logging.


Session ID logging has been consistently removed from test assertions

The found occurrences of "sid" in auth.controller.spec.ts are not related to logging messages - they are used in test setup for query parameters and test case descriptions. The original change to remove session IDs from log messages appears to be consistently applied across the test files.

  • The occurrence in auth.controller.spec.ts line with query({ sid: SID_VALUE }) is for API request setup
  • The other occurrence is just a test description for an invalid token scenario
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining session ID logging in test files
# Look for patterns that might log session IDs in warning/error messages

rg -g "*.spec.ts" "warn.*sid|error.*sid|log.*sid" apps/services/bff/src/

Length of output: 366


Script:

#!/bin/bash
# Get context around the matches to verify if they're related to logging
rg -g "*.spec.ts" -B 2 -A 2 "warn.*sid|error.*sid|log.*sid" apps/services/bff/src/

Length of output: 1045

apps/services/bff/src/app/modules/auth/auth.module.ts (2)

2-4: Good practice: Import required services for modular design.

Including CryptoService, PKCEService, and SessionCookieService demonstrates adherence to NestJS's modular structure by injecting only what's needed. This separation of services helps ensure each one is testable, maintainable, and easily replaced if necessary.


11-17: Properly structured providers array.

Each service is clearly listed, which aligns with NestJS best practices. This fosters clear visibility and a straightforward dependency injection setup, making the AuthModule self-contained and easy to integrate into the wider application.

apps/services/bff/src/app/modules/user/user.module.ts (2)

4-4: Centralizing session handling with SessionCookieService.

Injecting SessionCookieService in the UserModule to handle session cookies follows NestJS's principle of modularization. Ensure you have relevant tests for session management in your user flows to validate session retrieval and clearing logic.


20-20: Confirm usage coverage.

While adding SessionCookieService here is beneficial, verify that all necessary endpoints within this module properly use it for session management. This promotes consistency across the user-related features.

apps/services/bff/src/app/modules/proxy/proxy.module.ts (2)

4-4: Consistent cookie encryption approach.

Importing SessionCookieService in the ProxyModule ensures consistent encryption and decryption logic throughout all proxy-related flows. This centralization helps avoid duplicate code and potential security oversights.


20-20: Ensure thorough testing of proxy path flows.

Verify that the proxy routes using SessionCookieService for cookie handling have adequate test coverage, particularly for encryption edge cases and invalid cookie scenarios.

apps/services/bff/src/app/services/sessionCookie.service.ts (11)

1-2: Consistent logging provider import.

Using @island.is/logging aligns with the shared logging standards. Verify that the appropriate log levels are employed for different error and info scenarios, reducing noise and ensuring meaningful log entries.


3-3: Clean dependency injection with NestJS.

Annotating with @Inject for LOGGER_PROVIDER is consistent with NestJS patterns, ensuring the logger instance is properly resolved at runtime.


4-4: Request and Response typing.

Importing Request and Response from express is valid. Ensure that any usage remains consistent with NestJS's @Req and @Res decorators, if applicable, and that the application still benefits from NestJS's abstraction layers.


5-5: Use of central constants for cookie name.

Referencing a shared SESSION_COOKIE_NAME helps maintain clarity and global consistency across the codebase. Keep an eye out for potential duplication of cookie constants in other modules.


6-6: Reusability of shared cookie options.

getCookieOptions() demonstrates DRY usage for cookie settings. Ensure it’s flexible enough (i.e., sameSite config, secure flags) to handle production and development differences.


7-7: Encryption service injection.

Dependency injecting CryptoService ensures the session cookie encryption steps remain consistent with the rest of the application’s cryptographic operations. Verify that the same encryption approach is used uniformly for any sensitive data.


9-10: Follows NestJS service architecture.

@Injectable() and a constructor-based injection pattern reflect standard NestJS practices. This approach simplifies testing by allowing the SessionCookieService to be easily mocked or replaced for test environments.


11-15: Logger-based error handling.

Injecting logger ensures errors get captured consistently. It's good practice to monitor error rates in production to detect any potential decryption or encryption anomalies.


17-32: Comprehensive error handling in set().

By catching the encryption process error, logging, and rethrowing, you preserve the call stack for debugging while ensuring the application can handle failures gracefully.


34-55: Fail-safe design in get().

Returning undefined upon decryption failures gracefully handles corrupted or tampered cookies. Logging the error while not crashing the request flow can help reduce disruption in user sessions.


56-62: Appropriate cookie clearing in clear().

Clearing the session cookie with the same options ensures consistent domain and path scoping. Confirm that the relevant tests cover user logouts, timeouts, and manual session clearing logic.

apps/services/bff/src/app/modules/user/user.service.ts (3)

7-7: Good import usage
Properly importing the SessionCookieService fosters cleaner code organization and better maintainability.


19-19: Constructor DI looks good
Injecting SessionCookieService via constructor aligns with NestJS best practices for dependency injection.


44-44: Appropriate session retrieval
Retrieving the session ID via this.sessionCookieService.get(req) centralizes session logic, enhancing security and readability.

apps/services/bff/src/app/modules/cache/cache.service.ts (5)

10-10: Centralizing separator usage
Storing the separator in a private property makes the code more maintainable and prevents hardcoded string repetition.


31-37: Clear logic for removing session ID
The keyWithoutSid method effectively sanitizes cache keys to avoid leaking session IDs in logs. This is a valuable security measure.


40-40: Good design for error messages
Using this.keyWithoutSid(key) in createKeyError helps ensure session IDs are not exposed in error logs.


54-54: Consistent use of separator
Referencing this.separator here ensures consistency across the codebase when generating session keys.


92-94: Secure deletion error handling
Wrapping the cache deletion in a try-catch and using this.keyWithoutSid in the error message further mitigates session ID leaks.

apps/services/bff/src/app/services/error.service.ts (3)

6-6: Single import for session cookie operations
Importing SessionCookieService reduces duplicated cookie-handling code across multiple services.


36-36: DI for central cookie management
Injecting SessionCookieService ensures consistent and secure operations on session cookies, aligning with NestJS patterns.


82-82: Secure session cleanup
Clearing the session cookie via this.sessionCookieService.clear(res) maintains a single source of truth for cookie management.

apps/services/bff/src/app/services/crypto.service.spec.ts (5)

6-6: Relevant import
Importing CryptoService is aligned with testing best practices, ensuring the tested class is clearly referenced.


92-93: New test suite
Defining a dedicated skipAlgorithmPrefix suite improves clarity and separation of concerns in the test file.


96-109: Default behavior test
Verifies that the encryption includes the algorithm prefix under normal circumstances. This is essential for backward compatibility.


111-124: Prefix skipping test
Ensures the encryption can omit the algorithm prefix, increasing flexibility for environments that need a more compact format.


126-141: Error throwing with mismatched prefix
Validating that mismatched prefix usage properly fails guards against incorrect usage, emphasizing robust error handling.

apps/services/bff/src/app/services/crypto.service.ts (9)

35-35: Documentation parameter addition looks good.
The skipAlgorithmPrefix parameter is clearly documented, and its purpose is straightforward.


41-44: Informative usage examples.
Including multiple examples for the encrypt method clarifies how skipAlgorithmPrefix behaves in different use cases.


46-46: Method signature updates are consistent.
The addition of skipAlgorithmPrefix aligns well with the examples and helps retain a flexible encryption format.


61-66: Conditional logic checks out.
The logic to conditionally prepend the algorithm prefix is implemented correctly and keeps the encryption output flexible.


69-69: Maintaining backward compatibility.
Returning the output in the old format if skipAlgorithmPrefix is false ensures existing consumers are unaffected.


81-81: Decryption parameter documentation is consistent.
The docstring for skipAlgorithmPrefix mirrors the encryption usage, ensuring clarity.


87-90: Examples for decryption method are well explained.
Providing both prefix and non-prefix scenarios is very helpful for developers.


92-92: Method signature alignment.
Including skipAlgorithmPrefix in decrypt ensures symmetry with the encryption method.


94-109: Split logic is carefully implemented.
Using slice(1) to exclude the algorithm prefix is a neat approach. The additional checks help avoid malformed data issues.

apps/services/bff/src/app/modules/auth/token-refresh.service.ts (2)

129-129: Redacted session details in logs.
Removing direct session ID references from logs mitigates potential leakage. This is a beneficial security practice.


218-218: Warning message updated for privacy.
Simplifying the log statement while signaling a token refresh failure helps reduce the risk of session ID exposure.

apps/services/bff/src/app/modules/proxy/proxy.service.ts (3)

22-22: Centralizing session management.
Importing SessionCookieService helps maintain a single source of truth for session handling.


72-72: Dependency Injection practice is correct.
Injecting SessionCookieService aligns with NestJS best practices for service encapsulation.


87-87: Secure session ID retrieval.
Obtaining the session ID via sessionCookieService.get promotes consistency and removes direct cookie references.

apps/services/bff/src/app/modules/proxy/proxy.controller.spec.ts (4)

14-14: Appropriate import for encryption.
Including CryptoService for test usage ensures security measures are consistently evaluated here as well.


64-65: New variables for encrypted session handling.
Declaring cryptoService and encryptedSid clarifies the test approach for secure session checks.


84-88: Initializing and using cryptoService.
Encrypting SID_VALUE adds an extra layer of security in test scenarios, reflecting real usage.


134-134: Consistent usage of the encrypted session ID.
All test calls include the encrypted SID in the cookie, ensuring uniform coverage and preventing sensitive data leaks.

Also applies to: 146-146, 152-152, 180-180, 185-185, 198-198, 203-203, 228-228, 233-233, 262-262, 267-267, 308-308, 313-313

apps/services/bff/src/app/modules/user/user.controller.spec.ts (14)

14-14: Good import for CryptoService.
This reference allows secure session-encryption logic to be integrated into the tests.


74-75: Neat approach adding cryptoService and encryptedSid.
This test-level injection neatly aligns with the new encryption flow.


89-89: Initialization logic looks correct.
Using app.get to retrieve CryptoService is a proper NestJS pattern.


91-93: Ensure that the encryption key is properly secured.
While encrypting the SID is beneficial, confirm that the cryptographic key and algorithm configurations are securely stored.


122-122: Cookie set with encryptedSid is correct.
This ensures sessions remain hidden in transit.


139-139: Encrypted SID usage is consistent.
Using the encrypted SID for callback testing aligns with the new security approach.


147-147: Consistent encryption approach in tests.
The cookie is set to the new encrypted SID, ensuring thorough coverage.


172-172: Cookie usage is secured.
Passing the encrypted SID supports safe session transmission.


189-189: Cookie setting is well integrated.
Ensuring continuity with the encrypted SID helps validate new session logic.


214-214: Encrypted SID in the cookie is maintained.
Consistency helps mitigate session hijacking risks.


231-231: Cookie set to encrypted SID remains consistent.
This strengthens test coverage for session management.


253-253: Cookie assignment is secure.
Verifying that the encrypted SID is applied across various test paths is good.


270-270: Sustained usage of encryption in cookie.
Ensures uniform testing of all code paths with the updated approach.


337-337: Repeating pattern of securely setting cookies is correct.
This test covers additional refresh scenarios with encrypted SID.

apps/services/bff/src/app/modules/auth/auth.controller.spec.ts (15)

16-16: Good inclusion of CryptoService.
This import paves the way for secure session encryption.


74-75: Nicely integrates cryptoService and encryptedSid.
This will ensure each test scenario uses the encrypted session identifier.


87-87: cryptoService retrieval is correct.
Aligns well with the NestJS injection pattern.


89-89: Configuration retrieval is standard.
No issues found in how the config is fetched.


92-93: Secure cookie encryption.
SID encryption ensures that sensitive data is less exposed in transit.


322-322: Ensuring the correct cache key.
This verifies that the login attempt key uses the plain SID (while the cookie is encrypted).


333-333: Cookie set with encryptedSid is correct.
Matches the new security requirements.


340-340: current login key usage confirmed.
This assertion verifies appropriate transitions from login attempt to current login.


367-367: Cookie usage for subsequent requests is consistent.
Ensures the new security approach remains stable across endpoints.


397-402: Negative test scenario with invalid UUID.
Passing an encrypted but incorrect SID effectively tests error handling behavior.


425-425: Consistent approach to secure cookie usage.
This method call is properly using the encrypted SID in testing.


431-431: Logout flow test referencing encrypted SID.
Ensures alignment with the new cookie encryption method.


460-460: Callback logout test coverage.
This verifies graceful handling of POST-based logout flow.


529-529: Secure cookie in logout callback.
Ensures alignment with the rest of the encryption approach.


578-578: Verifies double session detection with encrypted cookie.
Appropriate consistency across all session logic scenarios.

apps/services/bff/src/app/modules/auth/auth.service.ts (9)

24-24: SessionCookieService import is appropriate.
Centralizing cookie management is a strong design choice.


54-54: Injecting SessionCookieService.
This fosters better abstraction for cookie operations, simplifying future maintenance.


266-266: Fetching SID from the session cookie.
This is a secure approach to unify how sid is retrieved throughout the app.


359-359: Clearing session cookie.
Good use of sessionCookieService to handle cookie cleanup consistently.


362-365: Setting the new SID in session cookie.
Using sessionCookieService ensures robust encryption and consistency in all flows.


368-368: Retrieving the old session cookie.
This step is crucial for revoking or cleaning up stale tokens.


424-424: Cookie retrieval for logout is correct.
The sessionCookieService usage is consistent with the rest of the codebase.


434-436: Clear and concise error logging.
This provides a direct reference to the mismatched cookie for easier debugging.


474-474: Consistent cookie clearing on logout.
Centralized session management is good practice.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (11)
apps/services/bff/src/app/modules/user/user.controller.spec.ts (4)

78-87: Enhance type safety of mock objects.

While the mock objects are well-structured, consider using more specific types for the mock functions to ensure type safety.

 const mockResponse: Partial<Response> = {
-  cookie: jest.fn(),
-  clearCookie: jest.fn(),
+  cookie: jest.fn<Response['cookie']>(),
+  clearCookie: jest.fn<Response['clearCookie']>(),
 }

101-117: Add error handling test cases for SessionCookieService.

The setup properly integrates SessionCookieService, but consider adding test cases for error scenarios:

  • Invalid session values
  • Cookie service failures
  • Edge cases in session encryption

This aligns with the PR's security focus and ensures robust error handling.


124-125: Enhance test cleanup for better isolation.

Consider resetting all mock function calls and restoring the original SessionCookieService state.

 // Reset mocks
 mockRequest.cookies = {}
+mockResponse.cookie.mockClear()
+mockResponse.clearCookie.mockClear()
+jest.spyOn(sessionCookieService, 'set').mockClear()

206-206: Add security-focused assertions for session handling.

While the cache key updates correctly use the hashed session ID, consider adding assertions to verify:

  • The hashed session ID format
  • Prevention of session fixation
  • Proper encryption of sensitive data in cache

This aligns with the PR's security enhancement objectives.

// Add these assertions in relevant test cases
expect(hashedSid).not.toEqual(SID_VALUE)
expect(hashedSid).toMatch(/^[a-f0-9]{64}$/) // Assuming SHA-256 hashing

Also applies to: 218-218, 248-248, 287-287, 353-353

apps/services/bff/src/app/services/cryptoKey.service.ts (2)

16-21: Validate presence of required config at startup.

Currently, the constructor calls initializeCryptoKey() but doesn't handle missing configuration in tokenSecretBase64. You might consider adding a user-friendly error or fallback mechanism if the environment variable is empty or undefined, preventing the code from throwing a less descriptive error.


30-40: Robust error handling for invalid key length.

The explicit check for 32-byte length is good. Consider logging the incorrect length for operational insights (without revealing the actual key) in case of misconfiguration. For example, log something like: “Expected 32 bytes, got X bytes.”

apps/services/bff/src/app/services/sessionCookie.service.ts (1)

31-43: Potential fallback logic or retry.

If hashing fails, the error is rethrown. That’s valid for immediate troubleshooting. However, consider customizing the error or adding more context.

apps/services/bff/src/app/modules/auth/auth.controller.spec.ts (4)

104-116: Enhance setup documentation for better clarity.

Consider improving the comments to better explain:

  1. Why we're capturing the hashed value from the first mock call
  2. The relationship between the mock response and request cookies
-    // Set the hashed SID
+    // Hash the session ID using SessionCookieService for subsequent test cases
     sessionCookieService.set({
       res: mockResponse as Response,
       value: SID_VALUE,
     })

-    // Capture the hashed value from the mock cookie call
+    // Extract the hashed session ID from the first (index 0) cookie setting call
     hashedSid = (mockResponse.cookie as jest.Mock).mock.calls[0][1]

-    // Set up the mock request cookies for subsequent get() calls
+    // Configure mock request with the hashed session ID for cookie validation
     mockRequest.cookies = {
       [SESSION_COOKIE_NAME]: hashedSid,
     }

Line range hint 345-364: Enhance cache key validation in login tests.

Consider adding more robust validations:

  1. Extract the cache key format into a constant
  2. Add validation for the hash format
+    const CACHE_KEY_FORMAT = `attempt::${mockConfig.name}::%s`
+
     expect(loginAttempt[0]).toContain(
-      `attempt::${mockConfig.name}::${SID_VALUE}`,
+      CACHE_KEY_FORMAT.replace('%s', SID_VALUE),
     )
+    // Verify the hashed session ID format
+    expect(hashedSid).toMatch(/^[a-f0-9]{64}$/) // Assuming SHA-256 hash

356-357: Reduce duplication in cookie header setting.

Consider extracting the cookie header setup into a helper function to maintain DRY principles and improve maintainability.

+    const withSessionCookie = (request: request.Test) => 
+      request.set('Cookie', [`${SESSION_COOKIE_NAME}=${hashedSid}`])
+
-    .set('Cookie', [`${SESSION_COOKIE_NAME}=${hashedSid}`])
+    withSessionCookie(request)

Also applies to: 390-391, 443-444, 547-548


Line range hint 578-596: Enhance logout test coverage and reduce duplication.

Consider the following improvements:

  1. Extract cache key construction to a helper
  2. Add test cases for edge cases (e.g., expired sessions, invalid hash formats)
+    const buildCacheKey = (type: 'current' | 'attempt', sid: string) =>
+      `${type}::${mockConfig.name}::${sid}`
+
-    const currentKey = `current::${mockConfig.name}::${hashedSid}`
+    const currentKey = buildCacheKey('current', hashedSid)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 97c05ba and eb15ace.

📒 Files selected for processing (15)
  • apps/services/bff/src/app/modules/auth/auth.controller.spec.ts (12 hunks)
  • apps/services/bff/src/app/modules/auth/auth.module.ts (1 hunks)
  • apps/services/bff/src/app/modules/auth/auth.service.ts (9 hunks)
  • apps/services/bff/src/app/modules/auth/token-refresh.service.spec.ts (8 hunks)
  • apps/services/bff/src/app/modules/auth/token-refresh.service.ts (6 hunks)
  • apps/services/bff/src/app/modules/proxy/proxy.controller.spec.ts (11 hunks)
  • apps/services/bff/src/app/modules/proxy/proxy.module.ts (2 hunks)
  • apps/services/bff/src/app/modules/proxy/proxy.service.ts (4 hunks)
  • apps/services/bff/src/app/modules/user/user.controller.spec.ts (14 hunks)
  • apps/services/bff/src/app/modules/user/user.module.ts (2 hunks)
  • apps/services/bff/src/app/modules/user/user.service.ts (4 hunks)
  • apps/services/bff/src/app/services/crypto.service.spec.ts (2 hunks)
  • apps/services/bff/src/app/services/crypto.service.ts (3 hunks)
  • apps/services/bff/src/app/services/cryptoKey.service.ts (1 hunks)
  • apps/services/bff/src/app/services/sessionCookie.service.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (6)
  • apps/services/bff/src/app/services/crypto.service.spec.ts
  • apps/services/bff/src/app/modules/auth/token-refresh.service.spec.ts
  • apps/services/bff/src/app/modules/user/user.module.ts
  • apps/services/bff/src/app/modules/proxy/proxy.module.ts
  • apps/services/bff/src/app/modules/proxy/proxy.service.ts
  • apps/services/bff/src/app/modules/auth/token-refresh.service.ts
🧰 Additional context used
📓 Path-based instructions (9)
apps/services/bff/src/app/modules/user/user.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/auth/auth.module.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/user/user.controller.spec.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/services/crypto.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/services/cryptoKey.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/auth/auth.controller.spec.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/services/sessionCookie.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/auth/auth.service.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
apps/services/bff/src/app/modules/proxy/proxy.controller.spec.ts (2)

Pattern apps/services/**/*: "Confirm that the code adheres to the following:

  • NestJS architecture, including modules, services, and controllers.
  • Dependency injection patterns and service encapsulation.
  • Integration and unit testing coverage and practices."

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
🔇 Additional comments (26)
apps/services/bff/src/app/modules/user/user.controller.spec.ts (2)

4-4: LGTM! Appropriate imports for session handling.

The added imports properly support the new session cookie functionality, following NestJS best practices for type safety.

Also applies to: 15-15


Line range hint 1-378: Well-structured test suite with robust security coverage.

The implementation successfully integrates session encryption while maintaining good test practices:

  • Proper dependency injection
  • Comprehensive test scenarios
  • Clear separation of concerns
  • Consistent use of hashed session IDs

The test suite aligns well with the PR's security objectives and NestJS architecture guidelines.

apps/services/bff/src/app/services/cryptoKey.service.ts (2)

1-11: Excellent use of NestJS and clear documentation.

The file introduces a well-documented and self-contained CryptoKeyService. Adhering to NestJS conventions for dependency injection and providing detailed docstrings helps maintain clarity.


46-48: Public getter aligns with best practices.

Exposing the _cryptoKey via a getter fosters encapsulation. This approach allows future modifications or validations in a single location.

apps/services/bff/src/app/modules/auth/auth.module.ts (2)

2-5: Well-structured imports for NestJS module.

You are cleanly organizing providers (CryptoService, PKCEService, CryptoKeyService, etc.) which ensures clear dependencies.


12-19: Providers array is properly expanded.

The addition of SessionCookieService and CryptoKeyService follows NestJS best practices for modular dependency injection. Ensure each service is thoroughly tested.

apps/services/bff/src/app/services/sessionCookie.service.ts (6)

1-9: Import statements and constants are well-structured.

You are correctly importing Logger, Injectable, CryptoKeyService, and defining the session cookie constant. This sets a clean foundation for the rest of the service.


10-17: Constructor injection is consistent with NestJS.

The use of LOGGER_PROVIDER and CryptoKeyService promotes clear separation of concerns.


24-26: Graceful handling of missing session cookies.

Returning undefined when no session cookie is found simplifies checks in consuming code.


48-58: Comparison logic is properly hashed.

This ensures the original session value is never exposed. Good to see consistent hashing.


63-65: Cookie clearing is straightforward.

Using clearCookie with shared options consolidates the session removal process.


70-75: Secure HMAC approach.

Utilizing HMAC with SHA-256 ensures robust hashing. This setup prevents reverse engineering of session IDs.

apps/services/bff/src/app/modules/user/user.service.ts (4)

7-7: Centralized session cookie management.

Importing SessionCookieService replaces direct cookie handling, aligning with the new architecture.


19-19: Constructor injection for the session cookie.

This ensures the UserService can handle sessions without duplicating logic.


44-44: Secure retrieval of hashed session ID.

Using sessionCookieService.get(req) is a safer approach, ensuring no raw session ID is exposed.


69-69: Consistent naming with “cacheKey” parameter.

Renaming from sid to cacheKey prevents confusion and clarifies that the stored session data is hashed.

apps/services/bff/src/app/services/crypto.service.ts (2)

13-14: LGTM! Good separation of concerns.

The constructor now properly delegates key management to CryptoKeyService, improving modularity and maintainability.


34-38: Verify the cryptographic key handling.

The encryption and decryption methods now consistently use the key from CryptoKeyService. However, let's verify the key's lifecycle and potential race conditions.

Also applies to: 81-85

✅ Verification successful

Let me gather more information about the CryptoKeyService implementation.


Let me check one more aspect about the service initialization.


Cryptographic key handling is properly implemented and thread-safe

The CryptoKeyService implementation shows a robust and thread-safe design:

  • The key is initialized once during service construction
  • Uses a private readonly field _cryptoKey with a public getter-only access
  • Key initialization is synchronous and validates the key length
  • The service is managed by NestJS DI container (Injectable decorator) which ensures singleton scope by default

The implementation prevents race conditions and ensures consistent key access across the application.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential race conditions in key initialization
# Test: Search for CryptoKeyService initialization and usage patterns
ast-grep --pattern 'class CryptoKeyService {
  $$$
  cryptoKey
  $$$
}'

# Test: Check for any direct key assignments or modifications
rg -A 5 'cryptoKey\s*='

Length of output: 639


Script:

#!/bin/bash
# Check the complete implementation of CryptoKeyService
cat apps/services/bff/src/app/services/cryptoKey.service.ts

# Check where CryptoKeyService is instantiated
rg -l "new CryptoKeyService"

# Check if there are any async operations in key initialization
rg "async.*initializeCryptoKey"

Length of output: 1575


Script:

#!/bin/bash
# Check how the service is registered in the module
rg -A 5 "CryptoKeyService.*providers"

# Check for any direct instantiation in tests
rg "CryptoKeyService.*beforeEach"

# Check if the service is used in singleton scope
ast-grep --pattern '@Injectable({
  $$$
  scope
  $$$
})'

Length of output: 135

apps/services/bff/src/app/modules/proxy/proxy.controller.spec.ts (3)

65-77: LGTM! Well-structured test setup.

Good test isolation with proper mocking of Response and Request objects.


96-111: LGTM! Proper session cookie service initialization.

The setup correctly initializes the session cookie service and captures the hashed session ID for subsequent tests.


156-156: LGTM! Consistent session cookie usage.

All test cases have been properly updated to use the hashed session ID, maintaining consistency across the test suite.

Also applies to: 168-168, 174-174, 202-202, 207-207, 220-220, 225-225, 250-250, 255-255, 284-284, 289-289, 330-330, 335-335

apps/services/bff/src/app/modules/auth/auth.service.ts (4)

54-54: LGTM! Good dependency injection.

Properly injected SessionCookieService for centralized session management.


128-131: LGTM! Secure cache key handling.

Good security practice using hashed session IDs for cache keys.


Line range hint 431-483: LGTM! Comprehensive logout handling.

The logout process properly handles session verification, cleanup, and error cases.


362-379: Verify session transition handling.

The session transition logic looks secure, but let's verify the handling of concurrent sessions.

✅ Verification successful

Session transition handling is properly implemented with secure cleanup

The verification shows that the session handling implementation in auth.service.ts follows secure practices for managing concurrent sessions:

  1. Existing sessions are properly cleared before setting new ones (sessionCookieService.clear(res))
  2. The code includes explicit checks for old session cookies and their verification
  3. The SessionCookieService implements proper hashing and encryption for cookie values
  4. Session cleanup is consistently handled across login and logout flows

Key security measures found:

  • Session cookies are cleared before setting new ones to prevent multiple active sessions
  • Old session verification is performed using secure hash comparison
  • Proper error handling and logging is implemented for session-related operations
  • Logout flow includes complete session cleanup (cookies and cache)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential session handling edge cases
# Test: Look for other places handling multiple sessions
rg -A 5 'session.*cookie'

# Test: Check for proper session cleanup patterns
ast-grep --pattern 'clear($$$)'

Length of output: 10986

apps/services/bff/src/app/modules/auth/auth.controller.spec.ts (1)

5-5: LGTM! Well-structured test setup with proper type definitions.

The mock objects and type imports are correctly implemented, following NestJS testing best practices.

Also applies to: 17-17, 78-87

@snaerth snaerth added the deploy-feature Deploys features to dev label Jan 4, 2025
Copy link
Contributor

github-actions bot commented Jan 4, 2025

Affected services are: services-bff,
Feature deployment of your services will begin shortly. Your feature will be accessible here:
https://bff-encrypt-sid-beta.dev01.devland.is/bff
https://bff-encrypt-sid-beta.dev01.devland.is/stjornbord/bff

Deployed services: services-bff-portals-admin,services-bff-portals-my-pages.
Excluded services: ``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deploy-feature Deploys features to dev
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant