[GH-ISSUE #7993] SAML metadata endpoint producing run-time error #10966

Closed
opened 2026-04-13 07:21:23 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @formatlos on GitHub (Feb 16, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/7993

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

When calling the /sso/saml2/sp/metadata of my configured SAML sso I do get the following error

TypeError: saml.SPMetadata is not a function 

That's the problematic code:

import * as saml from "samlify";
...
saml.SPMetadata(...)

When debugging into the code I see that the SPMetadata is not a named export but rather exported as saml.default.SPMetadata. I assume that the bundle (ESM in our case) is done with the esModuleInterop: true flag.

Current vs. Expected behavior

Not throwing any runtime error

What version of Better Auth are you using?

1.4.18

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 24.6.0: Wed Nov  5 21:33:58 PST 2025; root:xnu-11417.140.69.705.2~1/RELEASE_ARM64_T6000",
    "release": "24.6.0",
    "cpuCount": 10,
    "cpuModel": "Apple M1 Max",
    "totalMemory": "64.00 GB",
    "freeMemory": "7.97 GB"
  },
  "node": {
    "version": "v22.14.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "10.9.2"
  },
  "frameworks": [
    {
      "name": "fastify",
      "version": "^5.5.0"
    }
  ],
  "databases": [
    {
      "name": "pg",
      "version": "^8.17.2"
    },
    {
      "name": "postgres",
      "version": "^3.4.8"
    },
    {
      "name": "@prisma/client",
      "version": "^6.18.0"
    },
    {
      "name": "kysely",
      "version": "^0.28.9"
    }
  ],
  "betterAuth": {
    "version": "^1.4.18",
    "config": {
      "basePath": "/auth",
      "baseURL": "http://localhost:4000",
      "secret": "[REDACTED]",
      "logger": {
        "disabled": false,
        "disableColors": true,
        "level": "debug"
      },
      "telemetry": {
        "enabled": false
      },
      "emailAndPassword": {
        "enabled": true,
        "disableSignUp": true
      },
      "database": {
        "_events": {},
        "_eventsCount": 0,
        "options": {
          "connectionString": "[REDACTED]",
          "max": 10,
          "min": 0,
          "maxUses": null,
          "allowExitOnIdle": false,
          "maxLifetimeSeconds": 0,
          "idleTimeoutMillis": 10000
        },
        "_clients": [],
        "_idle": [],
        "_expired": {},
        "_pendingQueue": [],
        "ending": false,
        "ended": false
      },
      "trustedOrigins": [
        "http://localhost:4000"
      ],
      "onAPIError": {
        "errorURL": "/auth/error"
      },
      "plugins": [...]
    }
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @formatlos on GitHub (Feb 16, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/7993 ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce When calling the `/sso/saml2/sp/metadata` of my configured SAML sso I do get the following error ``` TypeError: saml.SPMetadata is not a function ``` That's the problematic code: ``` import * as saml from "samlify"; ... saml.SPMetadata(...) ``` When debugging into the code I see that the `SPMetadata` is not a named export but rather exported as `saml.default.SPMetadata`. I assume that the bundle (ESM in our case) is done with the `esModuleInterop: true` flag. ### Current vs. Expected behavior Not throwing any runtime error ### What version of Better Auth are you using? 1.4.18 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 24.6.0: Wed Nov 5 21:33:58 PST 2025; root:xnu-11417.140.69.705.2~1/RELEASE_ARM64_T6000", "release": "24.6.0", "cpuCount": 10, "cpuModel": "Apple M1 Max", "totalMemory": "64.00 GB", "freeMemory": "7.97 GB" }, "node": { "version": "v22.14.0", "env": "development" }, "packageManager": { "name": "npm", "version": "10.9.2" }, "frameworks": [ { "name": "fastify", "version": "^5.5.0" } ], "databases": [ { "name": "pg", "version": "^8.17.2" }, { "name": "postgres", "version": "^3.4.8" }, { "name": "@prisma/client", "version": "^6.18.0" }, { "name": "kysely", "version": "^0.28.9" } ], "betterAuth": { "version": "^1.4.18", "config": { "basePath": "/auth", "baseURL": "http://localhost:4000", "secret": "[REDACTED]", "logger": { "disabled": false, "disableColors": true, "level": "debug" }, "telemetry": { "enabled": false }, "emailAndPassword": { "enabled": true, "disableSignUp": true }, "database": { "_events": {}, "_eventsCount": 0, "options": { "connectionString": "[REDACTED]", "max": 10, "min": 0, "maxUses": null, "allowExitOnIdle": false, "maxLifetimeSeconds": 0, "idleTimeoutMillis": 10000 }, "_clients": [], "_idle": [], "_expired": {}, "_pendingQueue": [], "ending": false, "ended": false }, "trustedOrigins": [ "http://localhost:4000" ], "onAPIError": { "errorURL": "/auth/error" }, "plugins": [...] } } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 07:21:23 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Feb 16, 2026):

Hi @formatlos! I'm Dosu and I’m helping the better-auth team.

This looks like a legitimate bug in Better Auth's SAML plugin. The issue is that samlify is imported using a namespace import (import * as saml from "samlify"), and then SPMetadata is called directly as saml.SPMetadata() in the metadata endpoint.

As you noted, in ESM environments the exports may be nested under saml.default.SPMetadata rather than saml.SPMetadata, causing the runtime error.

This is a known pattern issue - Better Auth fixed a similar ESM import problem with Zod v4 by switching from a namespace import to a default import. The same fix would likely need to be applied to the samlify imports in the SSO package.

Other users have reported similar issues with the SAML implementation, and since Better Auth transitioned to ESM-only, this type of interop issue can surface depending on how dependencies export their modules.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

<!-- gh-comment-id:3907508894 --> @dosubot[bot] commented on GitHub (Feb 16, 2026): <!-- Greeting --> Hi @formatlos! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This looks like a legitimate bug in Better Auth's SAML plugin. The issue is that [samlify is imported using a namespace import](https://github.com/better-auth/better-auth/blob/d3b579e42a707a9037a9210ad14611366b745d2a/packages/sso/src/index.ts#L4-L4) (`import * as saml from "samlify"`), and then [SPMetadata is called directly as `saml.SPMetadata()`](https://github.com/better-auth/better-auth/blob/d3b579e42a707a9037a9210ad14611366b745d2a/packages/sso/src/routes/sso.ts#L220-L235) in the metadata endpoint. As you noted, in ESM environments the exports may be nested under `saml.default.SPMetadata` rather than `saml.SPMetadata`, causing the runtime error. This is a known pattern issue - Better Auth [fixed a similar ESM import problem with Zod v4](https://github.com/better-auth/better-auth/pull/6833) by switching from a namespace import to a default import. The same fix would likely need to be applied to the samlify imports in the SSO package. [Other users have reported similar issues with the SAML implementation](https://github.com/better-auth/better-auth/issues/6609#issuecomment-3786063676), and since Better Auth [transitioned to ESM-only](https://github.com/better-auth/better-auth/pull/5703), this type of interop issue can surface depending on how dependencies export their modules. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/6ac35ec8-8526-4893-850a-fb0a8511db46?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/6ac35ec8-8526-4893-850a-fb0a8511db46?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/6ac35ec8-8526-4893-850a-fb0a8511db46?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/6ac35ec8-8526-4893-850a-fb0a8511db46?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/6ac35ec8-8526-4893-850a-fb0a8511db46?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/6ac35ec8-8526-4893-850a-fb0a8511db46?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/6ac35ec8-8526-4893-850a-fb0a8511db46?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20looks%20like%20a%20legitimate%20bug%20in%20Better%20Auth%27s%20SAML%20plugin.%20The%20issue%20is%20that%20%5Bsamlify%20is%20imported%20using%20a%20namespace%20import%5D%28https%3A//github.com/better-auth/better-auth/blob/d3b579e42a707a9037a9210ad14611366b745d2a/packages/sso/src/index.ts%23L4-L4%29%20%28%60import%20%2A%20as%20saml%20from%20%22samlify%22%60%29%2C%20and%20then%20%5BSPMetadata%20is%20called%20directly%20as%20%60saml.SPMetadata%28%29%60%5D%28https%3A//github.com/better-auth/better-auth/blob/d3b579e42a707a9037a9210ad14611366b745d2a/packages/sso/src/routes/sso.ts%23L220-L235%29%20in%20the%20metadata%20endpoint.%0A%0AAs%20you%20noted%2C%20in%20ESM%20environments%20the%20exports%20may%20be%20nested%20under%20%60saml.default.SPMetadata%60%20rather%20than%20%60saml.SPMetadata%60%2C%20causing%20the%20runtime%20error.%0A%0AThis%20is%20a%20known%20pattern%20issue%20-%20Better%20Auth%20%5Bfixed%20a%20similar%20ESM%20import%20problem%20with%20Zod%20v4%5D%28https%3A//github.com/better-auth/better-auth/pull/6833%29%20by%20switching%20from%20a%20namespace%20import%20to%20a%20default%20import.%20The%20same%20fix%20would%20likely%20need%20to%20be%20applied%20to%20the%20samlify%20imports%20in%20the%20SSO%20package.%0A%0A%5BOther%20users%20have%20reported%20similar%20issues%20with%20the%20SAML%20implementation%5D%28https%3A//github.com/better-auth/better-auth/issues/6609%23issuecomment-3786063676%29%2C%20and%20since%20Better%20Auth%20%5Btransitioned%20to%20ESM-only%5D%28https%3A//github.com/better-auth/better-auth/pull/5703%29%2C%20this%20type%20of%20interop%20issue%20can%20surface%20depending%20on%20how%20dependencies%20export%20their%20modules.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/7993)
Author
Owner

@GautamBytes commented on GitHub (Feb 16, 2026):

Hey @formatlos , will look into this!

<!-- gh-comment-id:3909799273 --> @GautamBytes commented on GitHub (Feb 16, 2026): Hey @formatlos , will look into this!
Author
Owner

@himself65 commented on GitHub (Feb 18, 2026):

http://github.com/better-auth/better-auth/pull/8041

<!-- gh-comment-id:3920087453 --> @himself65 commented on GitHub (Feb 18, 2026): http://github.com/better-auth/better-auth/pull/8041
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10966