Skip to content

Fix AUTH_SECRET#352

Closed
Brandawg93 wants to merge 2 commits intomainfrom
auth-secret
Closed

Fix AUTH_SECRET#352
Brandawg93 wants to merge 2 commits intomainfrom
auth-secret

Conversation

@Brandawg93
Copy link
Copy Markdown
Owner

No description provided.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the ensureAuthSecret() function to implement proper caching of generated authentication secrets using a module-level variable. The changes ensure that the same secret is consistently returned across multiple calls, addressing potential issues where different secrets might be generated in different contexts.

Key Changes

  • Added a module-level cachedAuthSecret variable to store generated secrets in memory
  • Refactored ensureAuthSecret() to have a clear priority order: environment variable → cached value → newly generated value
  • Added a fallback code path for non-Node.js runtime environments (edge runtime, build time)
  • Updated tests to verify the new caching behavior and ensure the function returns the correct value

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/server/auth-config.ts Implements caching mechanism for AUTH_SECRET with module-level variable and refactors function logic to handle different runtime environments
__tests__/unit/server/auth-config.test.ts Adds new test for checking existing AUTH_SECRET and refactors existing tests to verify caching behavior and return values
Comments suppressed due to low confidence (1)

src/server/auth-config.ts:38

  • This negation always evaluates to true.
  if (!cachedAuthSecret) {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/server/auth-config.ts Outdated
Comment thread src/server/auth-config.ts Outdated
Comment thread src/server/auth-config.ts Outdated
Comment on lines +74 to 92
it('should return the same cached secret on subsequent calls', () => {
const mockRandomBytes = new Uint8Array(32)
for (let i = 0; i < 32; i++) {
mockRandomBytes[i] = i
}
mockGetRandomValues.mockImplementation((array: Uint8Array) => {
array.set(mockRandomBytes)
return array
})

ensureAuthSecret()
const result1 = ensureAuthSecret()
const result2 = ensureAuthSecret()
const result3 = ensureAuthSecret()

expect(mockGetRandomValues).not.toHaveBeenCalled()
expect(process.env.AUTH_SECRET).toBe(existingSecret)
expect(mockGetRandomValues).toHaveBeenCalledTimes(1)
expect(result1).toBe(result2)
expect(result2).toBe(result3)
expect(result1).toBe(Buffer.from(mockRandomBytes).toString('base64'))
})
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

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

The test should verify that process.env.AUTH_SECRET is set correctly on the first call, similar to the assertion in the test at line 71. This would ensure that the caching test covers the full behavior of setting both the cached variable and the environment variable.

Copilot uses AI. Check for mistakes.
@sonarqubecloud
Copy link
Copy Markdown

@Brandawg93 Brandawg93 closed this Jan 19, 2026
@Brandawg93 Brandawg93 deleted the auth-secret branch January 19, 2026 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants