fix(appsync): scan template holes and keyword object keys (#7320)

Exempt a banned keyword from the APPSYNC_JS subset scan only in an object-key position, after a { or , and before its :, so { class: "allowed" } runs while cond ? this : null is still refused. Adds regression cases for template text after a hole, whose scanning landed in #7343.

Closes #7240
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-11-03 13:43:54 -04:00
1 parent f22d0409ac
commit fca988cea1
2 files changed
+65 -2

No files matched your search

@@ -745,8 +745,12 @@ function validateAppSyncSubset(code) {
const literals = blankLiterals(code);
const lineNumber = (index) => code.slice(0, index).split(/\r\n|[\r\n\u2028\u2029]/).length;
for (const [pattern, description] of BANNED_CONSTRUCTS) {
const match = pattern.exec(literals.scanned);
if (match) {
for (const match of literals.scanned.matchAll(new RegExp(pattern.source, 'g'))) {
const before = literals.scanned.slice(0, match.index).trimEnd();
const after = literals.scanned.slice(match.index + match[0].length);
if (/^[\w$]+$/.test(match[0]) && /[,{]$/.test(before) && /^\s*:/.test(after)) {
continue;
}
return unsupportedConstruct(description, lineNumber(match.index));
}
}
@@ -58,6 +58,9 @@ class AppSyncJsResolverDockerIntegrationTest {
misplacedYield: String
malformedSyntax: String
keywordsInText: String
templateHole: String
keywordObjectKey: String
conditionalThis: String
regexLiteral: String
regexKeyword: String
regexClassThenUnsupported: String
@@ -220,6 +223,34 @@ class AppSyncJsResolverDockerIntegrationTest {
}
""";
private static final String TEMPLATE_HOLE_RESOLVER = """
export function request(ctx) {
const prefix = `${"value"}-`;
return { payload: { value: `its class: ${prefix}` } };
}
export function response(ctx) {
return ctx.result.value;
}
""";
private static final String KEYWORD_OBJECT_KEY_RESOLVER = """
export function request(ctx) {
return { payload: { class: "allowed" } };
}
export function response(ctx) {
return ctx.result.class;
}
""";
private static final String CONDITIONAL_THIS_RESOLVER = """
export function request(ctx) {
return { payload: ctx.args.flag ? this : null };
}
export function response(ctx) {
return ctx.result;
}
""";
private static final String REGEX_KEYWORD_RESOLVER = """
export function request(ctx) {
return { payload: { value: /while/.test("while") } };
@@ -381,6 +412,9 @@ class AppSyncJsResolverDockerIntegrationTest {
createUnitResolver(apiId, "Query", "misplacedYield", "local", YIELD_RESOLVER);
createUnitResolver(apiId, "Query", "malformedSyntax", "local", MALFORMED_RESOLVER);
createUnitResolver(apiId, "Query", "keywordsInText", "local", KEYWORDS_IN_TEXT_RESOLVER);
createUnitResolver(apiId, "Query", "templateHole", "local", TEMPLATE_HOLE_RESOLVER);
createUnitResolver(apiId, "Query", "keywordObjectKey", "local", KEYWORD_OBJECT_KEY_RESOLVER);
createUnitResolver(apiId, "Query", "conditionalThis", "local", CONDITIONAL_THIS_RESOLVER);
createUnitResolver(apiId, "Query", "regexLiteral", "local", REGEX_LITERAL_RESOLVER);
createUnitResolver(apiId, "Query", "regexKeyword", "local", REGEX_KEYWORD_RESOLVER);
createUnitResolver(apiId, "Query", "regexClassThenUnsupported", "local",
@@ -597,6 +631,31 @@ class AppSyncJsResolverDockerIntegrationTest {
.body("data.keywordsInText", equalTo("try catch class while ok"));
}
@Test
void aTemplateLiteralResumesAfterItsExpression() {
query("{ templateHole }")
.statusCode(200)
.body("errors", nullValue())
.body("data.templateHole", equalTo("its class: value-"));
}
@Test
void aKeywordUsedAsAnObjectKeyIsNotReadAsADeclaration() {
query("{ keywordObjectKey }")
.statusCode(200)
.body("errors", nullValue())
.body("data.keywordObjectKey", equalTo("allowed"));
}
@Test
void aConditionalThisBeforeAColonIsStillRefused() {
query("{ conditionalThis }")
.statusCode(200)
.body("data.conditionalThis", nullValue())
.body("errors[0].errorType", equalTo("UnsupportedFeature"))
.body("errors[0].message", containsString("this"));
}
@Test
void aRegexLiteralIsReportedAsUnsupported() {
query("{ regexLiteral }")