🔧

【Supabase】"TypeError: Key for the ES256 algorithm ..."の解決方法

に公開
2026/02/20

Supabase Edge Functions がローカルで動かなくなった

少し手こずったのでメモとして残しておきます。

いつも通り、 supabase start でローカル環境を立ち上げて、 Edge Functions を動かそうとしたところ、以下のエラーが発生しました。

TypeError: Key for the ES256 algorithm must be of type CryptoKey. Received an instance of Uint8Array

スタックトレースを見ると、どうやら Edge Functions のランタイムで使用されている jose ライブラリ起因のエラーのようです。

調べてみたところ、以下の記事がヒットしました。

https://medium.com/@sinancsoysal/fixing-jwserror-jwsinvalidsignature-in-self-hosted-supabase-edge-functions-d4799caf4c9f

Supabase の非対称 JWT への移行に伴う破壊的変更が原因のようです。

解決方法

解決方法は 2 つあります。

  1. Supabase CLI のバージョンを 2.69.0 にダウングレードする
  2. カスタム JWT 検証ハンドラを実装する

1. Supabase CLI のバージョンを 2.69.0 にダウングレードする

1 つ目の方法は簡単ですが、 Supabase CLI のアップデートが出来なくなるため、とりあえず動かすための応急処置です。

  1. 以前のバージョンの Supabase CLI を使用して Supabase を開始する
npx supabase@2.69.0 start
  1. Supabase CLI のバージョンと同じバージョンで Edge Functions を立ち上げる
npx supabase@2.69.0 functions serve

2. カスタム JWT 検証ハンドラを実装する

2 つ目の方法は、 Supabase CLI の最新バージョンを使用し続けることができますが、少し手間がかかります。

  1. カスタム JWT 検証ハンドラを実装する
supabase/functions/_shared/jwt/default.ts
// Default supabase JWT verification
// Use this template to validate tokens issued by Supabase default auth
import * as jose from "jsr:@panva/jose@6";

const SUPABASE_JWT_ISSUER = Deno.env.get("SB_JWT_ISSUER") ??
  Deno.env.get("SUPABASE_URL") + "/auth/v1";

const SUPABASE_JWT_KEYS = jose.createRemoteJWKSet(
  new URL(Deno.env.get("SUPABASE_URL")! + "/auth/v1/.well-known/jwks.json"),
);

function getAuthToken(req: Request) {
  const authHeader = req.headers.get("authorization");
  if (!authHeader) {
    throw new Error("Missing authorization header");
  }
  const [bearer, token] = authHeader.split(" ");
  if (bearer !== "Bearer") {
    throw new Error(`Auth header is not 'Bearer {token}'`);
  }
  return token;
}

function verifySupabaseJWT(jwt: string) {
  return jose.jwtVerify(jwt, SUPABASE_JWT_KEYS, {
    issuer: SUPABASE_JWT_ISSUER,
  });
}

// Validates authorization header
export async function AuthMiddleware(
  req: Request,
  next: (req: Request) => Promise<Response>,
) {
  if (req.method === "OPTIONS") return await next(req);
  try {
    const token = getAuthToken(req);
    const isValidJWT = await verifySupabaseJWT(token);
    if (isValidJWT) return await next(req);
    return Response.json({ msg: "Invalid JWT" }, {
      status: 401,
    });
  } catch (e) {
    return Response.json({ msg: e?.toString() }, {
      status: 401,
    });
  }
}
  1. カスタム JWT 検証ハンドラを Edge Functions で使用する
+ import { AuthMiddleware } from "../_shared/jwt/default.ts";

interface reqPayload {
  name: string;
}
Deno.serve((r) =>
+ AuthMiddleware(r, async (req) => {
    const { name }: reqPayload = await req.json();
    const data = {
      message: `Hello ${name} from foo!`,
    };
    return Response.json(data);
+ });
);
  1. デフォルトの JWT 検証ハンドラを無効化する
supabase/config.toml
...

[functions.hello]
enabled = true
- verify_jwt = true
+ verify_jwt = false
import_map = "./functions/hello/deno.json"
# Uncomment to specify a custom file path to the entrypoint.
# Supported file extensions are: .ts, .js, .mjs, .jsx, .tsx
entrypoint = "./functions/hello/index.ts"
# Specifies static files to be bundled with the function. Supports glob patterns.
# For example, if you want to serve static HTML pages in your function:
# static_files = [ "./functions/hello/*.html" ]
  1. 環境変数を設定する
supabase/functions/.env
SB_PUBLISHABLE_KEY=sb_publishable_<key>
SB_JWT_ISSUER=<url>/auth/v1

環境変数の値は、 supabase status コマンドで確認できます。

  1. Edge Functions を立ち上げる
npx supabase functions serve

おわりに

同じ問題に直面している方の参考になれば幸いです。

追記(2026/02/20)

Supabase 公式ページにも案内が掲載されていました。

https://supabase.com/docs/guides/functions/auth

内容を見る限り、今後しばらくは手動での JWT 検証が必要そうなので、少々面倒ですが、カスタム JWT 検証ハンドラの実装をおすすめします。

GitHubで編集を提案

Discussion