Skip to content

Commit 9db278b

Browse files
author
Jacek
committed
Merge remote-tracking branch 'origin/main' into feat/nextjs-cache-components-support
# Conflicts: # packages/nextjs/src/app-router/server/auth.ts
2 parents 697ab92 + d4057db commit 9db278b

55 files changed

Lines changed: 656 additions & 176 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.changeset/forty-months-appear.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@clerk/shared': major
3+
'@clerk/react': patch
4+
'@clerk/ui': patch
5+
---
6+
7+
Remove `useUserContext`, `useOrganizationContext`, `useSessionContext` and `useClientContext` from the `shared/react` package.
8+
9+
These hooks have never been meant for public use and have been replaced with internal hooks that do not rely on context.
10+
11+
If you need access to these resources, use the `useUser`, `useOrganization` and `useSession` hooks instead.
12+
13+
If you are building a React SDK and need direct access to the `client`, get in touch with us to discuss!
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clerk/upgrade": patch
3+
---
4+
5+
Improve `generate-guide` script to support generating guides for all SDKs at once and output MDX format compatible with clerk-docs
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clerk/backend": patch
3+
---
4+
5+
Improve token type validation in authentication requests

‎.changeset/silly-peaches-march.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/nextjs': patch
3+
'@clerk/shared': patch
4+
'@clerk/react': patch
5+
---

‎.changeset/tangy-sides-crash.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎integration/tests/machine-auth/oauth.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ test.describe('OAuth machine authentication @machine', () => {
5050
.commit();
5151

5252
await app.setup();
53-
await app.withEnv(appConfigs.envs.withEmailCodes);
53+
await app.withEnv(appConfigs.envs.withAPIKeys);
5454
await app.dev();
5555

5656
// Test user that will authorize the OAuth application

‎integration/tests/middleware-placement.test.ts‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,16 @@ test.describe('next start - missing middleware @quickstart', () => {
7575
});
7676

7777
test('Display error for missing middleware', async ({ page, context }) => {
78+
const { version } = await detectNext(app);
79+
const major = parseSemverMajor(version) ?? 0;
7880
const u = createTestUtils({ app, page, context });
7981
await u.page.goToAppHome();
8082

81-
expect(app.serveOutput).toContain('Your Middleware exists at ./src/middleware.(ts|js)');
83+
const expectedMessage =
84+
major >= 16
85+
? 'Your middleware or proxy file exists at ./src/middleware.(ts|js) or proxy.(ts|js)'
86+
: 'Your middleware file exists at ./src/middleware.(ts|js)';
87+
expect(app.serveOutput).toContain(expectedMessage);
8288
});
8389
});
8490

@@ -109,10 +115,16 @@ test.describe('next start - invalid middleware at root on src/ @quickstart', ()
109115
const u = createTestUtils({ app, page, context });
110116
await u.page.goToAppHome();
111117

112-
expect(app.serveOutput).not.toContain('Your Middleware exists at ./src/middleware.(ts|js)');
113-
expect(app.serveOutput).toContain(
114-
'Clerk: clerkMiddleware() was not run, your middleware file might be misplaced. Move your middleware file to ./src/middleware.ts. Currently located at ./middleware.ts',
115-
);
118+
const expectedMessage =
119+
major >= 16
120+
? 'Your middleware or proxy file exists at ./src/middleware.(ts|js) or proxy.(ts|js)'
121+
: 'Your middleware file exists at ./src/middleware.(ts|js)';
122+
expect(app.serveOutput).not.toContain(expectedMessage);
123+
const expectedError =
124+
major >= 16
125+
? 'Clerk: clerkMiddleware() was not run, your middleware or proxy file might be misplaced. Move your middleware or proxy file to ./src/middleware.ts. Currently located at ./middleware.ts'
126+
: 'Clerk: clerkMiddleware() was not run, your middleware file might be misplaced. Move your middleware file to ./src/middleware.ts. Currently located at ./middleware.ts';
127+
expect(app.serveOutput).toContain(expectedError);
116128
});
117129

118130
test('Does not display misplaced middleware error on Next 16+', async ({ page, context }) => {
@@ -153,10 +165,16 @@ test.describe('next start - invalid middleware inside app on src/ @quickstart',
153165
test.skip(major >= 16 && isCanary, 'Middleware detection is smarter in Next 16 canary.');
154166
const u = createTestUtils({ app, page, context });
155167
await u.page.goToAppHome();
156-
expect(app.serveOutput).not.toContain('Your Middleware exists at ./src/middleware.(ts|js)');
157-
expect(app.serveOutput).toContain(
158-
'Clerk: clerkMiddleware() was not run, your middleware file might be misplaced. Move your middleware file to ./src/middleware.ts. Currently located at ./src/app/middleware.ts',
159-
);
168+
const expectedMessage =
169+
major >= 16
170+
? 'Your middleware or proxy file exists at ./src/middleware.(ts|js) or proxy.(ts|js)'
171+
: 'Your middleware file exists at ./src/middleware.(ts|js)';
172+
expect(app.serveOutput).not.toContain(expectedMessage);
173+
const expectedError =
174+
major >= 16
175+
? 'Clerk: clerkMiddleware() was not run, your middleware or proxy file might be misplaced. Move your middleware or proxy file to ./src/middleware.ts. Currently located at ./src/app/middleware.ts'
176+
: 'Clerk: clerkMiddleware() was not run, your middleware file might be misplaced. Move your middleware file to ./src/middleware.ts. Currently located at ./src/app/middleware.ts';
177+
expect(app.serveOutput).toContain(expectedError);
160178
});
161179

162180
test('Does not display misplaced middleware error on Next 16 canary', async ({ page, context }) => {

‎packages/backend/src/tokens/__tests__/request.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010
mockJwtPayload,
1111
mockMalformedJwt,
1212
} from '../../fixtures';
13-
import { mockMachineAuthResponses, mockTokens, mockVerificationResults } from '../../fixtures/machine';
13+
import {
14+
mockMachineAuthResponses,
15+
mockSignedOAuthAccessTokenJwt,
16+
mockTokens,
17+
mockVerificationResults,
18+
} from '../../fixtures/machine';
1419
import { server } from '../../mock-server';
1520
import type { AuthReason } from '../authStatus';
1621
import { AuthErrorReason, AuthStatus } from '../authStatus';
@@ -1496,6 +1501,17 @@ describe('tokens.authenticateRequest(options)', () => {
14961501
isAuthenticated: false,
14971502
});
14981503
});
1504+
1505+
test('rejects OAuth JWT token when acceptsToken is session_token', async () => {
1506+
const request = mockRequest({ authorization: `Bearer ${mockSignedOAuthAccessTokenJwt}` });
1507+
const result = await authenticateRequest(request, mockOptions({ acceptsToken: 'session_token' }));
1508+
1509+
expect(result).toBeSignedOut({
1510+
reason: AuthErrorReason.TokenTypeMismatch,
1511+
message: '',
1512+
});
1513+
expect(result.toAuth()).toBeSignedOutToAuth();
1514+
});
14991515
});
15001516

15011517
describe('Array of Accepted Token Types', () => {

‎packages/backend/src/tokens/request.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { AuthErrorReason, handshake, signedIn, signedOut, signedOutInvalidToken
1414
import { createClerkRequest } from './clerkRequest';
1515
import { getCookieName, getCookieValue } from './cookie';
1616
import { HandshakeService } from './handshake';
17-
import { getMachineTokenType, isMachineToken, isTokenTypeAccepted } from './machine';
17+
import { getMachineTokenType, isMachineToken, isOAuthJwt, isTokenTypeAccepted } from './machine';
1818
import { OrganizationMatcher } from './organizationMatcher';
1919
import type { MachineTokenType, SessionTokenType } from './tokenTypes';
2020
import { TokenType } from './tokenTypes';
@@ -411,6 +411,19 @@ export const authenticateRequest: AuthenticateRequest = (async (
411411
async function authenticateRequestWithTokenInHeader() {
412412
const { tokenInHeader } = authenticateContext;
413413

414+
// Reject OAuth JWTs that may appear in headers when expecting session tokens.
415+
// OAuth JWTs are valid Clerk-signed JWTs and will pass verifyToken() verification,
416+
// but should not be accepted as session tokens.
417+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
418+
if (isOAuthJwt(tokenInHeader!)) {
419+
return signedOut({
420+
tokenType: TokenType.SessionToken,
421+
authenticateContext,
422+
reason: AuthErrorReason.TokenTypeMismatch,
423+
message: '',
424+
});
425+
}
426+
414427
try {
415428
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
416429
const { data, errors } = await verifyToken(tokenInHeader!, authenticateContext);

‎packages/nextjs/src/app-router/server/ClerkProvider.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export async function ClerkProvider<TUi extends Ui = Ui>(
3636

3737
const propsWithEnvs = mergeNextClerkPropsWithEnv({
3838
...rest,
39-
initialState: (await statePromiseOrValue) as InitialState | undefined,
39+
// Even though we always cast to InitialState here, this might still be a promise.
40+
// While not reflected in the public types, we do support this for React >= 19 for internal use.
41+
initialState: statePromiseOrValue as InitialState | undefined,
4042
nonce: await noncePromiseOrValue,
4143
});
4244

0 commit comments

Comments
 (0)