r50465 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50464‎ | r50465 | r50466 >
Date:00:51, 11 May 2009
Author:laner
Status:deferred
Tags:
Comment:
* Add width, length, and labels arguments in PlottersParser.php
* Allow user defined width and length in PlottersClass.php
* Closed a bunch of XSS attack vectors
Modified paths:
  • /trunk/extensions/Plotters/PlottersClass.php (modified) (history)
  • /trunk/extensions/Plotters/PlottersParser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Plotters/PlottersClass.php
@@ -66,9 +66,8 @@
6767 }
6868
6969 function renderPlot() {
70 - // TODO: allow user defined height and width
7170 // TODO: allow user defined graph id
72 - return '<div><canvas id="graph" height="300" width="300"></canvas></div>';
 71+ return '<div><canvas id="graph" height="' . $this->argumentArray["height"] . '" width="' . $this->argumentArray["width"] . '"></canvas></div>';
7372 }
7473
7574 function renderFallback() {
@@ -77,19 +76,21 @@
7877 }
7978
8079 function renderJavascript() {
 80+ $output = '<script type="text/javascript">';
 81+ // TODO: allow user defined graph id
 82+ $output .= 'function drawGraph() {';
 83+ $output .= 'var data = [];';
 84+
8185 // Prepare data
82 - $data = "[";
83 - foreach ( $this->dataArray as $line ) {
84 - $data .= "[" . implode( $this->argumentArray["datasep"], $line ) . "]" . ", ";
 86+ for ( $i = 0; $i < count( $this->dataArray ); $i++ ) {
 87+ $output .= "data[$i] = [];";
 88+ $dataline = $this->dataArray[$i];
 89+ for ( $j = 0; $j < count( $dataline ); $j++ ) {
 90+ $output .= "data[$i][$j] = '" . $dataline[$j] . "';";
 91+ }
8592 }
86 - $data = substr( $data, 0, -2 );
87 - $data .= "]";
8893
8994 // Run preprocessors
90 - $output = '<script type="text/javascript">';
91 - // TODO: allow user defined graph id
92 - $output .= 'function drawGraph() {';
93 - $output .= 'var data = ' . $data . ';';
9495 foreach ( $this->argumentArray["preprocessors"] as $preprocessor ) {
9596 $output .= 'data = plotter_' . $preprocessor . '_process( data, ';
9697 foreach ( $this->argumentArray["preprocessorarguments"] as $argument ) {
@@ -103,7 +104,7 @@
104105 // Run script
105106 $output .= 'plotter_' . $this->argumentArray["script"] . '_draw( data, ';
106107 foreach ( $this->argumentArray["scriptarguments"] as $argument ) {
107 - $output .= $argument . ', ';
 108+ $output .= "'" . $argument . "'" . ", ";
108109 }
109110 $output = substr( $output, 0, -2 );
110111 $output .= " );";
Index: trunk/extensions/Plotters/PlottersParser.php
@@ -35,9 +35,10 @@
3636 function parseArguments( $argv ) {
3737 // Parse arguments, set defaults, and do sanity checks
3838 $this->argumentArray = array ( "renderer" => "plotkit", "preprocessors" => array(), "preprocessorarguments" => array(),
39 - "script" => "", "scriptarguments" => array(), "datasep" => "," );
 39+ "script" => "", "scriptarguments" => array(), "datasep" => ",", "width" => "300", "height" => "300", "labels" => array() );
4040 if ( isset( $argv["renderer"] ) ) {
41 - $this->argumentArray["renderer"] = $argv["renderer"];
 41+ //TODO: limit this to supported renderers
 42+ $this->argumentArray["renderer"] = preg_replace( '/[^A-Z0-9]/i', '', $argv["renderer"] );
4243 }
4344 if ( isset( $argv["preprocessors"] ) ) {
4445 // Sanitize scripts - alphanumerics only
@@ -48,16 +49,15 @@
4950 }
5051 if ( isset( $argv["preprocessorarguments"] ) ) {
5152 // Replace escaped separators
52 - $argv["preprocessorarguments"] = preg_replace( '/\\:/', '§UNIQ§', $argv["preprocessorarguments"] );
53 - $argv["preprocessorarguments"] = preg_replace( '/\\,/', '§UNIQ2§', $argv["preprocessorarguments"] );
 53+ $argv["preprocessorarguments"] = preg_replace( "/\\\:/", '§UNIQ§', $argv["preprocessorarguments"] );
 54+ $argv["preprocessorarguments"] = preg_replace( "/\\\,/", '§UNIQ2§', $argv["preprocessorarguments"] );
5455
55 - // Parse and sanitize arguments - escape single quotes and backslashes
 56+ // Parse and sanitize arguments
5657 $arguments = explode( ':', $argv["preprocessorarguments"] );
5758 foreach ( $arguments as $argument ) {
5859 $subargumentarr = explode( ',', $argument );
5960 foreach ( $subargumentarr as &$singleargument ) {
60 - $singleargument = preg_replace( "/\\\\/", '\\\\', $singleargument );
61 - $singleargument = preg_replace( "/'/", "\\'", $singleargument );
 61+ $singleargument = htmlentities( $singleargument, ENT_QUOTES );
6262
6363 // Fix escaped separators
6464 $singleargument = preg_replace( "/§UNIQ§/", ":", $singleargument );
@@ -73,22 +73,42 @@
7474 }
7575 if ( isset( $argv["scriptarguments"] ) ) {
7676 // Replace escaped separators
77 - $argv["scriptarguments"] = preg_replace( '/\\,/', '§UNIQ§', $argv["scriptarguments"] );
 77+ $argv["scriptarguments"] = preg_replace( "/\\\,/", '§UNIQ§', $argv["scriptarguments"] );
7878
79 - // Parse and sanitize arguments - escape single quotes and backslashes
 79+ // Parse and sanitize arguments
8080 $arguments = explode( ',', $argv["scriptarguments"] );
8181 foreach ( $arguments as $argument ) {
82 - $argument = preg_replace( "/\\\\/", '\\\\', $argument );
83 - $argument = preg_replace( "/'/", "\\'", $argument );
 82+ $argument = htmlentities( $argument, ENT_QUOTES );
8483
8584 // Fix escaped separators
8685 $argument = preg_replace( "/§UNIQ§/", ",", $argument );
8786 $this->argumentArray["scriptarguments"][] = $argument;
8887 }
 88+ Plotter::debug( 'plot script argument values: ', $this->argumentArray["scriptarguments"] );
8989 }
9090 if ( isset( $argv["datasep"] ) ) {
91 - $this->argumentArray["datasep"] = $argv["datasep"];
 91+ $this->argumentArray["datasep"] = htmlentities( $argv["datasep"], ENT_QUOTES );
9292 }
 93+ if ( isset( $argv["width"] ) ) {
 94+ $this->argumentArray["width"] = preg_replace( '/[^0-9]/', '', $argv["width"] );
 95+ }
 96+ if ( isset( $argv["height"] ) ) {
 97+ $this->argumentArray["height"] = preg_replace( '/[^0-9]/', '', $argv["height"] );
 98+ }
 99+ if ( isset( $argv["labels"] ) ) {
 100+ // Replace escaped separators
 101+ $argv["labels"] = preg_replace( "/\\\,/", '§UNIQ§', $argv["labels"] );
 102+
 103+ // Parse and sanitize arguments
 104+ $labels = explode( ',', $argv["labels"] );
 105+ foreach ( $labels as $label ) {
 106+ $label = htmlentities( $label, ENT_QUOTES );
 107+
 108+ // Fix escaped separators
 109+ $label = preg_replace( "/§UNIQ§/", ",", $label );
 110+ $this->argumentArray["labels"][] = $label;
 111+ }
 112+ }
93113 }
94114
95115 function parseData( $input, $parser ) {
@@ -96,13 +116,12 @@
97117 $sep = $this->argumentArray["datasep"];
98118 $input = preg_replace( "/\\\\$sep/", '§UNIQ§', $input );
99119
100 - // Parse and sanitize data - escape single quotes and backslashes
 120+ // Parse and sanitize data
101121 $lines = preg_split( "/\n/", $input, -1, PREG_SPLIT_NO_EMPTY );
102122 foreach ( $lines as $line ) {
103123 $values = explode( ',', $line );
104124 foreach ( $values as &$value ) {
105 - $value = preg_replace( "/\\\\/", "\\\\", $value );
106 - $value = preg_replace( "/'/", "\\'", $value );
 125+ $value = htmlentities( $value, ENT_QUOTES );
107126
108127 // Fix escaped separators
109128 $value = preg_replace( "/§UNIQ§/", "\\$sep", $value );

Status & tagging log