Skip to content

Commit

Permalink
Add flag to optionally enable libxml2
Browse files Browse the repository at this point in the history
Doing so will enable doc generation sanitization
With it disabled, a warning is printed to STDERR when generating docs
  • Loading branch information
nobodywasishere committed May 30, 2024
1 parent 5465223 commit 8a01443
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 34 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ target ?= ## Cross-compilation target
interpreter ?= ## Enable interpreter feature
check ?= ## Enable only check when running format
order ?=random ## Enable order for spec execution (values: "default" | "random" | seed number)
libxml2 ?= ## Enable LibXML2 support in the compiler itself
## (necessary for doc generation sanitization)

O := .build
SOURCES := $(shell find src -name '*.cr')
SPEC_SOURCES := $(shell find spec -name '*.cr')
override FLAGS += -D strict_multi_assign -D preview_overload_order $(if $(release),--release )$(if $(stats),--stats )$(if $(progress),--progress )$(if $(threads),--threads $(threads) )$(if $(debug),-d )$(if $(static),--static )$(if $(LDFLAGS),--link-flags="$(LDFLAGS)" )$(if $(target),--cross-compile --target $(target) )$(if $(interpreter),,-Dwithout_interpreter )
override FLAGS += -D strict_multi_assign -D preview_overload_order $(if $(release),--release )$(if $(stats),--stats )$(if $(progress),--progress )$(if $(threads),--threads $(threads) )$(if $(debug),-d )$(if $(static),--static )$(if $(LDFLAGS),--link-flags="$(LDFLAGS)" )$(if $(target),--cross-compile --target $(target) )$(if $(interpreter),,-Dwithout_interpreter )$(if $(libxml2),,-Dwithout_libxml2)
SPEC_WARNINGS_OFF := --exclude-warnings spec/std --exclude-warnings spec/compiler --exclude-warnings spec/primitives
SPEC_FLAGS := $(if $(verbose),-v )$(if $(junit_output),--junit_output $(junit_output) )$(if $(order),--order=$(order) )
CRYSTAL_CONFIG_LIBRARY_PATH := '$$ORIGIN/../lib/crystal'
Expand Down
4 changes: 3 additions & 1 deletion Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ target ?= ## Cross-compilation target
interpreter ?= ## Enable interpreter feature
check ?= ## Enable only check when running format
order ?=random ## Enable order for spec execution (values: "default" | "random" | seed number)
libxml2 ?= ## Enable LibXML2 support in the compiler itself
## (necessary for doc generation sanitization)

MAKEFLAGS += --no-builtin-rules
.SUFFIXES:
Expand All @@ -57,7 +59,7 @@ RMDIR = if exist $1 rd /S /Q $1
O := .build
SOURCES := $(call GLOB,src\\*.cr)
SPEC_SOURCES := $(call GLOB,spec\\*.cr)
override FLAGS += -D strict_multi_assign -D preview_overload_order $(if $(release),--release )$(if $(stats),--stats )$(if $(progress),--progress )$(if $(threads),--threads $(threads) )$(if $(debug),-d )$(if $(static),--static )$(if $(LDFLAGS),--link-flags="$(LDFLAGS)" )$(if $(target),--cross-compile --target $(target) )$(if $(interpreter),,-Dwithout_interpreter )
override FLAGS += -D strict_multi_assign -D preview_overload_order $(if $(release),--release )$(if $(stats),--stats )$(if $(progress),--progress )$(if $(threads),--threads $(threads) )$(if $(debug),-d )$(if $(static),--static )$(if $(LDFLAGS),--link-flags="$(LDFLAGS)" )$(if $(target),--cross-compile --target $(target) )$(if $(interpreter),,-Dwithout_interpreter )$(if $(libxml2),,-Dwithout_libxml2 )
SPEC_WARNINGS_OFF := --exclude-warnings spec\std --exclude-warnings spec\compiler --exclude-warnings spec\primitives
SPEC_FLAGS := $(if $(verbose),-v )$(if $(junit_output),--junit_output $(junit_output) )$(if $(order),--order=$(order) )
CRYSTAL_CONFIG_LIBRARY_PATH := $$ORIGIN\lib
Expand Down
60 changes: 31 additions & 29 deletions spec/compiler/crystal/tools/doc/doc_renderer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -374,33 +374,35 @@ describe Doc::MarkdDocRenderer do
HTML
end

describe "renders html with sanitization" do
it_renders nil, %(<h1 align="center">Foo</h1>), %(<h1>Foo</h1>)
it_renders nil, %(<script>alert("hello world")</script>), %()
it_renders nil, %(<p style="font-size: 100px">example text</p></div>), %(<p>example text</p>)

it_renders nil, %(```crystal\n# <script>alert("hello world")</script>\n```),
%(<pre><code class="language-crystal"><span class="c"># &lt;script&gt;alert(&quot;hello world&quot;)&lt;/script&gt;</span></code></pre>)
end

describe "still renders tables despite sanitization" do
table_mkdn = <<-HTML
<table>
<tr>
<th>column 1</th>
<th>column 2</th>
</tr>
<tr>
<td>data 1</td>
<td>data 2</td>
</tr>
<tr>
<td>data 3</td>
<td>data 4</td>
</tr>
</table>
HTML

it_renders nil, table_mkdn, table_mkdn
end
{% if !flag?(:without_libxml2) %}
describe "renders html with sanitization" do
it_renders nil, %(<h1 align="center">Foo</h1>), %(<h1>Foo</h1>)
it_renders nil, %(<script>alert("hello world")</script>), %()
it_renders nil, %(<p style="font-size: 100px">example text</p></div>), %(<p>example text</p>)

it_renders nil, %(```crystal\n# <script>alert("hello world")</script>\n```),
%(<pre><code class="language-crystal"><span class="c"># &lt;script&gt;alert(&quot;hello world&quot;)&lt;/script&gt;</span></code></pre>)
end

describe "still renders tables despite sanitization" do
table_mkdn = <<-HTML
<table>
<tr>
<th>column 1</th>
<th>column 2</th>
</tr>
<tr>
<td>data 1</td>
<td>data 2</td>
</tr>
<tr>
<td>data 3</td>
<td>data 4</td>
</tr>
</table>
HTML

it_renders nil, table_mkdn, table_mkdn
end
{% end %}
end
4 changes: 4 additions & 0 deletions src/compiler/crystal/command/docs.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class Crystal::Command
STDERR.puts "Couldn't determine version from git or shard.yml, please provide --project-version option"
end

if Crystal::Doc::MarkdDocRenderer::SANITIZER.nil?
STDERR.puts "Crystal built without LibXML2 support, documentation sanitization disabled"
end

unless project_info.name? && project_info.version?
abort
end
Expand Down
17 changes: 14 additions & 3 deletions src/compiler/crystal/tools/doc/markd_doc_renderer.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
require "sanitize"
{% if !flag?(:without_libxml2) %}
require "sanitize"
{% end %}

class Crystal::Doc::MarkdDocRenderer < Markd::HTMLRenderer
SANITIZER = Sanitize::Policy::HTMLSanitizer.common
{% if !flag?(:without_libxml2) %}
SANITIZER = Sanitize::Policy::HTMLSanitizer.common
{% else %}
SANITIZER = nil
{% end %}

@anchor_map = Hash(String, Int32).new(0)

def initialize(@type : Crystal::Doc::Type, options)
Expand Down Expand Up @@ -179,6 +186,10 @@ class Crystal::Doc::MarkdDocRenderer < Markd::HTMLRenderer
end

def sanitize(node : Markd::Node) : String
SANITIZER.process(node.text)
{% if !flag?(:without_libxml2) %}
SANITIZER.process(node.text)
{% else %}
node.text
{% end %}
end
end

0 comments on commit 8a01443

Please sign in to comment.