swagger-api / swagger-api/swagger-codegen

[typescript-fetch] btoa used for encoding Basic auth doesn't work with multibyte characters

Open
#7,735 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When trying to call an API with HTTP Basic security, if the username or password contains a character with a unicode code point >255, the browser (Chrome) throws a DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range..

That issue is documented by Mozilla The Unicode Problem and they also suggest a few solutions.

Swagger-codegen version

2.3.1, 2.3.0

Swagger declaration file content or url
swagger: "2.0"
info:
  version: "1.0.0"
securityDefinitions:
  UserSecurity:
    type: basic
security:
  - UserSecurity: []
paths:
  /test:
    get:
      responses:
        "200": {}
Command line used for generation

java -jar swagger-codegen-cli-2.3.1.jar generate -i test.yml -l typescript-fetch -o out/

Steps to reproduce
  • generate the code
  • edit api.ts and insert a new line at the end:
    • new DefaultApi({username: 't€stuser', password: 't€estpwd'}).testGet()
  • compile the code
  • execute it in a typical browser
  • an Character Out Of Range Error is thrown
Related issues/PRs

I didn't find anything, but a quick search for btoa suggests that it also affects the typescript-jquery and typescript-angular generators.

Suggest a fix/enhancement

As a quick workaround, I added the b64EncodeUnicode function from the MDN docs. A better fix would probably avoid using btoa altogether, as it isn't available out of the box inside Node.js and we currently work around that, too.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated api.ts from the typescript-fetch generator and locate the btoa call used for Basic authentication. Compare the related typescript-jquery and typescript-angular generators, then reproduce the Unicode-credential case in a browser and Node.js. Done means multibyte usernames and passwords encode without the Character Out Of Range error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.