Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated/Erroneous Authorization Field (Header field being used as a parameter) #3252

Open
2 of 4 tasks
mwarner1 opened this issue Jan 17, 2025 · 0 comments
Open
2 of 4 tasks

Comments

@mwarner1
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I am using the @headers("Authorization") annotation to inject that header into a method inside a TypeScript class, and it works as expected. However, NestJS is using this annotation and presenting it as a required header field, but then does not actually send that field in the API call.

I have main.ts configured so that the swagger UI page has the "Authorize" button, and entering a value there causes the token to be sent correctly.

const config = new DocumentBuilder()
        .setTitle("Some API")
        .setDescription("The API")
        .setVersion('1.0')
        .addBearerAuth({
            type: "http",
            scheme: "bearer",
            bearerFormat: "JWT",
            in: "header",
            name: "JWT",
            description: "Enter your Bearer token",
        }, "Authorization")
        .addSecurityRequirements("Authorization")
        .build();
    const documentFactory = () => SwaggerModule.createDocument(app, config);
    SwaggerModule.setup("v1/api", app, documentFactory);

However, that makes the value under Parameters unnecessary and even wrong (since it isn't sent anyway). In the screenshot below I did not use the Authorization button at the top of the screen to "Authenticate" in order to illustrate that the value in Parameters is not useful.

Image

@Controller()
export class UserCredentialController {
@Get(`/v1/auth/readlogin`)
async getOwnUserLoginInfo(@Headers("Authorization") authHeader: string) {
    if (!authHeader) {
        throw new UnauthorizedException("No authorization header found");
    }
    // Rest of code trimmed
}
// omitted

I have tried various combinations of @Api annotations to no avail. Ideally there would be a way to suppress the unused Parameters field. The closest I can get is to make the field optional using @ApiHeader({name: "Authorization", required: false}), but again, this is wrong since anything entered in that field isn't actually sent anyway.

Minimum reproduction code

https://github.com/mwarner1/swagger-bug-demo

Steps to reproduce

No response

Expected behavior

Swagger should not use @Header fields to define parameters.

Package version

11.0.1

NestJS version

4.0.0

Node.js version

20.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant