Skip to content

Commit dcb1029

Browse files
fix: validate nested functionCall shapes in condition expressions
- Ensure inner map of functionCall envelope is validated with _isValidConditionShape in and/or/not. - Validate functionCall in _isValidConditionShape recursively. - Add unit tests for malformed nested functionCall expressions.
1 parent a08cb1e commit dcb1029

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

‎packages/genui/lib/src/model/data_model.dart‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class DataContext implements cf.ExecutionContext {
156156
if (item is Map) {
157157
if (item.containsKey('functionCall')) {
158158
final Object? fc = item['functionCall'];
159-
if (fc is Map) {
159+
if (fc is Map && _isValidConditionShape(fc)) {
160160
return _evaluateStream(fc);
161161
} else {
162162
genUiLogger.warning('Invalid condition expression: $item');
@@ -177,7 +177,7 @@ class DataContext implements cf.ExecutionContext {
177177
if (val is Map) {
178178
if (val.containsKey('functionCall')) {
179179
final Object? fc = val['functionCall'];
180-
if (fc is Map) {
180+
if (fc is Map && _isValidConditionShape(fc)) {
181181
args[argName] = _evaluateStream(fc);
182182
} else {
183183
genUiLogger.warning('Invalid condition expression: $val');
@@ -225,7 +225,7 @@ class DataContext implements cf.ExecutionContext {
225225
if (condition is Map) {
226226
if (condition.containsKey('functionCall')) {
227227
final Object? fc = condition['functionCall'];
228-
if (fc is Map) {
228+
if (fc is Map && _isValidConditionShape(fc)) {
229229
return evaluateConditionStream(fc);
230230
} else {
231231
genUiLogger.warning('Invalid condition expression: $condition');
@@ -244,9 +244,12 @@ class DataContext implements cf.ExecutionContext {
244244
}
245245

246246
bool _isValidConditionShape(Map<Object?, Object?> map) {
247-
return map.containsKey('path') ||
248-
map.containsKey('call') ||
249-
(map.containsKey('functionCall') && map['functionCall'] is Map);
247+
if (map.containsKey('path') || map.containsKey('call')) return true;
248+
if (map.containsKey('functionCall')) {
249+
final Object? fc = map['functionCall'];
250+
return fc is Map && _isValidConditionShape(fc);
251+
}
252+
return false;
250253
}
251254

252255
/// Helper to determine truthiness according to A2UI expression semantics.

‎packages/genui/test/model/data_model_test.dart‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,49 @@ void main() {
541541
isFalse,
542542
);
543543

544+
// Malformed inner map inside functionCall wrapper in `and`
545+
final JsonMap andMalformedFc = {
546+
'call': 'and',
547+
'args': {
548+
'values': [
549+
{
550+
'functionCall': {
551+
'cal': 'required',
552+
'args': <String, Object?>{},
553+
},
554+
},
555+
true,
556+
],
557+
},
558+
};
559+
expect(
560+
await contextWithFuncs.evaluateConditionStream(andMalformedFc).first,
561+
isFalse,
562+
);
563+
564+
// Malformed inner map inside functionCall wrapper in `not`
565+
final JsonMap notMalformedFc = {
566+
'call': 'not',
567+
'args': {
568+
'value': {
569+
'functionCall': {'cal': 'required', 'args': <String, Object?>{}},
570+
},
571+
},
572+
};
573+
expect(
574+
await contextWithFuncs.evaluateConditionStream(notMalformedFc).first,
575+
isFalse,
576+
);
577+
578+
// Top-level functionCall with malformed inner map
579+
final JsonMap topMalformedFc = {
580+
'functionCall': {'cal': 'required', 'args': <String, Object?>{}},
581+
};
582+
expect(
583+
await contextWithFuncs.evaluateConditionStream(topMalformedFc).first,
584+
isFalse,
585+
);
586+
544587
// Unknown function inside `and`
545588
final JsonMap andUnknown = {
546589
'call': 'and',

0 commit comments

Comments
 (0)