Skip to content

Trim PAT token and reject internal whitespace with a coded error#183

Draft
parthban-db wants to merge 1 commit into
mainfrom
parthban-db/stack/bugbash-bug7-pat-trim
Draft

Trim PAT token and reject internal whitespace with a coded error#183
parthban-db wants to merge 1 commit into
mainfrom
parthban-db/stack/bugbash-bug7-pat-trim

Conversation

@parthban-db
Copy link
Copy Markdown
Contributor

@parthban-db parthban-db commented Jun 3, 2026

🥞 Stacked PR

Use this link to review incremental changes.


Summary

newPatCredentials now trims surrounding whitespace from the token and rejects tokens containing internal whitespace. A token read with a trailing newline (e.g. $(cat token)) now produces a clean Bearer <token> header instead of a malformed one.

Why

Previously newPatCredentials only checked token === '' and interpolated the raw value straight into the header, so a token with a trailing newline silently produced Bearer dapiABC\n and the server rejected the request with an opaque auth failure. Trimming fixes the common newline case, and an explicit check turns the genuinely broken case (whitespace inside the token) into a fast, actionable error instead of a confusing server-side rejection.

What changed

  • Trim leading/trailing whitespace from the token before building the Bearer header.
  • Reject a token that is empty after trimming with PatCredentialsError('TOKEN_REQUIRED').
  • Reject a token with internal whitespace with PatCredentialsError('TOKEN_MALFORMED').
  • Builds on the exported PatCredentialsError introduced in [auth] Throw exported, coded PatCredentialsError from PAT credentials #186 (this branch adds the TOKEN_MALFORMED path and the trim logic).
  • Adds table-driven test coverage for trailing-newline, surrounding-whitespace, whitespace-only, and internal-whitespace tokens in packages/auth/tests/credentials/pat.test.ts.
  • Deliberately diverges from the Go SDK (config/auth_pat.go), which neither trims nor validates whitespace; the JS SDK surfaces the malformed-header failure to callers, so trimming plus a coded error is more useful here.

Validated: npm run build, npm test, npm run test:browser, npm run typecheck, and npm run lint for @databricks/sdk-auth all pass.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

Please ensure that the NEXT_CHANGELOG.md file is updated with any relevant changes.
If this is not necessary for your PR, please include the following in your PR description:
NO_CHANGELOG=true
and rerun the job.

1 similar comment
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

Please ensure that the NEXT_CHANGELOG.md file is updated with any relevant changes.
If this is not necessary for your PR, please include the following in your PR description:
NO_CHANGELOG=true
and rerun the job.

@parthban-db parthban-db force-pushed the parthban-db/stack/bugbash-bug7-pat-trim branch from 8977991 to 15486da Compare June 4, 2026 10:55
@parthban-db parthban-db changed the base branch from main to parthban-db/stack/bugbash-bug12-pat-error June 4, 2026 10:57
@parthban-db parthban-db force-pushed the parthban-db/stack/bugbash-bug12-pat-error branch from b1275df to 3fdf1e6 Compare June 4, 2026 12:07
@parthban-db parthban-db force-pushed the parthban-db/stack/bugbash-bug7-pat-trim branch from 15486da to 6f8e509 Compare June 4, 2026 12:08
Base automatically changed from parthban-db/stack/bugbash-bug12-pat-error to main June 4, 2026 12:56
`newPatCredentials` now trims surrounding whitespace from the token before validating, so a value with a trailing newline (e.g. read from a file or env var) produces a valid `Authorization` header. A token containing internal whitespace now throws `PatCredentialsError` with code `TOKEN_MALFORMED` instead of silently building a malformed `Bearer` header.

Co-authored-by: Isaac
@parthban-db parthban-db force-pushed the parthban-db/stack/bugbash-bug7-pat-trim branch from 6f8e509 to 3fe7378 Compare June 4, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant