From 7e72760984df10d8b2b15970fb1b85e09bb3f713 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 17:27:13 +0200 Subject: [PATCH 1/2] [csslib] Skip comments and whitespace without recursion --- pkgs/csslib/CHANGELOG.md | 4 + pkgs/csslib/lib/src/tokenizer.dart | 101 ++++++++++++++------ pkgs/csslib/test/comment_skipping_test.dart | 59 ++++++++++++ 3 files changed, 134 insertions(+), 30 deletions(-) create mode 100644 pkgs/csslib/test/comment_skipping_test.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 2b5e3472da..2308f34c00 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,9 @@ ## 1.0.3-wip +- Avoid recursive tokenizer calls when skipping CSS comments and whitespace, + preventing stack overflow on comment-heavy stylesheets. +- End HTML comments at the first `-->` even when more dashes precede it, and + don't end comments at a NUL character. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/lib/src/tokenizer.dart b/pkgs/csslib/lib/src/tokenizer.dart index a0d3eee3ed..ff0fb36b2b 100644 --- a/pkgs/csslib/lib/src/tokenizer.dart +++ b/pkgs/csslib/lib/src/tokenizer.dart @@ -27,7 +27,62 @@ class Tokenizer extends TokenizerBase { @override Token next({bool unicodeRange = false}) { - // keep track of our starting position + // Skip whitespace, comments and CDATA markers in a loop rather than by + // recursing into next(), so long runs of them can't overflow the stack. + if (_inString) { + final skipStart = _index; + var trailingWhitespaceStart = -1; + while (_index < _text.length) { + final ch = _text.codeUnitAt(_index); + if (ch == TokenChar.NEWLINE || + ch == TokenChar.RETURN || + ch == TokenChar.SPACE || + ch == TokenChar.TAB) { + trailingWhitespaceStart = _index; + _index++; + while (_index < _text.length) { + final next = _text.codeUnitAt(_index); + if (next != TokenChar.NEWLINE && + next != TokenChar.RETURN && + next != TokenChar.SPACE && + next != TokenChar.TAB) { + break; + } + _index++; + } + continue; + } + + if (ch == TokenChar.SLASH && + _index + 1 < _text.length && + _text.codeUnitAt(_index + 1) == TokenChar.ASTERISK) { + final end = _text.indexOf('*/', _index + 2); + if (end < 0) break; // Preserve incomplete-comment errors. + _index = end + 2; + } else if (ch == TokenChar.LESS && _text.startsWith('', _index + 4); + if (end < 0) break; + _index = end + 3; + } else if (ch == TokenChar.LESS && + _text.startsWith('', _index)) { + _index += 3; + } else { + break; + } + trailingWhitespaceStart = -1; + } + if (_index == _text.length && trailingWhitespaceStart >= 0) { + // Preserve the historical EOF span after trailing whitespace. + _startIndex = trailingWhitespaceStart; + return _finishToken(TokenKind.END_OF_FILE); + } + // A unicode range can't span whitespace or comments. + if (_index != skipStart) unicodeRange = false; + } + + // Keep track of our starting position. _startIndex = _index; int ch; @@ -402,41 +457,27 @@ class Tokenizer extends TokenizerBase { } Token finishHtmlComment() { - while (true) { - var ch = _nextChar(); - if (ch == 0) { - return _finishToken(TokenKind.INCOMPLETE_COMMENT); - } else if (ch == TokenChar.MINUS) { - /* Check if close part of Comment Definition --> (CDC). */ - if (_maybeEatChar(TokenChar.MINUS)) { - if (_maybeEatChar(TokenChar.GREATER)) { - if (_inString) { - return next(); - } else { - return _finishToken(TokenKind.HTML_COMMENT); - } - } - } - } + // The comment ends at the first `-->`, even if more dashes precede it. + final end = _text.indexOf('-->', _index); + if (end < 0) { + _index = _text.length; + return _finishToken(TokenKind.INCOMPLETE_COMMENT); } + _index = end + 3; + if (_inString) return next(); + return _finishToken(TokenKind.HTML_COMMENT); } @override Token finishMultiLineComment() { - while (true) { - var ch = _nextChar(); - if (ch == 0) { - return _finishToken(TokenKind.INCOMPLETE_COMMENT); - } else if (ch == 42 /*'*'*/) { - if (_maybeEatChar(47 /*'/'*/)) { - if (_inString) { - return next(); - } else { - return _finishToken(TokenKind.COMMENT); - } - } - } + final end = _text.indexOf('*/', _index); + if (end < 0) { + _index = _text.length; + return _finishToken(TokenKind.INCOMPLETE_COMMENT); } + _index = end + 2; + if (_inString) return next(); + return _finishToken(TokenKind.COMMENT); } } diff --git a/pkgs/csslib/test/comment_skipping_test.dart b/pkgs/csslib/test/comment_skipping_test.dart new file mode 100644 index 0000000000..baf18c6e33 --- /dev/null +++ b/pkgs/csslib/test/comment_skipping_test.dart @@ -0,0 +1,59 @@ +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:csslib/parser.dart'; +import 'package:source_span/source_span.dart'; +import 'package:test/test.dart'; + +void main() { + test('thousands of skipped comments cannot overflow the tokenizer stack', () { + final comments = List.filled(20000, '/* note */ \n').join(); + final errors = []; + final tree = parse('$comments.x { color: red; }', errors: errors); + expect(errors, isEmpty); + expect(tree.topLevels, hasLength(1)); + }); + + test('HTML comment, CDATA and block comments preserve following rules', () { + final errors = []; + final tree = parse( + ' ' + '.entry { color: red; }', + errors: errors, + ); + expect(errors, isEmpty); + expect(tree.topLevels, hasLength(1)); + }); + + test('comments before EOF do not produce empty rules', () { + final errors = []; + final tree = parse('/* leading */ \n /* trailing */', errors: errors); + expect(errors, isEmpty); + expect(tree.topLevels, isEmpty); + }); + + test('HTML comment ends at the first -->', () { + final errors = []; + final tree = + parse(' .entry { color: red; }', errors: errors); + expect(errors, isEmpty); + expect(tree.topLevels, hasLength(1)); + }); + + test('NUL character does not end a comment', () { + final errors = []; + final tree = + parse('/* a \u0000 b */ .entry { color: red; }', errors: errors); + expect(errors, isEmpty); + expect(tree.topLevels, hasLength(1)); + }); + + test('unicode range does not continue after whitespace or comments', () { + for (final input in [' 10', '/**/10']) { + final tokenizer = Tokenizer(SourceFile.fromString(input), input, true); + expect(tokenizer.next(unicodeRange: true).kind, TokenKind.INTEGER, + reason: input); + } + }); +} From c2737531be826e4796e2a5e1f66993d5644a146f Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Sat, 14 Nov 2026 18:41:56 +0200 Subject: [PATCH 2/2] Address review feedback --- pkgs/csslib/lib/src/tokenizer.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkgs/csslib/lib/src/tokenizer.dart b/pkgs/csslib/lib/src/tokenizer.dart index ff0fb36b2b..95dc08b7c1 100644 --- a/pkgs/csslib/lib/src/tokenizer.dart +++ b/pkgs/csslib/lib/src/tokenizer.dart @@ -464,7 +464,6 @@ class Tokenizer extends TokenizerBase { return _finishToken(TokenKind.INCOMPLETE_COMMENT); } _index = end + 3; - if (_inString) return next(); return _finishToken(TokenKind.HTML_COMMENT); } @@ -476,7 +475,6 @@ class Tokenizer extends TokenizerBase { return _finishToken(TokenKind.INCOMPLETE_COMMENT); } _index = end + 2; - if (_inString) return next(); return _finishToken(TokenKind.COMMENT); } }