Skip to content
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
2 changes: 2 additions & 0 deletions .changeset/diag-userbutton-mount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
13 changes: 13 additions & 0 deletions packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3235,9 +3235,22 @@ export class Clerk implements ClerkInterface {
organization: this.organization,
};
this.__internal_lastEmittedResources = resources;
// DIAG: instrument userButton mount regression
// eslint-disable-next-line no-console
console.log('[CLERK_DIAG] clerk.#emit', {
listeners: this.#listeners.length,
userId: this.user?.id ?? null,
sessionId: this.session?.id ?? null,
clientId: this.client?.id ?? null,
sessionsOnClient: this.client?.sessions?.length ?? 0,
});
for (const listener of this.#listeners) {
listener(resources);
}
} else {
// DIAG: emit was skipped because client is unset
// eslint-disable-next-line no-console
console.log('[CLERK_DIAG] clerk.#emit skipped (no client)');
Comment on lines +3238 to +3253
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Remove or hard-gate [CLERK_DIAG] logs before merge

Line 3240 logs identifiers (userId, sessionId, clientId) on every emit. This is a merge blocker: accidental merge would leak internal identifiers into browser/CI logs and add noisy hot-path logging. Please gate these diagnostics behind an explicit non-production flag (or remove before merge), and apply the same guard to the matching [CLERK_DIAG] logs in useUserBase and withCoreUserGuard.

As per coding guidelines: "Implement proper logging with different levels" and "validate all inputs and sanitize outputs."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/clerk-js/src/core/clerk.ts` around lines 3238 - 3253, The DIAG
console logs in the Clerk emit path leak internal identifiers and run on a hot
path; wrap the diagnostic logging in a non-production/explicit diagnostic flag
check (e.g., only log when a process/env flag like CLERK_DIAG_ENABLED or an
internal config boolean is true) and stop logging raw IDs—either remove
userId/sessionId/clientId or redact them (e.g., boolean presence or
truncated/sanitized values) before logging; apply the same gated/sanitized
change to the other diagnostic console.log sites referenced in useUserBase and
withCoreUserGuard and ensure the guard references the same flag/config accessor
used here (the block around the console.log in the method that iterates
this.#listeners).

}
};

Expand Down
15 changes: 13 additions & 2 deletions packages/shared/src/react/hooks/base/useUserBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ export function useUserBase(): UserResource | null | undefined {
[clerk],
),
useCallback(() => {
if (!clerk.loaded || !clerk.__internal_lastEmittedResources) {
// DIAG: instrument userButton mount regression
const fellBack = !clerk.loaded || !(clerk as any).__internal_lastEmittedResources;
// eslint-disable-next-line no-console
console.log('[CLERK_DIAG] useUserBase getSnapshot', {
loaded: !!clerk.loaded,
hasLastEmitted: !!(clerk as any).__internal_lastEmittedResources,
emittedUserId: (clerk as any).__internal_lastEmittedResources?.user?.id,
clerkUserId: (clerk as any).user?.id,
sessionId: (clerk as any).session?.id,
fellBackToInitial: fellBack,
});
if (fellBack) {
return getInitialState();
}
return clerk.__internal_lastEmittedResources.user;
return (clerk as any).__internal_lastEmittedResources.user;
}, [clerk, getInitialState]),
getInitialState,
);
Expand Down
8 changes: 8 additions & 0 deletions packages/ui/src/contexts/CoreUserContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ export function withCoreUserGuard<P>(Component: React.ComponentType<P>): React.C
const Hoc = (props: P) => {
const user = __internal_useUserBase();

// DIAG: instrument userButton mount regression
// eslint-disable-next-line no-console
console.log('[CLERK_DIAG] withCoreUserGuard render', {
component: Component.displayName || Component.name,
hasUser: !!user,
userId: user?.id,
});

if (!user) {
return null;
}
Expand Down
Loading