Serverless-offline: Error when custom authorizer's Resource is '*'

Created on 5 Apr 2018  路  3Comments  路  Source: dherault/serverless-offline

The PR #373 added support for complex resources when a wildcard is present. However it fails when the Resource is just a '*'.

// ./src/authMatchPolicyResource.js
module.exports = (policyResource, resource) => {
  if (policyResource === resource) {
    return true;
  }
  else if (policyResource.includes('*')) {
    //Policy contains a wildcard resource
    const splitPolicyResource = policyResource.split(':');
    const splitResource = resource.split(':');
    //These variables contain api id, stage, method and the path
    //for the requested resource and the resource defined in the policy
    const splitPolicyResourceApi = splitPolicyResource[5].split('/'); // this line fails
    const splitResourceApi = splitResource[5].split('/');

    return splitPolicyResourceApi.every((resourceFragment, index) => {
      if (splitResourceApi.length >= index + 1) {
        return (splitResourceApi[index] === resourceFragment || resourceFragment === '*');
      }
      //The last position in the policy resource is a '*' it matches all
      //following resource fragments

      return splitPolicyResourceApi[splitPolicyResourceApi.length - 1] === '*';
    });
  }

  return false;
};

I think it fails in line 11 because it tries to split the index [5] of an already splitted string which is not even an array. So it fails with TypeError: Uncaught error: Cannot read property 'split' of undefined

Maybe I'm doing something wrong, but in the case it is indeed a bug: Is there a fix for this already in place or should I make a PR for this?

bug help wanted

Most helpful comment

I think an if statement should do the trick, but I'm open to better patterns

All 3 comments

I think an if statement should do the trick, but I'm open to better patterns

I'm also having the same issue. The if statement would fix the problem however I have altered my policy to arn:aws:execute-api:*:*:*/*/* in the meantime.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Looveh picture Looveh  路  4Comments

adambiggs picture adambiggs  路  4Comments

stunningpixels picture stunningpixels  路  3Comments

dnalborczyk picture dnalborczyk  路  3Comments

balintpeak picture balintpeak  路  4Comments