Skip to content

Commit e930ced

Browse files
authored
[vector_graphics_compiler]: Fix Stack Overflow and CPU/Memory DoS on SVGs with circular references or exponential expansions (#11740)
### Overview This PR resolves a critical compile-time **Stack Overflow** crash and prevents **CPU/Memory Denial of Service (DoS)** vulnerabilities in the `vector_graphics_compiler` tool caused by circular references and exponential element expansions (such as Billion Laughs / XML Entity Expansion attacks) in SVG assets. During compilation, the recursive AST resolution phase was vulnerable to: 1. **Infinite Loops (Stack Overflow)**: Self-referencing or circular elements (mask-loops, pattern-loops, recursive `<use>` chains, or circular `<use>` inside `<clipPath>` nodes) caused infinite recursion and thread stack exhaustion. 2. **Exponential DAG Expansions (CPU DoS)**: Acyclic Directed Acyclic Graphs (DAGs) referencing lower levels multiple times could bypass standard cycle guards and expand to billions of resolved nodes (e.g. Billion Laughs), locking up CPU and memory. This PR implements dual-layer security defenses: local DFS cycle-guards to cleanly break circular loops, and a strict, cumulative reference expansion limit to prevent resource exhaustion.--- ### Proposed Changes #### 1. Core DFS Cycle-Guards in AST Resolution (`resolver.dart` & `parser.dart`) - Added active ancestor tracking sets (`_activeMasks`, `_activePatterns`, and `_activeDeferred`) using `Set<String>` in `ResolvingVisitor`. - Wrapped recursive resolution steps in `try/finally` blocks to guarantee that IDs are popped from the tracking sets when unwinding the call stack, preventing state leaks. - Added a local cycle-guard set (`activeDeferred`) inside `getClipPath`'s recursive `extractPathsFromNode` helper to break circular `<use>` references inside clip paths. - If a [cycle](https://svgwg.org/svg2-draft/linking.html#:~:text=an%20identified%20resource.-,invalid%20reference,-Any%20of%20the) is detected, resolution immediately aborts for that node and falls back gracefully (rendering the base graphic normally without the broken layer), satisfying W3C SVG 2 [error-handling specifications](https://svgwg.org/svg2-draft/conform.html#ErrorProcessing:~:text=The%20document%20rendering%20shall%20continue%20after%20encountering%20element%20which%20has%20an%20error.%20The%20element%20or%20its%20part%20that%20is%20in%20error%20won%27t%20be%20rendered.). #### 2. Cumulative Reference Expansion Cap (`resolver.dart` & `parser.dart`) - Integrated a cumulative event counter (`_deferredExpansionCount`) to track the total number of reference expansions (calls to `visitDeferredNode` and `extractPathsFromNode`) during compilation. - Enforces a hard security threshold of **`10,000`** cumulative expansions. - If the threshold is exceeded, the compiler immediately throws a handled `StateError` (`SVG contains too many nested reference expansions (possible Denial of Service exploit)`), preventing CPU or heap memory exhaustion. #### 3. Regression and Security Unit Tests (`test/parser_test.dart`) Added robust test cases covering circular and exponential structures: - **Circular Loop Avoidance**: `Circular Mask Loop Avoidance`, `Circular Deferred Node Loop Avoidance`, `Circular Pattern Loop Avoidance`, and `Circular ClipPath Loop Avoidance`. - **Exponential DoS Mitigations**: `Exponential DAG expansion triggers DoS protection limit` and `Exponential DAG clipPath expansion triggers DoS protection limit` (verifying compile-time abortion on a 30-level deep Billion Laughs tree). --- ### Why a 1000 Expansion Limit? 1000 is somewhat arbitrary. Benchmarking tests did not reveal any concerns with limit. --- ### Verification Results - **100% Passing Tests**: Verified that all **307 unit tests** inside the `vector_graphics_compiler` package pass successfully. - **Zero Lints or Warnings**: `flutter analyze` and `dart format` complete successfully with **"No issues found!"**. --- ### Related Issues Fixes flutter/flutter#186750 Fixes flutter/flutter#186814 ## Pre-Review Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] page, which explains my responsibilities. - [x] I read and followed the [relevant style guides] and ran [the auto-formatter]. - [x] I signed the [CLA]. - [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]` - [x] I [linked to at least one issue that this PR fixes] in the description above. - [x] I followed [the version and CHANGELOG instructions], using [semantic versioning] and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1]. - [x] I updated/added any relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under[^1]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. <!-- Links --> [Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style [the auto-formatter]: https://github.com/flutter/packages/blob/main/script/tool/README.md#format-code [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [the version and CHANGELOG instructions]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version-and-changelog-updates [semantic versioning]: https://dart.dev/tools/pub/versioning#semantic-versions [repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style [test exemption]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests # Appendix ## Vibecoded benchmark ``` ## Benchmark Results Table | Limit | SVG Type | Compilation Time (ms) | Memory (MB) | Status | | :--- | :--- | :---: | :---: | :--- | | 100 | `simple.svg` | 0.59 | 418.0 | **Success** | | 100 | `complex.svg` | 20.05 | 461.5 | **Success** | | 100 | `dos_depth_10.svg` | N/A | N/A | *Aborted (StateError)* | | 100 | `dos_depth_15.svg` | N/A | N/A | *Aborted (StateError)* | | 100 | `dos_depth_20.svg` | N/A | N/A | *Aborted (StateError)* | | 100 | `dos_depth_30.svg` | N/A | N/A | *Aborted (StateError)* | | 500 | `simple.svg` | 0.58 | 397.3 | **Success** | | 500 | `complex.svg` | 19.08 | 463.1 | **Success** | | 500 | `dos_depth_10.svg` | N/A | N/A | *Aborted (StateError)* | | 500 | `dos_depth_15.svg` | N/A | N/A | *Aborted (StateError)* | | 500 | `dos_depth_20.svg` | N/A | N/A | *Aborted (StateError)* | | 500 | `dos_depth_30.svg` | N/A | N/A | *Aborted (StateError)* | | 1000 | `simple.svg` | 0.67 | 417.0 | **Success** | | 1000 | `complex.svg` | 21.07 | 470.1 | **Success** | | 1000 | `dos_depth_10.svg` | N/A | N/A | *Aborted (StateError)* | | 1000 | `dos_depth_15.svg` | N/A | N/A | *Aborted (StateError)* | | 1000 | `dos_depth_20.svg` | N/A | N/A | *Aborted (StateError)* | | 1000 | `dos_depth_30.svg` | N/A | N/A | *Aborted (StateError)* | | 5000 | `simple.svg` | 0.62 | 398.0 | **Success** | | 5000 | `complex.svg` | 21.17 | 460.5 | **Success** | | 5000 | `dos_depth_10.svg` | 10.31 | 465.7 | **Success** | | 5000 | `dos_depth_15.svg` | N/A | N/A | *Aborted (StateError)* | | 5000 | `dos_depth_20.svg` | N/A | N/A | *Aborted (StateError)* | | 5000 | `dos_depth_30.svg` | N/A | N/A | *Aborted (StateError)* | | 10000 | `simple.svg` | 0.58 | 424.2 | **Success** | | 10000 | `complex.svg` | 22.35 | 533.5 | **Success** | | 10000 | `dos_depth_10.svg` | 9.97 | 466.8 | **Success** | | 10000 | `dos_depth_15.svg` | N/A | N/A | *Aborted (StateError)* | | 10000 | `dos_depth_20.svg` | N/A | N/A | *Aborted (StateError)* | | 10000 | `dos_depth_30.svg` | N/A | N/A | *Aborted (StateError)* | | 50000 | `simple.svg` | 0.59 | 423.6 | **Success** | | 50000 | `complex.svg` | 21.70 | 463.2 | **Success** | | 50000 | `dos_depth_10.svg` | 9.70 | 470.6 | **Success** | | 50000 | `dos_depth_15.svg` | N/A | N/A | *Aborted (StateError)* | | 50000 | `dos_depth_20.svg` | N/A | N/A | *Aborted (StateError)* | | 50000 | `dos_depth_30.svg` | N/A | N/A | *Aborted (StateError)* | ``` ```dart /// compile_worker.dart import 'dart:convert'; import 'dart:io'; import 'package:vector_graphics_compiler/vector_graphics_compiler.dart'; import 'dart:async'; Future<void> main(List<String> args) async { try { if (args.isEmpty) { stderr.writeln('Usage:'); stderr.writeln( ' dart compile_worker.dart single <input_svg_path> <output_vec_path>', ); stderr.writeln(' dart compile_worker.dart ipc'); exit(1); } final mode = args[0]; if (mode == 'single') { if (args.length < 3) { stderr.writeln( 'Error: single mode requires <input_svg_path> and <output_vec_path>', ); exit(1); } _runSingle(args[1], args[2]); } else if (mode == 'ipc') { await _runIpc(); } else { stderr.writeln('Unknown mode: $mode'); exit(1); } } catch (error, stackTrace) { stderr.writeln('Error in main: $error'); stderr.writeln(stackTrace); exit(1); } } int _readPeakRss() { try { final file = File('/proc/self/status'); if (file.existsSync()) { final lines = file.readAsLinesSync(); for (final line in lines) { if (line.startsWith('VmHWM:')) { final parts = line.split(RegExp(r'\s+')); if (parts.length >= 2) { return int.tryParse(parts[1]) ?? 0; } } } } } catch (e) { // Ignore } return 0; } void _runSingle(String inputPath, String outputPath) { try { final file = File(inputPath); if (!file.existsSync()) { throw Exception('Input file not found: $inputPath'); } final svgXml = file.readAsStringSync(); final vecBytes = encodeSvg( xml: svgXml, debugName: inputPath, enableMaskingOptimizer: false, enableClippingOptimizer: false, enableOverdrawOptimizer: false, ); final outputFile = File(outputPath); if (outputFile.parent.path.isNotEmpty) { outputFile.parent.createSync(recursive: true); } outputFile.writeAsBytesSync(vecBytes); stdout.writeln( jsonEncode(<String, Object>{ 'status': 'success', 'peak_rss_kb': _readPeakRss(), }), ); exit(0); } catch (e) { stdout.writeln( jsonEncode(<String, Object>{'status': 'error', 'error': e.toString()}), ); exit(1); } } Future<void> _runIpc() async { final Stream<String> lines = stdin .transform(utf8.decoder) .transform(const LineSplitter()); await for (final line in lines) { final trimmed = line.trim(); if (trimmed.isEmpty) { continue; } try { final Map<String, dynamic> request = jsonDecode(trimmed) as Map<String, dynamic>; final String xml = request['xml'] as String; final String name = request['name'] as String; final stopwatch = Stopwatch()..start(); encodeSvg( xml: xml, debugName: name, enableMaskingOptimizer: false, enableClippingOptimizer: false, enableOverdrawOptimizer: false, ); stopwatch.stop(); final response = <String, dynamic>{ 'status': 'success', 'latency_us': stopwatch.elapsedMicroseconds, }; stdout.writeln(jsonEncode(response)); await stdout.flush(); } catch (e) { final response = <String, dynamic>{ 'status': 'error', 'error': e.toString(), }; stdout.writeln(jsonEncode(response)); await stdout.flush(); } } } ``` ```dart /// benchmark.dart import 'dart:async'; import 'dart:convert'; import 'dart:io'; Future<void> main() async { final String scratchDir = '/usr/local/google/home/jeffkwoh/.gemini/jetski/scratch/benchmark_reference_limits'; final String constantFilePath = '/usr/local/google/home/jeffkwoh/.gemini/jetski/worktrees/packages/add-upstream-test-coverage-20260519/packages/vector_graphics_compiler/lib/src/svg/constants.dart'; final File constantFile = File(constantFilePath); if (!constantFile.existsSync()) { stderr.writeln('Error: Constants file not found at $constantFilePath'); exit(1); } // 1. Read and back up the original contents of the constant file. final String originalContents = constantFile.readAsStringSync(); print('Successfully backed up constants.dart. Original size: ${originalContents.length} bytes.'); final List<int> limits = [100, 500, 1000, 5000, 10000, 50000]; final List<String> assets = [ 'simple.svg', 'complex.svg', 'dos_depth_10.svg', 'dos_depth_15.svg', 'dos_depth_20.svg', 'dos_depth_30.svg' ]; final List<BenchmarkResult> results = []; // Ensure global try/finally block to write back original contents of constant file. try { for (final int limit in limits) { print('\n----------------------------------------'); print('Configuring limit kMaxReferenceExpansions = $limit'); print('----------------------------------------'); // a. Dynamically modify constants.dart final String updatedContents = originalContents.replaceAll( RegExp(r'const int kMaxReferenceExpansions = \d+;'), 'const int kMaxReferenceExpansions = $limit;', ); constantFile.writeAsStringSync(updatedContents); // b. Wait for 100ms to ensure disk sync await Future<void>.delayed(const Duration(milliseconds: 100)); // c. Run benchmarks for each SVG asset for (final String assetName in assets) { final String svgPath = '$scratchDir/assets/$assetName'; final String tmpOutPath = '$scratchDir/out_$assetName.vec'; print('Running single compilation for $assetName (peak RSS measurement)...'); // Run single compilation to get clean peak RSS. final ProcessResult singleResult = await Process.run( Platform.resolvedExecutable, ['compile_worker.dart', 'single', svgPath, tmpOutPath], workingDirectory: scratchDir, ); // Clean up the temporary .vec file if created. final File tmpFile = File(tmpOutPath); if (tmpFile.existsSync()) { tmpFile.deleteSync(); } final String stdoutStr = singleResult.stdout.toString().trim(); if (stdoutStr.isEmpty) { print(' Single compilation failed or returned empty stdout for $assetName.'); if (singleResult.stderr.toString().isNotEmpty) { print(' [STDERR] ${singleResult.stderr}'); } results.add(BenchmarkResult( limit: limit, svgType: assetName, compilationTimeMs: null, memoryMb: null, status: 'Aborted (StateError)', )); continue; } Map<String, dynamic> singleJson; try { singleJson = jsonDecode(stdoutStr) as Map<String, dynamic>; } catch (e) { print(' Failed to parse single mode JSON output: $stdoutStr'); results.add(BenchmarkResult( limit: limit, svgType: assetName, compilationTimeMs: null, memoryMb: null, status: 'Aborted (StateError)', )); continue; } if (singleJson['status'] == 'error') { print(' Limit hit or compile error: ${singleJson['error']}'); results.add(BenchmarkResult( limit: limit, svgType: assetName, compilationTimeMs: null, memoryMb: null, status: 'Aborted (StateError)', )); continue; } final int peakRssKb = singleJson['peak_rss_kb'] as int; final double memoryMb = peakRssKb / 1024.0; print(' Peak RSS: ${memoryMb.toStringAsFixed(2)} MB. Launching IPC mode for JIT warm-up and latency timing...'); // Start IPC worker subprocess. final Process process = await Process.start( Platform.resolvedExecutable, ['compile_worker.dart', 'ipc'], workingDirectory: scratchDir, ); final lines = process.stdout.transform(utf8.decoder).transform(const LineSplitter()); final iterator = StreamIterator(lines); process.stderr.transform(utf8.decoder).listen((errorLine) { print(' [CHILD STDERR] $errorLine'); }); double? averageLatencyMs; String statusStr = 'Success'; try { final File svgFile = File(svgPath); final String svgXml = svgFile.readAsStringSync(); final String requestJson = jsonEncode({ 'xml': svgXml, 'name': assetName, }); // Warm up compiler: send twice. for (int w = 0; w < 2; w++) { process.stdin.writeln(requestJson); await process.stdin.flush(); if (await iterator.moveNext()) { final Map<String, dynamic> warmResponse = jsonDecode(iterator.current) as Map<String, dynamic>; if (warmResponse['status'] != 'success') { throw Exception('Warm-up failed: ${warmResponse['error']}'); } } else { throw Exception('IPC stdout closed during warm-up'); } } // Run 10 actual timed iterations. final List<int> latenciesUs = []; for (int i = 0; i < 10; i++) { process.stdin.writeln(requestJson); await process.stdin.flush(); if (await iterator.moveNext()) { final Map<String, dynamic> response = jsonDecode(iterator.current) as Map<String, dynamic>; if (response['status'] == 'success') { latenciesUs.add(response['latency_us'] as int); } else { throw Exception('IPC timed run failed: ${response['error']}'); } } else { throw Exception('IPC stdout closed during timed runs'); } } final double avgUs = latenciesUs.reduce((a, b) => a + b) / latenciesUs.length; averageLatencyMs = avgUs / 1000.0; } catch (e) { print(' IPC run encountered error: $e'); statusStr = 'Aborted (StateError)'; } finally { process.kill(); await process.exitCode; } results.add(BenchmarkResult( limit: limit, svgType: assetName, compilationTimeMs: averageLatencyMs, memoryMb: memoryMb, status: statusStr, )); } } } finally { // ALWAYS restore the constants file to original state. print('\nRestoring constants.dart to original contents...'); constantFile.writeAsStringSync(originalContents); print('Restoration complete.'); } // 6. Generate jank_analysis_report.md print('\nGenerating jank_analysis_report.md...'); final File reportFile = File('$scratchDir/jank_analysis_report.md'); final StringBuffer report = StringBuffer(); report.writeln('# reference expansion limit jank analysis report'); report.writeln(); report.writeln('This report presents benchmarking results for evaluating various nested reference expansion limits under benign and malicious (Denial of Service) SVG workloads.'); report.writeln(); report.writeln('## Benchmark Results Table'); report.writeln(); report.writeln('| Limit | SVG Type | Compilation Time (ms) | Memory (MB) | Status |'); report.writeln('| :--- | :--- | :---: | :---: | :--- |'); for (final result in results) { final String limitStr = result.limit.toString(); final String svgType = '`${result.svgType}`'; final String compTimeStr = result.compilationTimeMs != null ? result.compilationTimeMs!.toStringAsFixed(2) : 'N/A'; final String memoryStr = result.memoryMb != null ? result.memoryMb!.toStringAsFixed(1) : 'N/A'; final String status = result.status == 'Success' ? '**Success**' : '*Aborted (StateError)*'; report.writeln('| $limitStr | $svgType | $compTimeStr | $memoryStr | $status |'); } report.writeln(); } class BenchmarkResult { final int limit; final String svgType; final double? compilationTimeMs; final double? memoryMb; final String status; BenchmarkResult({ required this.limit, required this.svgType, required this.compilationTimeMs, required this.memoryMb, required this.status, }); } ```
1 parent 81b5b2f commit e930ced

6 files changed

Lines changed: 450 additions & 41 deletions

File tree

packages/vector_graphics_compiler/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## NEXT
1+
## 1.2.4
22

3+
* Fix Stack Overflow crashes caused by circular references (masks, patterns, deferred nodes, and clip paths).
4+
* Prevent CPU/Memory Denial of Service (DoS) resource exhaustion from exponential DAG reference expansions (Billion Laughs SVG exploits) by enforcing a strict, cumulative reference expansion safety limit of 1,000.
35
* Updates minimum supported SDK version to Flutter 3.38/Dart 3.10.
46

57
## 1.2.3
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// The maximum number of nested reference expansions allowed in an SVG to prevent DoS exploits.
6+
const int kMaxReferenceExpansions = 1000;
7+
8+
/// The error message thrown when the nested reference expansions limit is exceeded.
9+
const String kMaxReferenceExpansionsErrorMessage =
10+
'SVG contains too many nested reference expansions (possible Denial of Service exploit).';

packages/vector_graphics_compiler/lib/src/svg/parser.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import '../vector_instructions.dart';
2121
import 'clipping_optimizer.dart';
2222
import 'color_mapper.dart';
2323
import 'colors.dart';
24+
import 'constants.dart';
2425
import 'masking_optimizer.dart';
2526
import 'node.dart';
2627
import 'numbers.dart' as numbers show parseDoubleWithUnits;
@@ -1678,6 +1679,7 @@ class _Resolver {
16781679
final Map<String, AttributedNode> _drawables = <String, AttributedNode>{};
16791680
final Map<String, Gradient> _shaders = <String, Gradient>{};
16801681
final Map<String, List<Node>> _clips = <String, List<Node>>{};
1682+
int _deferredExpansionCount = 0;
16811683

16821684
bool _sealed = false;
16831685

@@ -1702,6 +1704,7 @@ class _Resolver {
17021704

17031705
final pathBuilders = <PathBuilder>[];
17041706
PathBuilder? currentPath;
1707+
final activeDeferred = <String>{};
17051708
void extractPathsFromNode(Node? target) {
17061709
if (target is PathNode) {
17071710
final nextPath = PathBuilder.fromPath(target.path);
@@ -1716,7 +1719,19 @@ class _Resolver {
17161719
currentPath!.addPath(nextPath.toPath(reset: false));
17171720
}
17181721
} else if (target is DeferredNode) {
1719-
extractPathsFromNode(target.resolver(target.refId));
1722+
_deferredExpansionCount++;
1723+
if (_deferredExpansionCount > kMaxReferenceExpansions) {
1724+
throw StateError(kMaxReferenceExpansionsErrorMessage);
1725+
}
1726+
if (!activeDeferred.add(target.refId)) {
1727+
// Recursive loop detected.
1728+
return;
1729+
}
1730+
try {
1731+
extractPathsFromNode(target.resolver(target.refId));
1732+
} finally {
1733+
activeDeferred.remove(target.refId);
1734+
}
17201735
} else if (target is ParentNode) {
17211736
target.visitChildren(extractPathsFromNode);
17221737
}

packages/vector_graphics_compiler/lib/src/svg/resolver.dart

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import '../geometry/path.dart';
99
import '../geometry/vertices.dart';
1010
import '../image/image_info.dart';
1111
import '../paint.dart';
12+
import 'constants.dart';
1213
import 'node.dart';
1314
import 'parser.dart';
1415
import 'visitor.dart';
@@ -19,6 +20,11 @@ import 'visitor.dart';
1920
class ResolvingVisitor extends Visitor<Node, AffineMatrix> {
2021
late Rect _bounds;
2122

23+
final Set<String> _activeMasks = <String>{};
24+
final Set<String> _activeDeferred = <String>{};
25+
final Set<String> _activePatterns = <String>{};
26+
int _deferredExpansionCount = 0;
27+
2228
@override
2329
Node visitClipNode(ClipNode clipNode, AffineMatrix data) {
2430
final AffineMatrix childTransform = clipNode.concatTransform(data);
@@ -37,19 +43,31 @@ class ResolvingVisitor extends Visitor<Node, AffineMatrix> {
3743

3844
@override
3945
Node visitMaskNode(MaskNode maskNode, AffineMatrix data) {
40-
final AttributedNode? resolvedMask = maskNode.resolver(maskNode.maskId);
41-
if (resolvedMask == null) {
46+
_deferredExpansionCount++;
47+
if (_deferredExpansionCount > kMaxReferenceExpansions) {
48+
throw StateError(kMaxReferenceExpansionsErrorMessage);
49+
}
50+
if (!_activeMasks.add(maskNode.maskId)) {
51+
// Recursive loop detected.
4252
return maskNode.child.accept(this, data);
4353
}
44-
final Node child = maskNode.child.accept(this, data);
45-
final AffineMatrix childTransform = maskNode.concatTransform(data);
46-
final Node mask = resolvedMask.accept(this, childTransform);
47-
48-
return ResolvedMaskNode(
49-
child: child,
50-
mask: mask,
51-
blendMode: maskNode.blendMode,
52-
);
54+
try {
55+
final AttributedNode? resolvedMask = maskNode.resolver(maskNode.maskId);
56+
if (resolvedMask == null) {
57+
return maskNode.child.accept(this, data);
58+
}
59+
final Node child = maskNode.child.accept(this, data);
60+
final AffineMatrix childTransform = maskNode.concatTransform(data);
61+
final Node mask = resolvedMask.accept(this, childTransform);
62+
63+
return ResolvedMaskNode(
64+
child: child,
65+
mask: mask,
66+
blendMode: maskNode.blendMode,
67+
);
68+
} finally {
69+
_activeMasks.remove(maskNode.maskId);
70+
}
5371
}
5472

5573
@override
@@ -180,17 +198,29 @@ class ResolvingVisitor extends Visitor<Node, AffineMatrix> {
180198

181199
@override
182200
Node visitDeferredNode(DeferredNode deferredNode, AffineMatrix data) {
183-
final AttributedNode? resolvedNode = deferredNode.resolver(
184-
deferredNode.refId,
185-
);
186-
if (resolvedNode == null) {
201+
_deferredExpansionCount++;
202+
if (_deferredExpansionCount > kMaxReferenceExpansions) {
203+
throw StateError(kMaxReferenceExpansionsErrorMessage);
204+
}
205+
if (!_activeDeferred.add(deferredNode.refId)) {
206+
// Recursive loop detected.
187207
return Node.empty;
188208
}
189-
final Node concreteRef = resolvedNode.applyAttributes(
190-
deferredNode.attributes,
191-
replace: true,
192-
);
193-
return concreteRef.accept(this, data);
209+
try {
210+
final AttributedNode? resolvedNode = deferredNode.resolver(
211+
deferredNode.refId,
212+
);
213+
if (resolvedNode == null) {
214+
return Node.empty;
215+
}
216+
final Node concreteRef = resolvedNode.applyAttributes(
217+
deferredNode.attributes,
218+
replace: true,
219+
);
220+
return concreteRef.accept(this, data);
221+
} finally {
222+
_activeDeferred.remove(deferredNode.refId);
223+
}
194224
}
195225

196226
@override
@@ -293,26 +323,38 @@ class ResolvingVisitor extends Visitor<Node, AffineMatrix> {
293323

294324
@override
295325
Node visitPatternNode(PatternNode patternNode, AffineMatrix data) {
296-
final AttributedNode? resolvedPattern = patternNode.resolver(
297-
patternNode.patternId,
298-
);
299-
if (resolvedPattern == null) {
326+
_deferredExpansionCount++;
327+
if (_deferredExpansionCount > kMaxReferenceExpansions) {
328+
throw StateError(kMaxReferenceExpansionsErrorMessage);
329+
}
330+
if (!_activePatterns.add(patternNode.patternId)) {
331+
// Recursive loop detected.
300332
return patternNode.child.accept(this, data);
301333
}
302-
final Node child = patternNode.child.accept(this, data);
303-
final AffineMatrix childTransform = patternNode.concatTransform(data);
304-
final Node pattern = resolvedPattern.accept(this, childTransform);
305-
306-
return ResolvedPatternNode(
307-
child: child,
308-
pattern: pattern,
309-
x: resolvedPattern.attributes.x?.calculate(0) ?? 0,
310-
y: resolvedPattern.attributes.y?.calculate(0) ?? 0,
311-
width: resolvedPattern.attributes.width!,
312-
height: resolvedPattern.attributes.height!,
313-
transform: data,
314-
id: patternNode.patternId,
315-
);
334+
try {
335+
final AttributedNode? resolvedPattern = patternNode.resolver(
336+
patternNode.patternId,
337+
);
338+
if (resolvedPattern == null) {
339+
return patternNode.child.accept(this, data);
340+
}
341+
final Node child = patternNode.child.accept(this, data);
342+
final AffineMatrix childTransform = patternNode.concatTransform(data);
343+
final Node pattern = resolvedPattern.accept(this, childTransform);
344+
345+
return ResolvedPatternNode(
346+
child: child,
347+
pattern: pattern,
348+
x: resolvedPattern.attributes.x?.calculate(0) ?? 0,
349+
y: resolvedPattern.attributes.y?.calculate(0) ?? 0,
350+
width: resolvedPattern.attributes.width!,
351+
height: resolvedPattern.attributes.height!,
352+
transform: data,
353+
id: patternNode.patternId,
354+
);
355+
} finally {
356+
_activePatterns.remove(patternNode.patternId);
357+
}
316358
}
317359

318360
@override

packages/vector_graphics_compiler/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: vector_graphics_compiler
22
description: A compiler to convert SVGs to the binary format used by `package:vector_graphics`.
33
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics_compiler
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22
5-
version: 1.2.3
5+
version: 1.2.4
66

77
executables:
88
vector_graphics_compiler:

0 commit comments

Comments
 (0)