70. Running Asciidoctor Securely
Asciidoctor provides security levels that control the read and write access of attributes, the include directive, macros, and scripts while a document is processing.
Each level includes the restrictions enabled in the prior security level.
UNSAFE
|
A safe mode level that disables any security features enforced by Asciidoctor. Ruby is still subject to its own restrictions. This is the default safe mode for the CLI.
Its integer value is |
SAFE
|
This safe mode level prevents access to files which reside outside of the parent directory of the source file.
The Its integer value is |
SERVER
|
A safe mode level that disallows the document from setting attributes that would affect conversion of the document.
This level trims
It allows Its integer value is |
SECURE
|
A safe mode level that disallows the document from attempting to read files from the file system and including their contents into the document. Additionally, it:
Asciidoctor extensions may still embed content into the document depending whether they honor the safe mode setting. This is the default safe mode for the API.
Its integer value is |
You can set Asciidoctor’s safe mode level using the CLI or API.
70.1. Set the Safe Mode in the CLI
When Asciidoctor is invoked via the CLI, the safe mode is set to UNSAFE by default.
You can change the safe level by executing one of the following commands in the CLI.
-S,--safe-mode=SAFE_MODE-
Sets the safe mode level of the document according to the assigned level (
UNSAFE,SAFE,SERVER,SECURE). --safe,asciidoctor-safe-
Sets the safe mode level to
SAFE. Provided for compatibility with the python AsciiDocsafecommand.
70.2. Set the Safe Mode in the API
The default safe level in the API is SECURE.
In the API, you can set the safe mode using a string, symbol or integer value.
The value must be set in the document constructor using the :safe option.
result = Asciidoctor.convert_file('master.adoc', :safe => 'server')
or
result = Asciidoctor.convert_file('master.adoc', :safe => :server)
or
result = Asciidoctor.convert_file('master.adoc', :safe => 10)
70.3. Set Attributes Based on the Safe Mode
Asciidoctor provides access to the current safe mode through built-in attributes. You can use these attributes to enable or disable content based on the current safe mode of the processor.
The safe mode can be referenced by one of the following attributes:
-
The value of the
safe-mode-nameattribute (e.g., unsafe, safe, etc.) -
The value of the
safe-mode-levelattribute (e.g., 0, 10, etc.) -
The presense of the
safe-mode-<name>attribute, where<name>is the safe mode name.
The attributes in the next example define replacement text for features that are disabled in high security environments:
ifdef::safe-mode-secure[]
Link to chapters instead of including them.
endif::safe-mode-secure[]
This feature is particularly handy for displaying content on GitHub, where the safe mode is set to its most restrictive setting, secure.