Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Deep tag nesting or recursive nesting detected for hundreds of tags #936

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion comfortable_mexican_sofa.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |s|
s.add_dependency "kramdown", ">= 1.0.0"
s.add_dependency "mimemagic", ">= 0.3.2"
s.add_dependency "mini_magick", ">= 4.8.0"
s.add_dependency "rails", ">= 5.2.0"
s.add_dependency "rails", ">= 5.2.0", "< 6.1"
s.add_dependency "rails-i18n", ">= 5.0.0"
s.add_dependency "sassc-rails", ">= 2.0.0"
end
8 changes: 4 additions & 4 deletions lib/comfortable_mexican_sofa/content/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def register_tag(name, klass)
# @param [Comfy::Cms::WithFragments, nil] context
def initialize(context)
@context = context
@depth = 0
end

# This is how we render content out. Takes context (cms page) and content
# nodes
# @param [Array<String, ComfortableMexicanSofa::Content::Tag>]
# @param [Boolean] allow_erb
def render(nodes, allow_erb = ComfortableMexicanSofa.config.allow_erb)
if (@depth += 1) > MAX_DEPTH
# @param [Integer] depth
def render(nodes, allow_erb = ComfortableMexicanSofa.config.allow_erb, depth = 0)
if depth > MAX_DEPTH
raise Error, "Deep tag nesting or recursive nesting detected"
end

Expand All @@ -61,7 +61,7 @@ def render(nodes, allow_erb = ComfortableMexicanSofa.config.allow_erb)
else
tokens = tokenize(node.render)
nodes = nodes(tokens)
render(nodes, allow_erb || node.allow_erb?)
render(nodes, allow_erb || node.allow_erb?, depth.next)
end
end.flatten.join
end
Expand Down
3 changes: 2 additions & 1 deletion lib/comfortable_mexican_sofa/seeds/file/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ def export!
file_path = File.join(path, file.attachment.filename.to_s)

# writing attributes
::File.open(::File.join(path, "_#{file.attachment.filename}.yml"), "w") do |f|
::File.open(::File.join(path, "#{'%07d' % file.id}.yml"), "w") do |f|
f.write({
"label" => file.label,
"name" => file.attachment.filename.to_s,
"description" => file.description,
"categories" => file.categories.map(&:label)
}.to_yaml)
Expand Down
17 changes: 11 additions & 6 deletions lib/comfortable_mexican_sofa/seeds/file/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ def initialize(from, to = from)
end

def import!
Dir["#{path}[^_]*"].each do |file_path|
filename = ::File.basename(file_path)
Dir["#{path}*.yml"].sort.each do |file_path|
yml_name = ::File.basename(file_path)

attrs = YAML.safe_load(File.read(file_path))
filename = attrs["name"] unless attrs.nil?
org_path = Pathname.new(path) + filename

file = site.files.with_attached_attachment
.where("active_storage_blobs.filename" => filename).references(:blob).first ||
Expand All @@ -19,22 +23,23 @@ def import!
# We need to track actual file and its attributes
fresh_file = false

if File.exist?(attrs_path = File.join(path, "_#{filename}.yml"))
if File.exist?(attrs_path = File.join(path, yml_name))
if fresh_seed?(file, attrs_path)
fresh_file = true

attrs = YAML.safe_load(File.read(attrs_path))
attrs.delete("name")
category_ids = category_names_to_ids(file, attrs.delete("categories"))
file.attributes = attrs.merge(
category_ids: category_ids
)
end
end

if fresh_seed?(file, file_path)
if fresh_seed?(file, org_path)
fresh_file = true

file_handler = File.open(file_path)
file_handler = File.open(org_path)
file.file = {
io: file_handler,
filename: filename,
Expand All @@ -44,7 +49,7 @@ def import!

if fresh_file
if file.save
message = "[CMS SEEDS] Imported File \t #{file_path}"
message = "[CMS SEEDS] Imported File \t #{org_path}"
ComfortableMexicanSofa.logger.info(message)
else
message = "[CMS SEEDS] Failed to import File \n#{file.errors.inspect}"
Expand Down
9 changes: 9 additions & 0 deletions test/lib/content/renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,13 @@ def test_render_stack_overflow
end
end

def test_render_with_more_than_hundred_tags
test_string =
Array.new(ComfortableMexicanSofa::Content::Renderer::MAX_DEPTH) { "{{cms:text content}}" }.join(" ")
out = render_string(test_string)
expected =
Array.new(ComfortableMexicanSofa::Content::Renderer::MAX_DEPTH) { "content" }.join(" ")
assert_equal expected, out
end

end