Implement resource limits and remove leftover string references

This commit is contained in:
Julia Boutin
2025-10-27 10:12:43 -06:00
parent 9409dd8f4a
commit fb6cf17099
5 changed files with 18 additions and 12 deletions
-1
View File
@@ -40,7 +40,6 @@ module Liquid
QuotedString = /"[^"]*"|'[^']*'/ QuotedString = /"[^"]*"|'[^']*'/
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/o QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/o
TagAttributes = /(\w[\w-]*)\s*\:\s*(#{QuotedFragment})/o TagAttributes = /(\w[\w-]*)\s*\:\s*(#{QuotedFragment})/o
ContextInheritance = /\.\.\./
AnyStartingTag = /#{TagStart}|#{VariableStart}/o AnyStartingTag = /#{TagStart}|#{VariableStart}/o
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
+1 -1
View File
@@ -5,7 +5,7 @@
block_tag_unexpected_args: "Syntax Error in '%{tag}' - Valid syntax: {% %{tag} %}{% end%{tag} %}" block_tag_unexpected_args: "Syntax Error in '%{tag}' - Valid syntax: {% %{tag} %}{% end%{tag} %}"
assign: "Syntax Error in 'assign' - Valid syntax: assign [var] = [source]" assign: "Syntax Error in 'assign' - Valid syntax: assign [var] = [source]"
capture: "Syntax Error in 'capture' - Valid syntax: capture [var]" capture: "Syntax Error in 'capture' - Valid syntax: capture [var]"
snippet: "Syntax Error in 'snippet' - Valid syntax: snippet [quoted string]" snippet: "Syntax Error in 'snippet' - Valid syntax: snippet [var]"
case: "Syntax Error in 'case' - Valid syntax: case [condition]" case: "Syntax Error in 'case' - Valid syntax: case [condition]"
case_invalid_when: "Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %}" case_invalid_when: "Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %}"
case_invalid_else: "Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) " case_invalid_else: "Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) "
+4 -7
View File
@@ -128,12 +128,9 @@ module Liquid
end end
def rigid_template_name(p) def rigid_template_name(p)
if p.look(:string) return p.consume(:string) if p.look(:string)
p.consume(:string)
# inline snippets use variable identifiers p.consume(:id) if p.look(:id)
elsif p.look(:id)
p.consume(:id)
end
end end
def strict_parse(markup) def strict_parse(markup)
@@ -153,7 +150,7 @@ module Liquid
@is_for_loop = (with_or_for == FOR) @is_for_loop = (with_or_for == FOR)
@attributes = {} @attributes = {}
markup.scan(/(#{ContextInheritance})|#{TagAttributes.source}/) do |context_marker, key, value| markup.scan(/(\.\.\.)(?=\s|,|$)|#{TagAttributes.source}/) do |context_marker, key, value|
if context_marker if context_marker
@attributes.delete("...") @attributes.delete("...")
@attributes["..."] = true @attributes["..."] = true
+5 -3
View File
@@ -2,7 +2,7 @@
module Liquid module Liquid
# @liquid_type tag # @liquid_type tag
# @liquid_category theme # @liquid_category variable
# @liquid_name snippet # @liquid_name snippet
# @liquid_summary # @liquid_summary
# Creates a new inline snippet. # Creates a new inline snippet.
@@ -14,8 +14,6 @@ module Liquid
# {% endsnippet %} # {% endsnippet %}
class Snippet < Block class Snippet < Block
SYNTAX = /(#{VariableSignature}+)/o
def initialize(tag_name, markup, options) def initialize(tag_name, markup, options)
super super
p = @parse_context.new_parser(markup) p = @parse_context.new_parser(markup)
@@ -29,6 +27,10 @@ module Liquid
def render_to_output_buffer(context, output) def render_to_output_buffer(context, output)
snippet_drop = SnippetDrop.new(@body) snippet_drop = SnippetDrop.new(@body)
context.scopes.last[@to] = snippet_drop context.scopes.last[@to] = snippet_drop
snippet_size = @body.nodelist.sum { |node| node.to_s.bytesize }
context.resource_limits.increment_assign_score(snippet_size)
output output
end end
+8
View File
@@ -1238,4 +1238,12 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template, error_mode: :rigid) assert_template_result(expected, template, error_mode: :rigid)
end end
end end
class ResourceLimits < SnippetTest
def test_increment_assign_score_by_bytes_not_characters
t = Template.parse("{% snippet foo %}すごい{% endsnippet %}")
t.render!
assert_equal(9, t.resource_limits.assign_score)
end
end
end end