Skip to content

Commit

Permalink
Don’t crash with empty CSS values
Browse files Browse the repository at this point in the history
Fix #2012.
  • Loading branch information
liZe committed Dec 4, 2023
1 parent ecd5aea commit 0f7a45e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/test_css_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from weasyprint.css import preprocess_declarations
from weasyprint.css.computed_values import ZERO_PIXELS
from weasyprint.css.properties import INITIAL_VALUES
from weasyprint.css.validation.expanders import EXPANDERS
from weasyprint.css.validation.properties import PROPERTIES
from weasyprint.images import LinearGradient, RadialGradient

from .testing_utils import assert_no_logs, capture_logs
Expand Down Expand Up @@ -1248,3 +1250,10 @@ def test_image_orientation(rule, result):
))
def test_image_orientation_invalid(rule, reason):
assert_invalid(rule, reason)


@assert_no_logs
#@pytest.mark.parametrize('expander', list(EXPANDERS) + list(PROPERTIES))
@pytest.mark.parametrize('expander', list(EXPANDERS))
def test_empty_value(expander):
assert_invalid(f'{expander}:', message='Ignored')
4 changes: 4 additions & 0 deletions weasyprint/css/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ def validation_error(level, reason):
expander_ = EXPANDERS.get(name, validate_non_shorthand)
tokens = remove_whitespace(declaration.value)
try:
# Having no tokens is allowed by grammar but refused by all
# properties and expanders.
if not tokens:
raise InvalidValues('no value')
# Use list() to consume generators now and catch any error.
result = list(expander_(name, tokens, base_url))
except InvalidValues as exc:
Expand Down

0 comments on commit 0f7a45e

Please sign in to comment.