From 7c4eb71a981ad90b5186cf88daf152206150df35 Mon Sep 17 00:00:00 2001 From: James Funk Date: Tue, 30 Apr 2024 18:32:55 -0600 Subject: [PATCH] fix(stepfunctions): regex in DistributedMap label is incorrectly escaping 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* --- .../lib/states/distributed-map.ts | 2 +- .../test/distributed-map.test.ts | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/distributed-map.ts b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/distributed-map.ts index 89453f316aebe..00c24e02165e8 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/lib/states/distributed-map.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/lib/states/distributed-map.ts @@ -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'); } diff --git a/packages/aws-cdk-lib/aws-stepfunctions/test/distributed-map.test.ts b/packages/aws-cdk-lib/aws-stepfunctions/test/distributed-map.test.ts index e9ee37d453d59..56795af26cbd3 100644 --- a/packages/aws-cdk-lib/aws-stepfunctions/test/distributed-map.test.ts +++ b/packages/aws-cdk-lib/aws-stepfunctions/test/distributed-map.test.ts @@ -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) { @@ -777,4 +790,4 @@ function createAppWithMap(mapFactory: (stack: cdk.Stack) => stepfunctions.Distri const map = mapFactory(stack); new stepfunctions.StateGraph(map, 'Test Graph'); return app; -} \ No newline at end of file +}