micronaut-aws icon indicating copy to clipboard operation
micronaut-aws copied to clipboard

MicronautLambdaAuthenticationFetcher creates empty map instead of null if claims are absent

Open bhimsenp opened this issue 3 years ago • 2 comments

In current implementation of MicronautLambdaAuthenticationFetcher, different types of Authentication objects are returned depending on existence of authorizer in API gateway.

At two places when claims are absent, instead of creating Authentication object with null claims, it creates empty map.

When authorizer is present:

 protected Map<String, Object> attributesOfClaims(CognitoAuthorizerClaims claims) {
        if (claims == null) {
            return Collections.emptyMap();
        }
....}

When authorizer is absent:

return Flowable.just(
        new DefaultAuthentication(
                v,
                Collections.emptyMap()
        )
);

Problem caused because of empty map: In Micronaut Security, there is following code in AbstractSecurityRule

protected List<String> getRoles(Map<String, Object> claims) {
        List<String> roles = new ArrayList<>();
        if (claims == null) {
            roles.add(SecurityRule.IS_ANONYMOUS);
        } else {
            if (!claims.isEmpty()) {
                roles.addAll(rolesFinder.findInClaims(new MapClaims(claims)));
            }
            roles.add(SecurityRule.IS_ANONYMOUS);
            roles.add(SecurityRule.IS_AUTHENTICATED);
        }
        return roles;
    }

If claims are null and not empty, it actually adds IS_ANONYMOUS & IS_ANONYMOUS roles which causes AbstractSecurityRule to allow even when it should not in certain cases.

Proposed solution: Replace empty map with null

bhimsenp avatar Apr 29 '21 15:04 bhimsenp

@sdelamo - Is there any update on this? There is another issue on similar lines.

ashwini-desai avatar Mar 16 '22 10:03 ashwini-desai

I am going to work on AWS once 3.4.0 release is out. I hope to update soon.

sdelamo avatar Mar 16 '22 12:03 sdelamo