Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: improved jwt signing key rotation dialog (#36781)

Stojan Dimitrovski4e17ec663242
diff --git a/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/create-key-dialog.tsx b/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/create-key-dialog.tsx
index c0a4bb2bdc2..0a64ac99786 100644
--- a/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/create-key-dialog.tsx
+++ b/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/create-key-dialog.tsx
@@ -1,7 +1,10 @@
-import { useJWTSigningKeyCreateMutation } from 'data/jwt-signing-keys/jwt-signing-key-create-mutation'
-import { JWTAlgorithm } from 'data/jwt-signing-keys/jwt-signing-keys-query'
+import dayjs from 'dayjs'
+import relativeTime from 'dayjs/plugin/relativeTime'
import { useState } from 'react'
import { toast } from 'sonner'
+
+import { useJWTSigningKeyCreateMutation } from 'data/jwt-signing-keys/jwt-signing-key-create-mutation'
+import { JWTAlgorithm } from 'data/jwt-signing-keys/jwt-signing-keys-query'
import {
Button,
DialogFooter,
@@ -18,6 +21,8 @@ import {
} from 'ui'
import { algorithmDescriptions } from '../algorithm-details'
+dayjs.extend(relativeTime)
+
export const CreateKeyDialog = ({
projectRef,
onClose,
@@ -29,10 +34,24 @@ export const CreateKeyDialog = ({
const { mutate, isLoading: isLoadingMutation } = useJWTSigningKeyCreateMutation({
onSuccess: () => {
+ toast.success('Standby key created successfully')
onClose()
},
onError: (error) => {
- toast.error(`Failed to add new standby key: ${error.message}`)
+ let errorMessage = error.message
+
+ if (errorMessage.includes('Please wait until')) {
+ const dateString = errorMessage
+ .replace('Please wait until ', '')
+ .replace('before attempting this request again.', '')
+ .trim()
+ const date = dayjs(dateString)
+
+ if (date.isValid()) {
+ errorMessage = `Please wait for ${date.fromNow(true)} before attempting this request again.`
+ }
+ }
+ toast.error(`Failed to add new standby key: ${errorMessage}`)
},
})
diff --git a/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/index.tsx b/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/index.tsx
index 222edf7b24a..e6691f6da4e 100644
--- a/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/index.tsx
+++ b/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/index.tsx
@@ -1,6 +1,5 @@
import { AnimatePresence, motion } from 'framer-motion'
import {
- ArrowRight,
CircleArrowUp,
Eye,
FileKey,
@@ -15,6 +14,7 @@ import { useMemo, useState } from 'react'
import { useParams } from 'common'
import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
+import { useLegacyAPIKeysStatusQuery } from 'data/api-keys/legacy-api-keys-status-query'
import { useJWTSigningKeyDeleteMutation } from 'data/jwt-signing-keys/jwt-signing-key-delete-mutation'
import { useJWTSigningKeyUpdateMutation } from 'data/jwt-signing-keys/jwt-signing-key-update-mutation'
import {
@@ -24,9 +24,15 @@ import {
} from 'data/jwt-signing-keys/jwt-signing-keys-query'
import { useLegacyJWTSigningKeyCreateMutation } from 'data/jwt-signing-keys/legacy-jwt-signing-key-create-mutation'
import { useLegacyJWTSigningKeyQuery } from 'data/jwt-signing-keys/legacy-jwt-signing-key-query'
-import { useLegacyAPIKeysStatusQuery } from 'data/api-keys/legacy-api-keys-status-query'
import { useFlag } from 'hooks/ui/useFlag'
import {
+ AlertDialog,
+ AlertDialogCancel,
+ AlertDialogContent,
+ AlertDialogDescription,
+ AlertDialogFooter,
+ AlertDialogHeader,
+ AlertDialogTitle,
Badge,
Button,
Card,
@@ -34,7 +40,6 @@ import {
cn,
Dialog,
DialogContent,
- DialogDescription,
DialogFooter,
DialogHeader,
DialogSection,
@@ -44,34 +49,21 @@ import {
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
- Select_Shadcn_,
- SelectContent_Shadcn_,
- SelectItem_Shadcn_,
- SelectTrigger_Shadcn_,
- SelectValue_Shadcn_,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
- AlertDialog,
- AlertDialogAction,
- AlertDialogCancel,
- AlertDialogContent,
- AlertDialogDescription,
- AlertDialogFooter,
- AlertDialogHeader,
- AlertDialogTitle,
} from 'ui'
import TextConfirmModal from 'ui-patterns/Dialogs/TextConfirmModal'
-import { algorithmDescriptions, algorithmLabels } from '../algorithm-details'
import { AlgorithmHoverCard } from '../algorithm-hover-card'
import { statusColors, statusLabels } from '../jwt.constants'
import { SigningKeysComingSoonBanner } from '../signing-keys-coming-soon'
import { StartUsingJwtSigningKeysBanner } from '../start-using-keys-banner'
import { ActionPanel } from './action-panel'
import { CreateKeyDialog } from './create-key-dialog'
+import { RotateKeyDialog } from './rotate-key-dialog'
import { SigningKeyRow } from './signing-key-row'
const MotionTableRow = motion(TableRow)
@@ -187,19 +179,6 @@ export default function JWTSecretKeysTable() {
}
}
- const handleRotateKey = async () => {
- try {
- await updateMutation.mutateAsync({
- projectRef: projectRef!,
- keyId: standbyKey!.id,
- status: 'in_use',
- })
- resetDialog()
- } catch (error) {
- console.error('Failed to rotate key', error)
- }
- }
-
if (isLoading) {
return <GenericSkeletonLoader />
}
@@ -612,12 +591,12 @@ export default function JWTSecretKeysTable() {
<DialogTitle>Start using new JWT signing keys</DialogTitle>
</DialogHeader>
<DialogSectionSeparator />
- <DialogSection>
+ <DialogSection className="flex flex-col gap-2 text-sm text-foreground-light">
<p>
Your project today uses a legacy symmetric JWT secret to create JWTs. To be able to
- use an asymmetric JWT signing key you first have to migrate it to the new system. This
- change does not cause any downtime on your project.
+ use an asymmetric JWT signing key you first have to migrate it to the new approach.
</p>
+ <p>This change does not cause any downtime on your project.</p>
</DialogSection>
<DialogFooter>
<Button
@@ -637,132 +616,18 @@ export default function JWTSecretKeysTable() {
</DialogContent>
</Dialog>
- <Dialog open={shownDialog === 'rotate'} onOpenChange={resetDialog}>
- <DialogContent className="sm:max-w-[425px]">
- <DialogHeader>
- <DialogTitle>Rotate Key</DialogTitle>
- </DialogHeader>
- <DialogSectionSeparator />
- <DialogSection>
- <DialogDescription>
- {standbyKey ? (
- <>
- The standby key ({algorithmLabels[standbyKey.algorithm]}) will be promoted to 'In
- use'. This will:
- <ul className="list-disc pl-4 mt-2 space-y-2">
- <li>Change the current standby key to 'In use'</li>
- <li>Move the current 'In use' key to 'Previously used'</li>
- <li>Move any 'Previously used' key to 'Revoked'</li>
- </ul>
- </>
- ) : (
- <>
- Since there is no standby key, you need to choose an algorithm for the new key:
- <div className="mt-4 space-y-4">
- <Select_Shadcn_
- value={newKeyAlgorithm}
- onValueChange={(value: JWTAlgorithm) => setNewKeyAlgorithm(value)}
- >
- <SelectTrigger_Shadcn_ id="rotateAlgorithm">
- <SelectValue_Shadcn_ placeholder="Select algorithm" />
- </SelectTrigger_Shadcn_>
- <SelectContent_Shadcn_>
- <SelectItem_Shadcn_ value="HS256">HS256 (Symmetric)</SelectItem_Shadcn_>
- <SelectItem_Shadcn_ value="ES256">ES256 (ECC)</SelectItem_Shadcn_>
- <SelectItem_Shadcn_ value="RS256">RS256 (RSA)</SelectItem_Shadcn_>
- <SelectItem_Shadcn_ value="EdDSA">EdDSA (Ed25519)</SelectItem_Shadcn_>
- </SelectContent_Shadcn_>
- </Select_Shadcn_>
- <p className="text-sm text-foreground-light">
- {algorithmDescriptions[newKeyAlgorithm]}
- </p>
- </div>
- </>
- )}
- </DialogDescription>
- </DialogSection>
- <DialogFooter>
- <Button onClick={() => setShownDialog('confirm-rotate')}>Review Rotation</Button>
- </DialogFooter>
- </DialogContent>
- </Dialog>
-
- <Dialog open={shownDialog === 'confirm-rotate'} onOpenChange={resetDialog}>
- <DialogContent className="sm:max-w-lg overflow-hidden">
- <DialogHeader>
- <DialogTitle>Confirm key rotation</DialogTitle>
- <DialogDescription>
- Review the key rotation process below. Ensure your application's components have
- already picked up and are trusting your standby key to avoid downtime.
- </DialogDescription>
- </DialogHeader>
- <DialogSectionSeparator />
- <DialogSection className="relative bg">
- <div className="relative flex flex-col items-center space-y-6 py-6">
- {standbyKey ? (
- <div className="flex items-center">
- <Badge
- className={cn(statusColors['standby'], 'px-3 py-1 space-x-1 items-baseline')}
- >
- <Timer size={13} className="self-center" />
- <span>STANDBY KEY</span>
- <span className="text-xs font-mono text-foreground-light">
- {algorithmLabels[standbyKey!.algorithm]}
- </span>
- </Badge>
- <ArrowRight className="h-4 w-4 mx-1 text-foreground-light" />
- <Badge className={cn(statusColors['in_use'], 'px-3 py-1 space-x-1')}>
- <Key size={13} />
- <span>CURRENTLY USED</span>
- </Badge>
- </div>
- ) : (
- <div className="flex items-center space-x-2">
- <Badge
- className={cn(
- 'bg-surface-300 bg-opacity-100 text-foreground border border-foreground-muted px-3 py-1 space-x-1'
- )}
- >
- <Key size={13} className="mr-1.5" />
- <span>New Key</span>
- <span className="text-xs font-mono text-foreground-light">
- {algorithmLabels[newKeyAlgorithm]}
- </span>
- </Badge>
- <ArrowRight className="h-4 w-4 text-foreground-light" />
- <Badge className={cn(statusColors['in_use'], 'px-3 py-1')}>
- <Key size={13} className="mr-1.5" />
- CURRENTLY USED
- </Badge>
- </div>
- )}
-
- <div className="flex items-center">
- <Badge className={cn(statusColors['in_use'], 'px-3 py-1 space-x-1 items-baseline')}>
- <Key size={13} className="self-center" />
- <span>CURRENTLY USED</span>
- <span className="text-xs font-mono text-foreground-light">
- {inUseKey?.algorithm && algorithmLabels[inUseKey.algorithm]}
- </span>
- </Badge>
- <ArrowRight className="h-4 w-4 mx-1 text-foreground-light" />
- <Badge className={cn(statusColors['previously_used'], 'px-3 py-1 space-x-1')}>
- <Timer size={13} />
- <span>PREVIOUS KEY</span>
- </Badge>
- </div>
- </div>
- </DialogSection>
- <DialogFooter>
- <Button type="outline" onClick={resetDialog}>
- Cancel
- </Button>
- <Button onClick={() => handleRotateKey()} loading={isLoadingMutation}>
- Confirm rotation
- </Button>
- </DialogFooter>
- </DialogContent>
- </Dialog>
+ {standbyKey && inUseKey && projectRef && (
+ <Dialog open={shownDialog === 'rotate'} onOpenChange={resetDialog}>
+ <DialogContent className="sm:max-w-[425px]">
+ <RotateKeyDialog
+ projectRef={projectRef}
+ standbyKey={standbyKey}
+ inUseKey={inUseKey}
+ onClose={resetDialog}
+ />
+ </DialogContent>
+ </Dialog>
+ )}
<Dialog open={shownDialog === 'key-details'} onOpenChange={resetDialog}>
<DialogContent className="sm:max-w-lg">
diff --git a/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/rotate-key-dialog.tsx b/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/rotate-key-dialog.tsx
new file mode 100644
index 00000000000..38bedabcb14
--- /dev/null
+++ b/apps/studio/components/interfaces/JwtSecrets/jwt-secret-keys-table/rotate-key-dialog.tsx
@@ -0,0 +1,279 @@
+import { ButtonTooltip } from 'components/ui/ButtonTooltip'
+import { useEdgeFunctionsQuery } from 'data/edge-functions/edge-functions-query'
+import { useJWTSigningKeyUpdateMutation } from 'data/jwt-signing-keys/jwt-signing-key-update-mutation'
+import { JWTSigningKey } from 'data/jwt-signing-keys/jwt-signing-keys-query'
+import { ArrowRight, Info, Key, Timer } from 'lucide-react'
+import { useState } from 'react'
+import { toast } from 'sonner'
+import {
+ Badge,
+ Button,
+ Checkbox_Shadcn_,
+ cn,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogSection,
+ DialogSectionSeparator,
+ DialogTitle,
+ Label_Shadcn_,
+ Skeleton,
+} from 'ui'
+import { algorithmLabels } from '../algorithm-details'
+import { statusColors } from '../jwt.constants'
+
+export function RotateKeyDialog({
+ projectRef,
+ standbyKey,
+ inUseKey,
+ onClose,
+}: {
+ projectRef: string
+ standbyKey: JWTSigningKey
+ inUseKey: JWTSigningKey
+ onClose: () => void
+}) {
+ const [isStandbyUnderstood, setStandbyUnderstood] = useState(false)
+ const [isPreviouslyUsedUnderstood, setPreviouslyUsedUnderstood] = useState(false)
+ const [isEdgeFunctionsVerifyJWTUnderstood, setEdgeFunctionsVerifyJWTUnderstood] = useState(false)
+
+ const { data: edgeFunctions, isLoading: isLoadingEdgeFunctions } = useEdgeFunctionsQuery({
+ projectRef,
+ })
+
+ const { mutate, isLoading: isLoadingMutation } = useJWTSigningKeyUpdateMutation({
+ onSuccess: () => {
+ toast.success('Signing key rotated successfully')
+ onClose()
+ },
+ onError: (error) => {
+ toast.error(`Failed to rotate signing key: ${error.message}`)
+ },
+ })
+
+ const verifyJWTEdgeFunctions = edgeFunctions?.filter(({ verify_jwt }) => verify_jwt) ?? []
+
+ return (
+ <>
+ <DialogHeader>
+ <DialogTitle>Rotate JWT signing key</DialogTitle>
+ <DialogDescription>
+ Change the key used by Supabase Auth to create new JSON Web Tokens. Non-expired tokens
+ remain <span className="text-brand">valid and accepted</span>!
+ </DialogDescription>
+ </DialogHeader>
+ <DialogSectionSeparator />
+ <DialogSection className="bg">
+ <div className="grid grid-cols-[minmax(0,1fr)_auto_minmax(0,1fr)] items-center gap-x-4 gap-y-2">
+ <Badge
+ className={cn(
+ statusColors['standby'],
+ 'px-4 py-1 gap-2 flex flex-row items-center uppercase'
+ )}
+ >
+ <Timer className="size-4" />
+ Standby key
+ </Badge>
+ <div>
+ <ArrowRight className="size-4 text-foreground-light" />
+ </div>
+ <div>
+ <Badge
+ className={cn(
+ statusColors['in_use'],
+ 'px-4 py-1 gap-2 flex flex-row items-center uppercase'
+ )}
+ >
+ <Key className="size-4" />
+ Current key
+ </Badge>
+ </div>
+ <div className="text-xs text-foreground-light font-mono text-center">
+ {algorithmLabels[standbyKey.algorithm]}
+ </div>
+ <div />
+ <div />
+
+ <div className="col-span-3" />
+
+ <Badge
+ className={cn(
+ statusColors['in_use'],
+ 'px-4 py-1 gap-2 flex flex-row items-center uppercase'
+ )}
+ >
+ <Key className="size-4" />
+ Current key
+ </Badge>
+ <div>
+ <ArrowRight className="h-4 w-4 text-foreground-light" />
+ </div>
+ <Badge
+ className={cn(
+ statusColors['previously_used'],
+ 'px-4 py-1 gap-2 flex flex-row items-center uppercase'
+ )}
+ >
+ <Timer className="size-4" />
+ Previous key
+ </Badge>
+ <div />
+ <div />
+ <div className="text-xs text-foreground-light font-mono text-center">
+ {algorithmLabels[inUseKey.algorithm]}
+ </div>
+ </div>
+ </DialogSection>
+ <DialogSectionSeparator />
+ <DialogSection className="flex flex-col gap-4">
+ {isLoadingEdgeFunctions ? (
+ <>
+ <Skeleton className="h-4 w-full" />
+ <Skeleton className="h-4 w-full" />
+ <Skeleton className="h-4 w-full" />
+ <Skeleton className="h-4 w-full" />
+ </>
+ ) : (
+ <>
+ <div className="text-sm">To proceed please confirm:</div>
+
+ <Label_Shadcn_
+ htmlFor="understands-standby"
+ className="flex items-top gap-4 text-sm leading-none"
+ >
+ <Checkbox_Shadcn_
+ id="understands-standby"
+ className="mt-0.5"
+ checked={isStandbyUnderstood}
+ onCheckedChange={(value) => setStandbyUnderstood(!!value)}
+ />
+ <p className="text-sm text-foreground-light">
+ All of my application's components have picked up the standby key.
+ </p>
+ <ButtonTooltip
+ type="default"
+ icon={<Info />}
+ className="px-1.5 py-2 mt-0.5"
+ tooltip={{
+ content: {
+ className: 'max-w-[320px] p-4',
+ text: (
+ <p>
+ If your application verifies JWTs on its own in backend servers, functions,
+ lambdas or other such components, ensure that they've picked up and are
+ verifying tokens against the standby key.
+ <br />
+ <br />
+ Recommendation: Periodically fetch the public keys from the project's{' '}
+ <code>jwks.json</code> endpoint.
+ </p>
+ ),
+ },
+ }}
+ />
+ </Label_Shadcn_>
+
+ <Label_Shadcn_
+ htmlFor="understands-previously-used"
+ className="flex items-top gap-4 text-sm leading-none"
+ >
+ <Checkbox_Shadcn_
+ className="mt-0.5"
+ id="understands-previously-used"
+ checked={isPreviouslyUsedUnderstood}
+ onCheckedChange={(value) => setPreviouslyUsedUnderstood(!!value)}
+ />
+ <p className="text-sm text-foreground-light">
+ To invalidate non-expired JWTs I need to explicitly revoke the currently used key.
+ </p>
+ <ButtonTooltip
+ type="default"
+ icon={<Info />}
+ className="px-1.5 py-2 mt-0.5"
+ tooltip={{
+ content: {
+ className: 'max-w-[320px] p-4',
+ text: (
+ <p>
+ Rotating the signing key only changes what key is used by Supabase Auth to
+ issue{' '}
+ <strong>
+ <em>new tokens</em>
+ </strong>
+ .<br />
+ <br />
+ To prevent users from being prematurely signed out, you have to manually
+ revoke the current in use key after rotation.
+ <br />
+ <br />
+ Recommendation: If your JWT expiry time is 1 hour, wait at least 1 hour and
+ 15 minutes before revoking the key.
+ </p>
+ ),
+ },
+ }}
+ />
+ </Label_Shadcn_>
+
+ {verifyJWTEdgeFunctions.length > 0 && (
+ <Label_Shadcn_ htmlFor="edge-functions-verify-jwt" className="flex gap-4 text-sm">
+ <Checkbox_Shadcn_
+ id="edge-functions-verify-jwt"
+ className="mt-0.5"
+ checked={isEdgeFunctionsVerifyJWTUnderstood}
+ onCheckedChange={(value) => setEdgeFunctionsVerifyJWTUnderstood(!!value)}
+ />
+ <p className="text-sm text-foreground-light">
+ The following Edge Functions may stop funtioning as they verify the legacy JWT
+ secret:{' '}
+ {verifyJWTEdgeFunctions
+ .map(({ name }) => <code>{name}</code>)
+ .reduce<
+ React.ReactNode[]
+ >((arr, v) => (arr.length > 0 ? [...arr, ', ', v] : [v]), [])}
+ .
+ </p>
+ <ButtonTooltip
+ type="default"
+ icon={<Info />}
+ className="px-1.5 py-2 mt-0.5"
+ tooltip={{
+ content: {
+ className: 'max-w-[320px] p-4',
+ text: (
+ <p>
+ Edge Functions can verify only the legacy JWT secret. Since rotation will
+ change they key that signs JWTs if your Edge Function is set up to reject
+ JWTs not signed by the legacy JWT secret they might stop functioning.
+ <br />
+ <br />
+ Recommendation: Change all of your Edge Functions to use the{' '}
+ <code>--no-verify-jwt</code> option and implement JWT verification logic
+ within the function's body.
+ </p>
+ ),
+ },
+ }}
+ />
+ </Label_Shadcn_>
+ )}
+ </>
+ )}
+ </DialogSection>
+ <DialogFooter>
+ <Button
+ onClick={() => mutate({ projectRef, keyId: standbyKey.id, status: 'in_use' })}
+ disabled={
+ isLoadingEdgeFunctions ||
+ !isPreviouslyUsedUnderstood ||
+ !isStandbyUnderstood ||
+ (!!verifyJWTEdgeFunctions.length && !isEdgeFunctionsVerifyJWTUnderstood)
+ }
+ loading={isLoadingMutation}
+ >
+ Rotate signing key
+ </Button>
+ </DialogFooter>
+ </>
+ )
+}