Jump to content

Extension:Secure HTML: Difference between revisions

From mediawiki.org
Content deleted Content added
upd
m Undid edits by ~2025-35609-0 (talk) to last version by NDG: unexplained content removal
 
(36 intermediate revisions by 15 users not shown)
Line 1: Line 1:
{{Page security extension disclaimer}}
{{Extension
{{Extension
|name = Secure HTML
|status = unmaintained
|status = stable
|type1 = tag
|type1 = tag
|type2 = user rights
|type2 = user rights
|hook1 = ParserFirstCallInit
|username = Fo0bar
|username = Fo0bar
|author = [[User:Fo0bar|Ryan Finnie]]
|author = [[User:Fo0bar|Ryan Finnie]]
|image =
|image = SpecialSecureHTML screenshot.png
|version = 2.4.1
|version = 3.0
|update = 2014-04-14
|update = 2016-07-29
|mediawiki = 1.18+
|mediawiki = 1.23+
|license = GPL
|license = GPL-2.0+
|download = {{DownloadGitSnapshot|SecureHTML|Download '''latest stable release''' (2.4.1)|commit=refs/tags/v2.4.1}}, or: {{WikimediaDownload|SecureHTML}}
|download = {{DownloadGitSnapshot|SecureHTML|Download '''latest stable release''' (3.0)|commit=v3.0}}, or: {{WikimediaDownload|SecureHTML}}
|readme =
|readme =
|changelog =
|changelog =
|description = Lets you include arbitrary HTML in an authorized and secure way
|description = Lets you include arbitrary HTML in an authorized and secure way
|parameters= $wgSecureHTMLSecrets, $shtml_keys (deprecated)
|parameters= $wgSecureHTMLSecrets, $wgSecureHTMLSpecialRight, $wgSecureHTMLSpecialDropdown, $wgSecureHTMLTag
|tags = <shtml>
|tags = {{tag|shtml|open}} (configurable)
|rights =
|rights =
|example = [[finnix:Support_Finnix|<shtml> tag (PayPal forms)]], [[finnix:Special:SecureHTML|Special:SecureHTML]]
|example = [[finnix:Support_Finnix|{{tag|shtml|open}} tag (PayPal forms)]], [[finnix:Special:SecureHTML|Special:SecureHTML]]
}}
}}
The '''Secure HTML''' extension lets you include arbitrary HTML in an authorized and secure way


== Rationale ==
Occasionally you need to display [[w:HTML|HTML]] within a wiki, but allowing it site-wide opens you up to various [[w:cross-site scripting|XSS]] attacks. This extension solves that problem by letting you specify arbitrary HTML, but only if the HTML includes a corresponding hash that is created by combining the HTML input, along with a secret that only authorized people know.
Occasionally, you need to display [[w:HTML|HTML]] within a wiki, but allowing it site-wide opens you up to various [[w:cross-site scripting|XSS]] attacks. This extension solves that problem by letting you specify arbitrary HTML, but only if the HTML includes a corresponding hash created by signing the HTML input with a secret only authorized people know.


The extension uses a special page, ''Special:SecureHTML'' which helps you build a tag, <tt>&lt;shtml&gt;</tt>, which acts as a wrapper around raw HTML. An example looks like (linefeeds added for readability):
The extension uses a special page, ''Special:SecureHTML'' which helps you build a tag, {{tag|shtml|open}}, which acts as a wrapper around raw HTML.

<pre>
==Example==
<shtml version="2" keyname="fo0bar"
<syntaxhighlight lang="html">
<shtml
hash="7fa503206cb1de131dd6acdca576e92262dd6d176cc3466073a343863743b8ed"
hash="7fa503206cb1de131dd6acdca576e92262dd6d176cc3466073a343863743b8ed"
><strong>Hello world!</strong></shtml>
><strong>Hello world!</strong></shtml>
</syntaxhighlight>
</pre>


If the user uses a valid shared secret to build the hashed <tt>&lt;shtml&gt;</tt> snippet and includes it in a wiki page, the snippet is rendered as the raw HTML contained within the tag. If the shared secret is invalid, the snippet is rendered as an error message (but not containing the HTML, obviously).
If the user uses a valid shared secret to build the hashed {{tag|shtml|open}} snippet and includes it in a wiki page, the snippet is rendered as the raw HTML contained within the tag. If the shared secret is invalid, the snippet is rendered as an error message (but does not contain the HTML).


== Installation ==
== Installation ==
Secure HTML has been tested with MediaWiki 1.18 and later (the earliest supported version at the time of this writing). It may work with earlier MediaWiki versions, however.
Secure HTML is compatible with MediaWiki 1.23 and later.


{{ExtensionGitDownload|SecureHTML}}
{{ExtensionInstall|SecureHTML|registration=1|localsettings=
$wgSecureHTMLSecrets = [
* Edit <tt>LocalSettings.php</tt> in the root of your MediaWiki installation, and add the following lines near the bottom:
'keyname' => 'keysecret',
<syntaxhighlight lang="php">
];
require_once( "$IP/extensions/SecureHTML/SecureHTML.php" );
|custom-steps=

$wgSecureHTMLSecrets = array(
'keyname' => 'keysecret',
);
</syntaxhighlight>
* Modify $wgSecureHTMLSecrets as per below.
* Modify $wgSecureHTMLSecrets as per below.
* Go to ''Special:SecureHTML'' and use the page to create a hashed snippet of raw HTML using the key secrets defined.
* Go to ''Special:SecureHTML'' and use the page to create a hashed snippet of raw HTML using the key secrets defined.
* Add the hashed snippet to your desired wiki page.
* Add the hashed snippet to your desired wiki page.
}}


== Configuration ==
== Configuration ==

Secure HTML uses HMAC digests to sign a piece of raw HTML in a <tt>&lt;shtml&gt;</tt> tag, using a shared secret key. The $wgSecureHTMLSecrets configuration array may have multiple shared secrets, and is in the format:
Secure HTML uses HMAC digests to sign a piece of raw HTML in a {{tag|shtml|open}} tag, using a shared secret key. The $wgSecureHTMLSecrets configuration array may have multiple shared secrets and is in the format:

<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
$wgSecureHTMLSecrets = array(
$wgSecureHTMLSecrets = [
'Wiki admin' => 'foo',
'Wiki admin' => 'zX6Zn2WRKlQt',
'developers' => 'bar',
'developers' => '7RCkFRjw68CL',
'Support department' => 'baz',
'Support department' => 'NL9g5QntWNbC',
);
];
</syntaxhighlight>
</syntaxhighlight>


The first part of each pair is the key name, and the second part is the key secret. This way, you can logically segment shared secrets among several groups. If a <tt>keyname=</tt> parameter is not given to the <tt>&lt;shtml&gt;</tt> tag, the first entry in $wgSecureHTMLSecrets is assumed. So, for example:
The first part of each pair is the key name, and the second part is the key secret. This way, you can logically segment shared secrets among several groups. If a <nowiki>code></nowiki> parameter is not given to the {{tag|shtml|open}} tag, the first entry in $wgSecureHTMLSecrets is assumed. So, for example:

<pre>
<syntaxhighlight lang="html">
<!-- Use the default key ("Wiki admin" in the above example), signed with "foo" -->
<!-- Use the default key ("Wiki admin" in the above example), signed with "zX6Zn2WRKlQt" -->
<shtml version="2" hash="ab...cd">HTML</shtml>
<shtml hash="ab...cd">HTML</shtml>


<!-- Or specify the key name explicitly -->
<!-- Or specify the key name explicitly -->
<shtml version="2" keyname="Wiki admin" hash="ab...cd">HTML</shtml>
<shtml keyname="Wiki admin" hash="ab...cd">HTML</shtml>


<!-- Use the "developers" key, signed with "bar" -->
<!-- Use the "developers" key, signed with "7RCkFRjw68CL" -->
<shtml version="2" keyname="developers" hash="ab...cd">HTML</shtml>
<shtml keyname="developers" hash="ef...01">HTML</shtml>
</syntaxhighlight>
</pre>


The default tag name is <code>shtml</code>, but may be changed by setting e.g.:
== Special:SecureHTML ==
The special page ''Special:SecureHTML'' is used to build the snippet, specifying the raw HTML, the key secret, and (optionally) the key name. If a key name is not specified, the first entry in $wgSecureHTMLSecrets is assumed. When the form is submitted, the signed snipped is displayed, and an attempt to render the snippet is made. If the key secret is incorrect, this will show you the results immediately, before you try to add the snippet to a page.


<syntaxhighlight lang="php">
As of version 2.1, the special page is restricted to users who have the 'edit' right; the rationale being the user needs to be able to edit pages anyway to make use of this extension. If you would like to change this right, set $wgSecureHTMLSpecialRight to another right, or set to <nowiki>''</nowiki> to allow anyone to use the special page.
$wgSecureHTMLTag = 'securehtml';
</syntaxhighlight>


== Usage ==
Note that this restriction does not provide much extra security. If your MediaWiki installation requires users to be logged in to edit, it does provide superficial protection against anonymous dictionary attacks (checking the preview result) against a key. However, if a user already knows a key secret, he/she can build the signed snippet manually; the special page is not strictly needed.


The special page ''Special:SecureHTML'' is used to build the snippet, specifying the raw HTML, the key secret, and (optionally) the key name. If a key name is not specified, the first entry in $wgSecureHTMLSecrets is assumed. When the form is submitted, the signed snip is displayed, and an attempt to render the snippet is made. If the key secret is incorrect, you will see the results immediately before adding the snippet to a page.
== Internationalization ==
Translation of the extension strings is managed by [[Translatewiki.net]] ([http://translatewiki.net/w/i.php?title=Special%3ATranslate&taction=translate&group=ext-securehtml direct extension link]). Please contribute translations there.


Special:SecureHTML is restricted to users with the 'edit' right; the rationale is that the user needs to be able to edit pages anyway to use this extension. If you want to change this right, set $wgSecureHTMLSpecialRight to another right or set it to <nowiki>''</nowiki> to allow anyone to use the special page.
== Hash versions ==

=== Version 1 (deprecated) ===
Note that this restriction does not provide much extra security. If your MediaWiki installation requires users to be logged in to edit, it does provide superficial protection against anonymous dictionary attacks (checking the preview result) against a key. However, if users already know a key secret, they can build the signed snippet manually; the special page is not strictly needed.
The original version of this extension stored its keys in the global $shtml_keys, and used a simple MD5 concatenation of the key secret and the raw HTML to form the hash. This is potentialy less secure than HMAC, and has been deprecated. If a <tt>version=</tt> parameter is not given to the <tt>&lt;shtml&gt;</tt> tag, version 1 is assumed, and keys will be taken from $shtml_keys.

By default, Special:SecureHTML presents a dropdown list of configured key names from which to select. If you would rather not show all key names, set the following to turn the field into a freeform text input:

<syntaxhighlight lang="php">
$wgSecureHTMLSpecialDropdown = False;
</syntaxhighlight>

== Hash algorithms ==

Beginning with version 3.0, HMAC hash algorithms are configurable per key. The default is HMAC SHA256 when the key value is a string (the secret), but may be extended, for example:

<syntaxhighlight lang="php">
$wgSecureHTMLSecrets = [
'default sha256' => 'vwJ2prl4B4bg',
'custom sha512' => [
'algorithm' => 'sha512',
'secret' => 'RZQ8R99C95Xn',
],
'custom whirlpool' => [
'algorithm' => 'whirlpool',
'secret' => 'FXtN2QHflVPf',
],
];
</syntaxhighlight>


SHA256 should be secure for most purposes, but if you pick a custom algorithm, be careful which one you choose. For example, adler32 would be a very bad choice for hashing.
This interface is retained for backwards compatibility with the original version. If you have upgraded from the original version, please convert your snippets as soon as possible, as this interface will be removed at a future date.


Version 1.0 used a simple data + secret MD5 hash, which is now considered cryptographically insecure. This format was deprecated in version 2.0 in favor of HMAC SHA256 hashes and removed in 3.0. Any existing version 1.0 hashes must be converted to new hashes.
=== Version 2 ===
Version 2 is signified by <tt>&lt;shtml version="2" ...&gt;</tt>, and uses a HMAC_SHA256 digest of the raw HTML and key secret. It is the current, default (when using ''Special:SecureHTML'' to generate the snippet) and preferred interface.


==See also==
==See also==
* {{ll|HTML_restriction}} - list of extensions that allow for the inclusion of raw HTML
* [[Extension:NamespaceHTML]] - Allows raw HTML in specified namespaces
* [[Extension:SaferHTMLTag]] - Allows only sysops and certain user groups to edit pages containing the {{tag|html|open}} tag


[[Category:Script embedding extensions]]
[[Category:Script embedding extensions{{#translation:}}]]

Latest revision as of 15:48, 3 November 2025

MediaWiki extensions manual
Secure HTML
Release status: unmaintained
Implementation Tag , User rights
Description Lets you include arbitrary HTML in an authorized and secure way
Author(s) Ryan Finnie (Fo0bartalk)
Latest version 3.0 (2016-07-29)
MediaWiki 1.23+
$wgSecureHTMLSecrets, $wgSecureHTMLSpecialRight, $wgSecureHTMLSpecialDropdown, $wgSecureHTMLTag
‎<shtml> (configurable)
Licence GNU General Public License 2.0 or later
Download Download latest stable release (3.0), or:
Example ‎<shtml> tag (PayPal forms), Special:SecureHTML
Translate the Secure HTML extension if it is available at translatewiki.net

The Secure HTML extension lets you include arbitrary HTML in an authorized and secure way

Rationale

[edit | edit source]

Occasionally, you need to display HTML within a wiki, but allowing it site-wide opens you up to various XSS attacks. This extension solves that problem by letting you specify arbitrary HTML, but only if the HTML includes a corresponding hash created by signing the HTML input with a secret only authorized people know.

The extension uses a special page, Special:SecureHTML which helps you build a tag, ‎<shtml>, which acts as a wrapper around raw HTML.

Example

[edit | edit source]
<shtml
  hash="7fa503206cb1de131dd6acdca576e92262dd6d176cc3466073a343863743b8ed"
><strong>Hello world!</strong></shtml>

If the user uses a valid shared secret to build the hashed ‎<shtml> snippet and includes it in a wiki page, the snippet is rendered as the raw HTML contained within the tag. If the shared secret is invalid, the snippet is rendered as an error message (but does not contain the HTML).

Installation

[edit | edit source]

Secure HTML is compatible with MediaWiki 1.23 and later.


  • Download and move the extracted SecureHTML folder to your extensions/ directory.
    Developers and code contributors should install the extension from Git instead, using:
    cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/SecureHTML
    
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'SecureHTML' );
    $wgSecureHTMLSecrets = [
        'keyname' => 'keysecret',
    ];
    
  • Modify $wgSecureHTMLSecrets as per below.
  • Go to Special:SecureHTML and use the page to create a hashed snippet of raw HTML using the key secrets defined.
  • Add the hashed snippet to your desired wiki page.
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Configuration

[edit | edit source]

Secure HTML uses HMAC digests to sign a piece of raw HTML in a ‎<shtml> tag, using a shared secret key. The $wgSecureHTMLSecrets configuration array may have multiple shared secrets and is in the format:

$wgSecureHTMLSecrets = [
    'Wiki admin' => 'zX6Zn2WRKlQt',
    'developers' => '7RCkFRjw68CL',
    'Support department' => 'NL9g5QntWNbC',
];

The first part of each pair is the key name, and the second part is the key secret. This way, you can logically segment shared secrets among several groups. If a code> parameter is not given to the ‎<shtml> tag, the first entry in $wgSecureHTMLSecrets is assumed. So, for example:

<!-- Use the default key ("Wiki admin" in the above example), signed with "zX6Zn2WRKlQt" -->
<shtml hash="ab...cd">HTML</shtml>

<!-- Or specify the key name explicitly -->
<shtml keyname="Wiki admin" hash="ab...cd">HTML</shtml>

<!-- Use the "developers" key, signed with "7RCkFRjw68CL" -->
<shtml keyname="developers" hash="ef...01">HTML</shtml>

The default tag name is shtml, but may be changed by setting e.g.:

$wgSecureHTMLTag = 'securehtml';

Usage

[edit | edit source]

The special page Special:SecureHTML is used to build the snippet, specifying the raw HTML, the key secret, and (optionally) the key name. If a key name is not specified, the first entry in $wgSecureHTMLSecrets is assumed. When the form is submitted, the signed snip is displayed, and an attempt to render the snippet is made. If the key secret is incorrect, you will see the results immediately before adding the snippet to a page.

Special:SecureHTML is restricted to users with the 'edit' right; the rationale is that the user needs to be able to edit pages anyway to use this extension. If you want to change this right, set $wgSecureHTMLSpecialRight to another right or set it to '' to allow anyone to use the special page.

Note that this restriction does not provide much extra security. If your MediaWiki installation requires users to be logged in to edit, it does provide superficial protection against anonymous dictionary attacks (checking the preview result) against a key. However, if users already know a key secret, they can build the signed snippet manually; the special page is not strictly needed.

By default, Special:SecureHTML presents a dropdown list of configured key names from which to select. If you would rather not show all key names, set the following to turn the field into a freeform text input:

$wgSecureHTMLSpecialDropdown = False;

Hash algorithms

[edit | edit source]

Beginning with version 3.0, HMAC hash algorithms are configurable per key. The default is HMAC SHA256 when the key value is a string (the secret), but may be extended, for example:

$wgSecureHTMLSecrets = [
    'default sha256' => 'vwJ2prl4B4bg',
    'custom sha512' => [
        'algorithm' => 'sha512',
        'secret' => 'RZQ8R99C95Xn',
    ],
    'custom whirlpool' => [
        'algorithm' => 'whirlpool',
        'secret' => 'FXtN2QHflVPf',
    ],
];

SHA256 should be secure for most purposes, but if you pick a custom algorithm, be careful which one you choose. For example, adler32 would be a very bad choice for hashing.

Version 1.0 used a simple data + secret MD5 hash, which is now considered cryptographically insecure. This format was deprecated in version 2.0 in favor of HMAC SHA256 hashes and removed in 3.0. Any existing version 1.0 hashes must be converted to new hashes.

See also

[edit | edit source]