aws-lambda-stream icon indicating copy to clipboard operation
aws-lambda-stream copied to clipboard

Fix: eventType passed in as array to RULES matches with wrong event

Open shesupplypi opened this issue 3 years ago • 1 comments

Describe the bug

I am sure this is an edge case, but I found this behavior when testing out the code.

The eventType provided in the RULES is matching with the wrong event.type when two names are similar but differ by only the last letter.

For example, when creating a rule with an eventType:

eventType: ['something-updated', 'something-deleted']

and passing an event into the handler with this type:

      id: 'fcc12355-f339-4f71-bbed-eee646535bbb',
      type: 'something-update',

It will match that rule though the event types are not the same.

To Reproduce Steps to reproduce the behavior:

  1. Create an event:
   {
      id: 'fcc12355-f339-4f71-bbed-eee646535bbb',
      type: 'something-update',
   }
  1. Create a rule similar to the type in the event, but with an extra letter at the end, like so:
{
      id: 'p4',
      flavor: materialize,
      eventType: ['something-update**d'**, 'something-deleted'],
      toUpdateRequest
   }
  1. Pass event as a kinesis record into the handler:
export const handler = async (event) =>
   initialize(PIPELINES, OPTIONS)
      .assemble(fromKinesis(event))
      .through(toPromise)
  1. You will see that the toUpdateRequest function is called

Expected behavior The event.type in the event body should only match with the eventType in the RULES when the match is exact.

shesupplypi avatar Jan 14 '22 00:01 shesupplypi

Suggested fix, in filterOnEventType:

Remove .join() in the isArray else statement

Change this code:

export const filterOnEventType = (rule, uow) => {
  if (typeof rule.eventType === 'string') {
    return uow.event.type === rule.eventType;
  } else if (rule.eventType instanceof RegExp) {
    return rule.eventType.test(uow.event.type);
  } else if (isArray(rule.eventType)) {
    return rule.eventType.join().indexOf(uow.event.type) > -1;
  } else if (isFunction(rule.eventType)) {
    return rule.eventType(uow.event.type, rule);
  } else {
    throw new Error(`Rule: ${rule.id}, has improperly configured eventType filter. Must be a string, array of string, regex or function.`);
  }
};

to this:

export const filterOnEventType = (rule, uow) => {
  if (typeof rule.eventType === 'string') {
    return uow.event.type === rule.eventType;
  } else if (rule.eventType instanceof RegExp) {
    return rule.eventType.test(uow.event.type);
  } else if (isArray(rule.eventType)) {
    return rule.eventType.indexOf(uow.event.type) > -1;
  } else if (isFunction(rule.eventType)) {
    return rule.eventType(uow.event.type, rule);
  } else {
    throw new Error(`Rule: ${rule.id}, has improperly configured eventType filter. Must be a string, array of string, regex or function.`);
  }
};

shesupplypi avatar Jan 14 '22 00:01 shesupplypi