Skip to content

Commit

Permalink
fix(stepfunctions): regex in DistributedMap label is incorrectly esca…
Browse files Browse the repository at this point in the history
…ping characters (#29765)

Regex is incorrect. Switching to basic regex. Fixing regex to properly escape special characters

### Reason for this change

Regex was incorrectly escaping the `]` character, and as such was falsely returning `true`. It also seemed to be interpreting the `\` characters incorrectly, so I switched to a regex literal in line to fix that.



### Description of changes

Switch from `RegExp` object to inline regex.
Fix regex to correctly escape `]` character.



### Description of how you validated changes

Running in Node CLI, such as:

```node
> /[\s\?\*\<\>\{\}\\[\]\:\;\,\|\^\~\$\#\%\&\`\"]|[\u0000-\u001f]|[\u007f-\u009f]/gi.test(' ') 
true
```

Running in Chrome Dev Tools console, such as:

```js
new RegExp("/[\s\?\*\<\>\{\}\\[\]\:\;\,\|\^\~\$\#\%\&\`\"]|[\u0000-\u001f]|[\u007f-\u009f]/gi").test(' ')
false
```


no

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
piurafunk authored May 1, 2024
1 parent fc890df commit 7c4eb71
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class DistributedMap extends MapBase implements INextable {
errors.push('label must be 40 characters or less');
}

let labelRegex = new RegExp('[\s\?\*\<\>\{\}\\[\\]\:\;\,\\\|\^\~\$\#\%\&\`\"]|[\u0000-\u001f]|[\u007f-\u009f]', 'gi');
let labelRegex = /[\s\?\*\<\>\{\}\\[\]\:\;\,\|\^\~\$\#\%\&\`\"]|[\u0000-\u001f]|[\u007f-\u009f]/gi;
if (labelRegex.test(this.label)) {
errors.push('label cannot contain any whitespace or special characters');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,19 @@ describe('Distributed Map State', () => {

expect(() => app.synth()).toThrow(/label cannot contain any whitespace or special characters/);
});

test('does not fail in synthesis if label has `s`', () => {
const app = createAppWithMap((stack) => {
const map = new stepfunctions.DistributedMap(stack, 'Map State', {
label: 's',
itemsPath: stepfunctions.JsonPath.stringAt('$.inputForMap'),
});

return map;
});

app.synth();
});
});

function render(sm: stepfunctions.IChainable) {
Expand All @@ -777,4 +790,4 @@ function createAppWithMap(mapFactory: (stack: cdk.Stack) => stepfunctions.Distri
const map = mapFactory(stack);
new stepfunctions.StateGraph(map, 'Test Graph');
return app;
}
}

0 comments on commit 7c4eb71

Please sign in to comment.