0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00
mastra/auth
mastra-platform[bot] 33fba78931
fix: make URL slash trimming and PR number parsing linear-time (#30577)
## Summary

Resolves four open CodeQL code-scanning findings.

| Ticket | Alert | Rule | Location | Change |
| --- | --- | --- | --- | --- |
| SEC-326 |
[#988](https://github.com/mastra-ai/mastra/security/code-scanning/988) |
js/polynomial-redos | `packages/core/src/llm/model/gateways/mastra.ts` |
`/\/+$/` trailing-slash trim replaced with an end-of-string scan |
| SEC-325 |
[#987](https://github.com/mastra-ai/mastra/security/code-scanning/987) |
js/polynomial-redos | `mastracode/factory/src/boards/semantics.ts` |
`/(\d+)$/` replaced with a `trailingDigits` scan from the end |
| SEC-316 |
[#985](https://github.com/mastra-ai/mastra/security/code-scanning/985) |
js/polynomial-redos | `auth/supabase/src/index.ts` | `/\/+$/`
trailing-slash trim replaced with an end-of-string scan |
| SEC-316 |
[#986](https://github.com/mastra-ai/mastra/security/code-scanning/986) |
js/type-confusion-through-parameter-tampering |
`server-adapters/nestjs/src/__tests__/builtin-mcp-oauth.test.ts` | Test
auth provider now checks `typeof token === 'string'` before using it |
| SEC-259 |
[#752](https://github.com/mastra-ai/mastra/security/code-scanning/752) |
js/insufficient-password-hash | `packages/core/src/utils.ts` | No code
change. This is a false positive (see below) |

### ReDoS fixes

These regexes are unanchored at the start. On a string with a long run
of `/` (or digits) followed by another character, the engine retries the
match from every position, which takes quadratic time. On 100k
characters the old code took about 7s for the slash trim and about 10s
for the digit match. The replacements walk back from the end of the
string once and return the same results. That covers the edge cases too:
an empty run, an all-slash or all-digit string, and leading zeros like
`007`.

### #752 is a false positive

`createDeterministicId` already uses SHA-256
(`createHash('sha256').update(input).digest('hex').slice(0, 8)`). It
builds a short, non-secret `tool-<hash>` ID from `tool.description` in
`setVercelToolProperties`, and nothing in it stores a password. CodeQL
raises this rule for any fast hash it thinks might receive a credential.
Here its taint path runs from the API keys passed to the Gateway model
providers in `mastracode/sdk`, through provider/model objects, and not
through a tool description. No hash change would satisfy the rule: it
wants a slow KDF (bcrypt/scrypt/PBKDF2), which would change every
existing tool ID that callers rely on. Recommend dismissing #752 as a
false positive.

## Test plan

- [x] Regression tests with 100k-character pathological inputs in
`mastra.test.ts`, `auth/supabase/src/index.test.ts`, and
`boards/semantics.test.ts`. I checked that each one fails (times out at
7–20s) against the old regex and passes with the fix.
- [x] Behavior tests for gateway base-URL normalization (`///`,
`/v1///`, `/api/`) and for falling back to the PR number from the GitHub
key (`34`, `007`, empty, `12x`)
- [x] `vitest` passes for the gateway tests in `@mastra/core`, all of
`@mastra/auth-supabase`, the semantics and GitLab rules tests in
`@mastra/factory`, and the MCP OAuth test in `@mastra/nestjs`
- [x] `tsc --noEmit` is clean for `packages/core`, `auth/supabase`,
`mastracode/factory`, and `server-adapters/nestjs`
- [x] Changeset added for `@mastra/core`, `@mastra/auth-supabase`, and
`@mastra/factory`. The NestJS change only touches a test, so it has no
changeset

Ref SEC-326
Ref SEC-325
Ref SEC-316
Ref SEC-259


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

**ELI5:** Some code used regular expressions that ran very slowly on
very long strings of `/` or digit characters. This change replaces them
with loops that scan each string once. The results stay the same.

## Overview

The change replaces three end-anchored regular expressions with backward
scans. It also adds a type guard in a NestJS test helper. The diff
touches 9 files, with 93 lines added and 4 removed.

The change addresses three polynomial ReDoS findings and one
type-confusion finding from CodeQL. It does not change code for the
insufficient-password-hash finding (`#752`) on `createDeterministicId`.
The author classifies that finding as a false positive. The hash creates
non-secret tool IDs from tool descriptions. A slow KDF would change
existing IDs.

## Changes by area

- **Gateway base URL**
(`packages/core/src/llm/model/gateways/mastra.ts`): `getBaseUrl` removes
trailing `/` characters with a character-by-character loop. It then
removes a final `/v1` suffix.
- **Supabase URL** (`auth/supabase/src/index.ts`): The provider stores a
normalized `this.url`. A backward loop removes trailing `/` characters.
The Supabase client still receives the original `supabaseUrl`.
- **Pull request number**
(`mastracode/factory/src/boards/semantics.ts`): The new helper
`trailingDigits` scans backward over ASCII digits `0`–`9`.
`boundChangeRequestNumber` calls it only for `github-pr` sources. It
returns `undefined` when no trailing digit exists.
- **NestJS test helper**
(`server-adapters/nestjs/src/__tests__/builtin-mcp-oauth.test.ts`):
`AudienceAuth.authenticateToken` accepts `unknown`. It returns `null`
for non-string values before it parses `aud=`.
- **Changesets**:
- `.changeset/linear-slash-trim.md` sets patch releases for
`@mastra/core` and `@mastra/auth-supabase`. It describes linear-time
trailing-slash trimming. It states that the resulting URLs are
unchanged.
- `.changeset/linear-pr-number.md` sets a patch release for
`@mastra/factory`. It describes linear-time PR-number extraction. It
states that the returned number is unchanged.

## Tests

The test file diffs were not inspected. The following test additions
come from the raw summary and are not verified against the diff:

- A Supabase test that uses a URL with two 100,000-slash runs. It
asserts construction takes under 500 ms.
- Factory tests for trailing-digit extraction, leading zeroes,
`undefined` results, and 100,000-character keys.
- Gateway `fetchProviders` tests for base URL normalization and a
pathological slash-run case.

The PR reports passing Vitest runs and clean `tsc --noEmit` runs for
`packages/core`, `auth/supabase`, `mastracode/factory`, and
`server-adapters/nestjs`. These results were not verified from completed
checks on the current head: not available.

## Notes

- The NestJS change is test-only. It has no changeset.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: mastra-platform[bot] <mastra-platform[bot]@users.noreply.github.com>
Co-authored-by: mastra-platform[bot] <284800079+mastra-platform[bot]@users.noreply.github.com>
Co-authored-by: shreyas-mastra <shreyas@mastra.ai>
History 2026-11-16 22:50:34 -05:00
..
auth0 chore: version - exit prerelease mode 2026-11-12 21:12:29 +00:00
better-auth chore: version packages (alpha) (#30430) 2026-11-16 14:09:31 +01:00
clerk chore: version - exit prerelease mode 2026-11-12 21:12:29 +00:00
cloud chore: version - exit prerelease mode 2026-11-12 21:12:29 +00:00
firebase chore: version - exit prerelease mode 2026-10-28 13:15:52 +00:00
google chore: version - exit prerelease mode 2026-10-28 13:15:52 +00:00
neon chore: version - exit prerelease mode 2026-11-12 21:12:29 +00:00
okta chore: version - exit prerelease mode 2026-11-12 21:12:29 +00:00
studio chore: version - exit prerelease mode 2026-11-12 21:12:29 +00:00
supabase fix: make URL slash trimming and PR number parsing linear-time (#30577) 2026-11-16 22:50:34 -05:00
workos chore: version - exit prerelease mode 2026-11-12 21:12:29 +00:00