diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 51f8ee188..518c70978 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. - `Font.scale` keeps the line height (scaling pixel heights and keeping relative ratios) and no longer throws when the font size is unspecified. - Fix `Font.cssExpression` output for partial fonts and line heights. diff --git a/pkgs/csslib/lib/src/tokenizer.dart b/pkgs/csslib/lib/src/tokenizer.dart index a0d3eee3e..95dc08b7c 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,25 @@ 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; + 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; + 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 000000000..baf18c6e3 --- /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); + } + }); +}