Page MenuHomePhabricator

sanitizeIP and prettifyIP throw error on non-string inputs
Closed, ResolvedPublic2 Estimated Story PointsBUG REPORT

Description

What is the problem?

The sanitizeIP() and prettifyIP() JavaScript functions (in utils.js) throw an error when the argument passed to them is not a string.

This behaviour is different to the PHP version of those functions (in IPUtils) on which they are based.

JavaScript:

mw.util.sanitizeIP( false );
> Uncaught TypeError: ip.replace is not a function
mw.util.sanitizeIP( true );
> Uncaught TypeError: ip.replace is not a function
mw.util.sanitizeIP( null );
> Uncaught TypeError: ip is null

PHP:

>>> IPUtils::sanitizeIP( false );
=> null
>>> IPUtils::sanitizeIP( true );
=> "1"
>>> IPUtils::sanitizeIP( null );
=> null

The same is probably true for other data types such as array, etc.

Steps to reproduce problem
  1. Go to any page on any wiki
  2. Open the browser console
  3. Enter: mw.util.sanitizeIP(true)

Expected behavior: Returns something like: "", null, true, "1"
Observed behavior: Throws an error

Environment

Wiki(s): MediaWiki 1.39.0-alpha (a41d695) 13:19, 17 March 2022.

Event Timeline

Tchanders set the point value for this task to 2.Mar 21 2022, 2:04 PM

Change 773289 had a related patch set uploaded (by Tchanders; author: Tchanders):

[mediawiki/core@master] mediawiki.util/util.js: Handle non-string input in sanitizeIP

https://gerrit.wikimedia.org/r/773289

N.B. The new patch returns null on non-string input, so we won't have exact parity between the PHP and JS implementations. But the PHP implementation returns the return value of trim(), for which there isn't an equivalent in JS, and probably isn't particularly intentional behaviour.

Change 773289 merged by jenkins-bot:

[mediawiki/core@master] mediawiki.util: Handle non-string input in sanitizeIP

https://gerrit.wikimedia.org/r/773289

Confirmed the issue is fixed on Beta and Test.wiki. See screenshot below of fix on Beta:

Screen Shot 2022-03-30 at 10.51.59 PM.png (1×2 px, 359 KB)

Below screenshot shows the issue as is on Prod:

Screen Shot 2022-03-30 at 10.52.50 PM.png (1×2 px, 590 KB)