Commit graph

169 commits

Author SHA1 Message Date
Fomafix
110a5877e9 Use [...] instead of array(...) in PHP comments and documentation
Change-Id: I0c83783051bf35fe785bc01644eeb2946902b6b2
2019-06-17 21:15:09 +02:00
Reedy
c13fee87d4 Collapse some nested if statements
Change-Id: I9a97325d738d09370d29d35d5254bc0dadc57ff4
2019-04-04 19:02:22 +00:00
Fomafix
204126e7c7 Simplify strings in PHP code
Change-Id: I481810ade68b0c5a5be21d22e2a107646d5813e6
2019-03-01 22:16:26 +01:00
Thiemo Kreuz
37b3383e8b Remove comments literally repeating the next line of code
I would argue that these comments do not add any information that
would not be there already. Having them adds mental overhead, because
one needs to read both the comment and the next line of code first to
understand they say the exact same. I don't find this helpful, but
more distracting.

Change-Id: I39c98f25225947ebffdcc2fd8f0243e7a6c070d7
2019-02-27 17:28:40 +00:00
Brian Wolff
dbfe1c9a93 Add warning if you give Html::openElement a name with a space
Bug: T201747
Change-Id: I7772f1e8c0c12bdf1fd60d62e015f2ec82d8ac90
2018-12-20 18:03:56 -08:00
Erik Bernhardson
9f28aa0709 Render namespace lists in the user's language
Asking users to select from a list that is in a namespace
other than their user language can be quite difficult.
Transition a number of use cases. This was applied via a flag
to avoid unexpectedly changing extensions also utilizing
the namespace selector.

Pages updated:
* Special:Search
* Special:Watchlist
* Special:RecentChanges
* Special:Contributions
* Special:Import
* Special:WhatLinksHere

Bug: T174057
Change-Id: I3fdac72179a124849ef7ad1e0e54eb66396c3c6e
2018-10-12 11:27:20 -07:00
Timo Tijhof
15d6bffb56 Html: Reject </script> from inlineScript() and leave rest unescaped
There are three problems with the CDATA approach:

1. It doesn't work.

   HTML5 already interprets the contents of <script> tags as CDATA,
   which means escaping of characters like & is not needed. In fact,
   in HTML5 mode, a plain script tag with <script>0&amp;1;</script>
   would be a syntax error. Indicating it is not interpreted as
   text, but as CDATA. Effectively, the only thing an HTML parser
   looks for is </script>.

   And that's exactly the problem. Producing an inline script
   containing the characters "</string>" for legitimate reasons,
   is currently broken.
   No alternate wrapping or setting can make it work, either.

   See also:
   https://people.wikimedia.org/~krinkle/200506-html-inlinescript.html
   which contains:

   <script>/*<![CDATA[*/
   if (true && true) {
     console.log('This is a <script></script> tag (original)');
   }
   /*]]>*/</script>

   In a browser, the script is terminated by the first "</script>",
   leaving the code unfinished, throwing a SyntaxError, and outputting
   the rest of the script as plain text on the page.

2. CDATA is only for XML mode, whereas MediaWiki does not support
   the XML/XHTML output mode (since MediaWiki 1.22). Instead, we only
   output HTML (5). Code that does need to produce XML, should use the
   class from Xml.php instead.

3. It gives a false sense of security.

We could just remove the CDATA code as-is and that in itself would be an
improvement per point 2 and 3, and would break nothing per point 1.

However, this commit attempts to address the underlying bug by rejecting
the characters "</script>" from input. If this is needed in a literal,
it is the responsibility of the caller to escape it in a way that is
appropiate for how it is used (string, comment, regex, etc.).

There are two ways this can be used currently in core:

* User input as exported through JSON (e.g. mw.config, or mw.messages).
  This is already fine as both FormatJson::encode and json_encode handle
  escape either < or / in the string by default already.

* Previews of edits to user scripts. This is currently already broken and
  causes the script to end early and produce arbitrary HTML on the page.
  This commit limits the impact by refusing to output such script in a
  broken way. I will further address that use case in a follow-up.

Bug: T200506
Change-Id: I67ceb34eabf2f62fd3f3841b8f1459289fad28fb
2018-08-30 00:10:35 +01:00
Kunal Mehta
379407a640 Support a heading of '0' in Html::messageBox()
Change-Id: I181cb6f92ab1b10d87b1213a043a5f291149d094
2018-08-19 21:46:21 -07:00
Aryeh Gregor
90d4f56fe4 Mass conversion of $wgContLang to service
Brought to you by vim macros.

Bug: T200246
Change-Id: I79e919f4553e3bd3eb714073fed7a43051b4fb2a
2018-08-11 22:44:29 -06:00
Brian Wolff
53a18d1294 CSP: Allow an option of disabling nonces
The current rollout plan calls for initial rollout to only
disallow external JS, and leave removing unsafe inline stuff
to a later date. Thus this adds a useNonces option to the CSP
config to allow that.

Renamed ContentSecurityPolicy::isEnabled() to isNonceRequired
for clarity. The old name has never been in a released version
of MediaWiki, so is removed immediately.

Change-Id: I756d8e97b77c6f97dbbf040a20c8750fecb157c5
2018-07-10 00:12:32 +00:00
Umherirrender
130ec2523d Fix PhanTypeMismatchDeclaredParam
Auto fix MediaWiki.Commenting.FunctionComment.DefaultNullTypeParam sniff

Change-Id: I865323fd0295aabd06f3e3c75e0e5043fb31069e
2018-07-07 00:34:30 +00:00
Fomafix
125cbd8c01 Use \u{00A0} instead of &#160; or &nbsp;
Directly use the UTF-8 encoding of the 'NO-BREAK SPACE' (U+00A0) instead of
the HTML/XML entities &#160; or &#xa0; or &nbsp;.

With the UTF-8 character the generated HTML is shorter and better to read.

Also change the special value for the label in HTMLForm from &#160; to
U+00A0 but also support &#160; for backward compability.

Bug: T154300
Change-Id: I882599ac1120789bb4e524c4394870680caca4f4
2018-06-24 01:20:13 +00:00
Bartosz Dziewoński
485f66f174 Use PHP 7 '??' operator instead of '?:' with 'isset()' where convenient
Find: /isset\(\s*([^()]+?)\s*\)\s*\?\s*\1\s*:\s*/
Replace with: '\1 ?? '

(Everywhere except includes/PHPVersionCheck.php)
(Then, manually fix some line length and indentation issues)

Then manually reviewed the replacements for cases where confusing
operator precedence would result in incorrect results
(fixing those in I478db046a1cc162c6767003ce45c9b56270f3372).

Change-Id: I33b421c8cb11cdd4ce896488c9ff5313f03a38cf
2018-05-30 18:06:13 -07:00
Brian Wolff
70941efd35 Initial support for Content Security Policy, disabled by default
The primary goal here is a defense in depth measure to
stop an attacker who found a bug in the parser allowing
them to insert malicious attributes.

This wouldn't stop someone who could insert a full
script tag (since at current it can't distinguish between
malicious and legit user js). It also would not prevent
DOM-based or reflected XSS for anons, as the nonce value
is guessable for anons when receiving a response cached
by varnish. However, the limited protection of just stopping
stored XSS where the attacker only has control of attributes,
is still a big win in my opinion. (But it wouldn't prevent
someone who has that type of xss from abusing things like
data-ooui attribute).

This will likely break many gadgets. Its expected that any
sort of rollout on Wikimedia will be done very slowly, with
lots of testing and the report-only option to begin with.

This is behind feature flags that are off by default, so
merging this patch should not cause any change in default
behaviour.

This may break some extensions (The most obvious one
is charinsert (See fe648d41005), but will probably need
some testing in report-only mode to see if anything else breaks)

This uses the unsafe-eval option of CSP, in order to
support RL's local storage thingy. For better security,
we may want to remove some of the sillier uses of eval
(e.g. jquery.ui.datepicker.js).

For more info, see spec: https://www.w3.org/TR/CSP2/
Additionally see:
https://www.mediawiki.org/wiki/Requests_for_comment/Content-Security-Policy

Bug: T135963
Change-Id: I80f6f469ba4c0b608385483457df96ccb7429ae5
2018-05-13 21:01:11 -07:00
Brad Jorsch
9b2b375fce ParserOutput: Add 'deduplicateStyles' post-cache transformation
This transformation will find <style> tag with a "data-mw-deduplicate"
attribute. For each value of the attribute, the first instance will be
kept as-is, while any subsequent tags with the same value will be
replaced by a <link rel="mw-deduplicated-inline-style"> with its href
referring to the "data-mw-deduplicate" value using a custom scheme.

This also adds an $attribs parameter to Html::inlineStyle() so the
data-mw-deduplicate attribute can be added.

Note this doesn't actually depend on Ib931e25c, but action=mobileview
will break if it starts being used without that patch.

Bug: T160563
Change-Id: I055abdf4d73ec65771eaa4fe0999ec907c831568
Depends-On: Ib931e25ce85072000e62c486bbe5907f03372494
2018-02-11 05:55:56 +00:00
jdlrobson
ba45450feb Add unit tests for Html helper methods and change messageBox visibility
* Messagebox is now private to Html class to discourage unconventional
  usages
* Tests are added for all three helper methods added in
  4e7021a231

Bug: T166915
Change-Id: I1c3e4131b2439c0f4fb94ad4e616a909b52d6b78
2017-11-21 21:11:28 +00:00
jdlrobson
4e7021a231 Provide message/warning/error box abstraction
This will help us consolidate the various uses into one single
method which will help us drive standardisation of these defacto
widgets.

Hopefully, by being a method of the Html class, which has a very
low barrier for use will drive down the inconsistent display of
warning/error boxes across MediaWiki's products

Various usages of warningbox and errorbox have been ported over.
I've retained some more complicated usages which make use of the
parser (wrapWikiMsg) and any where id and class are medled with
- we'll probably want to consider whether we want to encourage
those going forward as they encourage adjusting the styling.

Bug: T166915
Change-Id: I2757e1f4ff2599e93a7257fc644cab69063896d2
2017-11-13 23:19:45 +00:00
Max Semenik
47416c0a86 Escape single quotes in attributes
Replaces a long excuse in Html class not to call
Sanitizer::encodeAttribute() with a call to it.
In anything security related, excuses are a sign that you're doing
something wrong:)

Change-Id: Icf7f60d8cd6ea757d8f1999d638b82733001f68a
2017-11-08 15:17:47 -08:00
Gergő Tisza
72d1859b94 Improve Html/Xml element generation docs
Corrects some docs to match actual behavior + clarifies what
syntax is allowed for attributes.

Change-Id: I45b27bc4d536063c508d902f592ad4d0dc265ed3
2017-03-01 16:13:29 -08:00
James D. Forrester
9635dda73a includes: Replace implicit Bugzilla bug numbers with Phab ones
It's unreasonable to expect newbies to know that "bug 12345" means "Task T14345"
except where it doesn't, so let's just standardise on the real numbers.

Change-Id: I6f59febaf8fc96e80f8cfc11f4356283f461142a
2017-02-21 18:13:24 +00:00
Bartosz Dziewoński
86e9469e5d Html: Unblacklist HTML5 form validation attributes
We blacklisted them in 2010. Modern browsers support them fairly well,
and it doesn't seem to conflict with any of our code.

I tested this with SecurePoll poll creation form, which contains an
astonishingly wide range of form controls and validation options.

Change-Id: I08244addcf9b6eb96137895f28e7b750914fef5c
2017-01-10 14:53:08 -08:00
Fomafix
202f695f67 Update weblinks in comments from HTTP to HTTPS
Use HTTPS instead of HTTP where the HTTP link is a redirect to the HTTPS link.

Also update some defect links.

Change-Id: Ic3a5eac910d098ed5c2a21e9f47c9b6ee06b2643
2016-11-07 15:24:46 +01:00
Amir Sarabadani
9850c542c6 Clean up array() syntax in docs, part VII
Last part

Change-Id: I38f015e2122ef4fd2d2141718bd889794c29f06c
2016-09-27 06:53:25 +03:30
Bartosz Dziewoński
d48c33e898 Html: Update list of $voidElements
`<command>` is nowhere to be found in current HTML5 specification.
Scarce documentation on the internet hints that it has been removed.

Change-Id: I2a704194c7e8f8ca307f9d97c7f47a47cfaf00a6
2016-09-19 16:26:28 +00:00
Brian Wolff
d0662487e6 SECURITY: Escape '<' and ']]>' in inline <style> blocks
This is to prevent people from closing the <style> tag, and
then doing arbitrary js-y things. In particular, this is needed
for when previewing user css pages.

This does not escape '>' since its used as the child selector
in css, and generally speaking, '>' is safe inside the contents
of elements.

Bug: T133147
Change-Id: If024398d7bd4b578ad7f8c74367787f5b19eb9d7
2016-08-23 03:34:22 +00:00
Timo Tijhof
f47d1eee0e OutputPage: Fix blank line between <html> and <head>
Follows-up a464d1d41 which changed OutputPage::headElement()
to join pieces by a line break instead of hardcoding line breaks
after (some) generated pieces.

This caused a minor regression in the form of a blank line between
<html> and <head> on every page, because I missed the one that
came from this class.

Change-Id: I5e48b852809699b205f4581c833605f3e232610a
2016-07-21 21:08:24 +01:00
Matthias Mullie
8c32946ca1 Exclude duplicate srcset urls
Bug: T135550
Change-Id: I956dc155426739d60052a0dc77dafdf0414d5bd7
2016-07-20 12:16:47 +02:00
Brian Wolff
ee4d5c6eed Remove support for $wgWellFormedXml=false
tl;dr: Having unnessary complexity in security critical code is bad.

* Extra options add extra complexity and maintenance burden
** Thus we should only have one html output mode. well formed = false
     was already vetoed in T52040, so lets go with WellFormed=true.
* Options which are used by very few people tend to get tested less
* Escaping is an area of code where we should be very conservative
* Having escaping rules depend on making assumptions about which
    characters various browsers consider "whitespace" is scary
* $wgWellFormedXml=false has had a negative security impact in the
    past (Usually not directly its fault, but has made other bugs
    more exploitable)
* Saving a couple bytes (even less bytes after gzip taken into
    account) is really not worth it in this context (imho).

Change-Id: I5c922e0980d3f9eb39adb5bb5833e158afda42ed
2016-05-12 17:40:01 -04:00
Timo Tijhof
dd2d7d0ffc OutputPage: Minor clean up of <head> and HTML
* Remove trailing space in self-closing tag.
  Brings parserTest output of Parser and Parsoid closer together.

* Remove various line breaks at begin and end of script contents.

* Remove FILTER_NOMIN from makeConfigSetScript() output.
  This isn't part of any user- or page-dependent module and not minified.
  And Xml::encodeJsCall already ensures compact output for prod mode.

Bug: T127328
Change-Id: I85a5a59fd0955c1a112e8b24b933f0d9e983a156
2016-03-24 03:24:31 +00:00
Kunal Mehta
6e9b4f0e9c Convert all array() syntax to []
Per wikitech-l consensus:
 https://lists.wikimedia.org/pipermail/wikitech-l/2016-February/084821.html

Notes:
* Disabled CallTimePassByReference due to false positives (T127163)

Change-Id: I2c8ce713ce6600a0bb7bf67537c87044c7a45c4b
2016-02-17 01:33:00 -08:00
Ori Livneh
268da96291 Remove redundant 'type' attributes from style and script tags
Already stripped by Html::element before actual output, but
remove them from the attribute arrays as well.

Change-Id: I8699ca7bf40df07e9d4c370f6863003c095ced0e
2015-12-09 20:07:13 +00:00
Edward Chernenko
088f01bbe6 Typo in comment of Html::radio()
Incorrectly copied documentation from Html::check().

Change-Id: I508e4beedb01b62989a7f37466c47ddd37091b49
2015-10-16 13:51:00 +00:00
umherirrender
977c810302 Remove empty line comments
Remove empty line comments as found by the
MediaWiki.WhiteSpace.SpaceBeforeSingleLineComment.EmptyComment sniff

Change-Id: I5d694f7a7d3bc97e16300ba03c60ad17f3c912a5
2015-10-14 09:46:44 +02:00
umherirrender
c572d18661 Fixed spacing
- Removed space after cast
- Removed spaces in array index
- Removed double spaces
- Added spaces around string concat
- Fixed mixed tabs and spaces at begin of line

Change-Id: I38e849723f055d2d4c05cba72f5c245a28e8d5da
2015-09-26 20:44:54 +00:00
Bartosz Dziewoński
53b012ce2a OOUIHTMLForm: Implement HTMLSelectNamespace
* Extracted some common code between this and Html::namespaceSelector
  into a new method Html::namespaceSelectorOptions().

Change-Id: I5e97e5c661582f726153533ad00695b450caed46
2015-07-13 19:20:09 +02:00
Bartosz Dziewoński
1996e35ec7 Html: Add buttonAttributes() more diligently
It was done only for elements generated with linkButton() or
submitButton(), and not input().

Change-Id: Idd7e01d596997bd5cade5a7851daf64bee10bb49
2015-06-05 01:15:14 +00:00
kaldari
f74b88311b Fixing definition and use of $attrs parameter in Html::buttonAttributes
Also clarifying definition of $modifiers parameter.

Also simplifying code for case where $attrs['class'] is not set.

Change-Id: I425211681ba75cb71c1ccc3b3c038c075ea9acb9
2015-06-01 18:53:15 -07:00
Thiemo Mättig
bf1341985b Add array type hints to minor methods in the Html class
I'm aware that adding these type hints does have the potential of beeing
a breaking change if a caller misuses it. Note that it really is a misuse
in this case because all these parameters are documented as "array" and
nothing else.

I double-checked the usages of all methods I touched and could not find
any caller that does not fulfill the contract of these methods - in other
words, all callers I can find in my local code base (which includes all
major extensions like Echo, Flow, Parsoid, VisualEditor and so on) pass
arrays to these parameters.

I left the main methods openElement, rawElement and so on untouched
because they are called way to often (500 times and more).

Change-Id: I5ca13b26fb08d732ce4cadc4ee3d38314e606fd3
2015-04-08 20:26:24 +00:00
Ori Livneh
6333fa6191 Html::srcSet: allow density to be specified either with or without trailing 'x'
$wgLogoHD is meant to contain high-density alternatives for $wgLogo, but its
keys include the trailing 'x' (e.g., '1.5x'), making it unusable with
Html::srcSet(). Fix that by normalizing all density values to have a
single trailing 'x'.

Change-Id: I62cc3a9e4aeff3a7cb102de2965b8b40fd106c37
2015-04-05 05:22:47 +00:00
csteipp
125ed0e5ff SECURITY: Escape > in Html::expandAttributes
Escape > characters in attributes, so we don't confuse post-processing,
like LanguageConverter.

Bug: T73394
Change-Id: I768e2a12c7b6ba635e6c8571676b8c776b16bf72
2015-04-01 09:56:12 -07:00
Thiemo Mättig
5a84fbf457 Replace Html::... with self::... in the Html class
How cool is that, I can call a patch "SelfHTML". ;-)

Change-Id: I17d36bc45a349c92715b88004aaae046d4f7be1c
2015-03-27 17:47:31 +00:00
Thiemo Mättig
7ff53f8e07 Fix misleading $class = false default in Html::infoBox
I found this because my PHPStorm complains about the type mismatch.
I could have changed the @param tag to "string|bool", but when looking
at the code, the $class variable is casted to a string anyway and
never used as a bool.

Change-Id: I3450fa8a898923bbae26830ed3be0017685020d3
2015-03-26 10:43:01 +01:00
Thiemo Mättig
afe08307d6 More specific types in doc tags in the Html class
This is a pure inline-documentation patch. It fixes a few actual
mistakes in documentation tags and makes some generic "array" types
more specific, if that's possible.

Change-Id: Id02e1e936624b845316b8ce99f8b8d2a1f829e97
2015-03-26 10:38:35 +01:00
Bartosz Dziewoński
98ef0a2f5d Remove a hack, and a hack for the hack, for MediaWiki UI input fields
Hack #1: We were ignoring the 'size' attribute of input fields when
  $wgUseMediaWikiUIEverywhere was true. Let's not do that.

Hack #2: We were setting a min-width for MediaWiki UI input fields,
  because fields which were supposed to be full-line were becoming
  tiny because of hack #1. Let's not do that either.

Bug: T92498
Change-Id: I1d2c6c9eb60b52a7267c122a719cfdaa1f74f815
2015-03-14 15:18:31 +00:00
Bartosz Dziewoński
896fdb3d97 Html: Make addition of 'mw-ui-input' conditional on $wgUseMediaWikiUIEverywhere
We were always adding it previously, which seemed harmless since
'mediawiki.ui.input' RL module, providing the styling, was only loaded
if $wgUseMediaWikiUIEverywhere was true… unless someone loaded it
manually to have specific input fields styled. Whoops.

There are a lot more unconditional additions like this in tons of
places in the code, and someone should check whether each one is
intentional or not, but probably no one will. Oh well.

Bug: T92496
Change-Id: I5e91a3852a76ebbbfe64485bccb4c30ddee28b66
2015-03-13 19:02:34 +00:00
Amir E. Aharoni
4cae45a5a6 Shorten long lines in Html.php to make phpcs pass
Change-Id: I5060b510105aa08f1cb4804e2c33d8e1b00ef390
2015-03-03 21:41:20 +00:00
Ricordisamoa
55fc61c474 Html::openElement: Don't omit closing tags.
Logic added with commit eefe1b13a3
for both openElement and closeElement, removed with commit
d950da075d but only from closeElement.

Reported by Petr Bena on Wikitech-l:
https://lists.wikimedia.org/pipermail/wikitech-l/2015-February/080861.html

Change-Id: I6ed45763f7e95f7f39b73432e4f6152431891963
2015-02-18 22:35:22 +01:00
Federico Leva
5e93f83758 Don't discourage usage of Html::element()
Old advice from 2009 (7aa4a8f9), not quite useful nowadays. The preceding
sentence already says that in absence of attributes the function may not
do much.

Change-Id: I4d276d6f42394fc09662ddfd7e1ffd13fb197bf6
2015-01-09 08:02:42 +01:00
Ricordisamoa
12dec5d85d Fix some stuttering in comments and documentation
Change-Id: I9c0088b9aab37335203cad45a1d6fa8ac3f43321
2014-12-17 19:44:10 +00:00
Bartosz Dziewoński
3e85dfb8ad Standardize indentation of multiline 'if'/'elseif' conditions
Always indent the continuation one level deeper, and always place the
closing parenthesis on the next line, per coding conventions.
https://www.mediawiki.org/wiki/Manual:Coding_conventions#Indenting_and_alignment
https://www.mediawiki.org/wiki/Manual:Coding_conventions#Line_continuation

Regexp used: (\t+)(if|while|\} elseif|foreach).+(?<![;}])\n\1\S

Also:
* Change to just one line if line length stays under 100 characters.
* Add "// Do nothing" comment in empty 'if' bodies.
* Change '#' comments to '//' comments near affected code.

Change-Id: I4f62658fddb5a0ed18bbf9b2231cd794683d6402
2014-11-17 15:33:34 +00:00