question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support empty security

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When I add global security. I can’t exclude one api from the @ApiSecurity().

const docBuilder = new DocumentBuilder().addSecurityRequirements('bearer')

Expected behavior

We can define global security follow the document openapi authentication and exclude from the path by set security: [] # No security.

Now we can use SetMetadata('swagger/apiSecurity', []) to hack this feature when define ClassDecorator. But the this data has been omit when define the MethodDecorator.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
rajp33commented, Mar 16, 2022

For anyone that still needs a workaround, I modified the above code to get it working:

const PublicAuthMiddleware = SetMetadata(IS_PUBLIC_KEY, true);
const PublicAuthSwagger = SetMetadata('swagger/apiSecurity', ['public']);

export const Public = () => applyDecorators(
  PublicAuthMiddleware,
  PublicAuthSwagger,
)

And then where my server is initialized:

const document = SwaggerModule.createDocument(app, config);

Object.values((document as OpenAPIObject).paths).forEach((path: any) => {
    Object.values(path).forEach((method: any) => {
    if (Array.isArray(method.security) && method.security.includes('public')) {
        method.security = [];
    }
    });
});
...
1reaction
TBG-FRcommented, Nov 2, 2021

Having followed the officiel NestJS docs to secure my API, all my routes are protected by an API-Key, and some of them can avoid it, with a Public() decorator (as explained in the docs)

In order to get Swagger to use that API Key, and not wanting to manually add @ApiSecurity() on all my routes excepted the few that are public, I used the following :

const PublicAuthMiddleware = () => SetMetadata(IS_PUBLIC_KEY, true);
const PublicAuthSwagger = () => SetMetadata('swagger/apiSecurity', ['']);
export const Public = () => applyDecorators(
  PublicAuthMiddleware,
  PublicAuthSwagger,
) 

in addition to

  const swaggerConfig = new DocumentBuilder()
  [...]
  .addSecurity('ApiKeyAuth', {type: 'apiKey', name: 'Authorization', in: 'header', description: "A valid issued API Key"})
  .addSecurityRequirements('ApiKeyAuth')

However, I don’t understand where I need to put the ApiFixEmptySecurity ? (@zzdhidden can you explain ? Your code piece was already really useful, thanks for that !)

@kamilmysliwiec I understand your answer in #1319 but I think one more decorator would be useful : sometimes you’ll want to have @ApiSecurity() (or ApiBearerAuth() or anything) enabled only on some of your routes, and sometimes you’ll want to enable Auth globally and disable it on some of your routes, that’s only one decorator, for a “big” benefit, no ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Windows Security is Empty - Microsoft Community
Let's try to perform the troubleshooting methods below and check if any of the resolves fixes the issue. 1. Boot your computer to...
Read more >
How to fix empty Windows 11 Security or Defender App?
1. First Fix: Update the Operating System. 2. Repair and Reset the Windows Security App. 3. Reinstall the Security App via Windows Terminal...
Read more >
Control traffic to resources using security groups
Use security groups to control the inbound and outbound traffic for associated resources.
Read more >
Guardium Policy Rule Fires on Empty Group - IBM
IBM Security Guardium policy rules may fire unexpectedly if a condition contains an empty group.
Read more >
Empty ‒ Qlik Sense for administrators - Qlik | Help
Empty (). Action: update. This rule lets the user update an app, provided that the app is not ... Conditions (Advanced view) ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found