From 1ec767791b90887154a39b92ed0d9c633003609d Mon Sep 17 00:00:00 2001 From: physikerwelt Date: Fri, 11 Dec 2015 08:23:45 +0100 Subject: [PATCH] Make math usable without RESTbase * Do not contact RESTbase in texvc rendering mode and use the well established tex checking that is already build into texvc. * Do not use RESTbase in LaTeXML mode. Background: In I1982612e8c6a356e3dbdf447724ac82e5968cc77 RESTbased replaced texvccheck that was a derivate of texvc designed for people that wanted to use secure MathML rendering using mathoid. The integration of mathoid to restbase made this feature obsolete. However, texvccheck was not only used to check the latex input that was sent to mathoid, but also the string which was sent to texvc. Since texvc has already build in tex checking this is not required and does not improve security. Finally, users updating from old versions of the math extension (prior to 2014) that do not have textexvccheck installed, do not need to compile the texvccheck binary after this change. PS5: Also treats the case where VisualEditor is not installed. Bug: T121173 Change-Id: I1bd076b09206869b5ed75280d22e1b36bfb8d8ad --- MathInputCheckRestbase.php | 11 +++++++++++ MathMathML.php | 2 +- MathRenderer.php | 16 +++++++++------- MathRestbaseInterface.php | 25 ++++++++++++++++++------- MathTexvc.php | 14 ++++++++++++++ 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/MathInputCheckRestbase.php b/MathInputCheckRestbase.php index bf17b64..0fd2bc5 100644 --- a/MathInputCheckRestbase.php +++ b/MathInputCheckRestbase.php @@ -38,6 +38,17 @@ class MathInputCheckRestbase extends MathInputCheck { if ( isset( $e->error->message ) ){ if ( $e->error->message === 'Illegal TeX function' ) { return $errorRenderer->getError( 'math_unknown_function', $e->error->found ); + } elseif ( preg_match( '/Math extension/', $e->error->message ) ) { + $names = MathHooks::getMathNames(); + $mode = $names['mathml']; + try { + $host = $this->restbaseInterface->getUrl( '' ); + } + catch ( Exception $ignore ) { + $host = 'invalid'; + } + $msg = $e->error->message; + return $errorRenderer->getError( 'math_invalidresponse', $mode, $host, $msg ); } return $errorRenderer->getError( 'math_syntax_error' ); } diff --git a/MathMathML.php b/MathMathML.php index dff9f41..58b9900 100644 --- a/MathMathML.php +++ b/MathMathML.php @@ -86,7 +86,7 @@ class MathMathML extends MathRenderer { * @see MathRenderer::render() */ public function render( $forceReRendering = false ) { - if ( $this->inputType == 'tex' ) { + if ( $this->inputType == 'tex' && $this->mode == 'mathml' ) { $tex = $this->getTex(); $displaystyle = false; if ( $this->getMathStyle() == 'inlineDisplaystyle' ) { diff --git a/MathRenderer.php b/MathRenderer.php index 1fe7ba7..fa7c4c9 100644 --- a/MathRenderer.php +++ b/MathRenderer.php @@ -573,14 +573,16 @@ abstract class MathRenderer { } } $checker = new MathInputCheckRestbase( $this->userInputTex ); - if ( $checker->isValid() ) { - $this->setTex( $checker->getValidTex() ); - $this->texSecure = true; - return true; - } else { - $this->lastError = $checker->getError(); - return false; + try { + if ( $checker->isValid() ) { + $this->setTex( $checker->getValidTex() ); + $this->texSecure = true; + return true; + } + } catch ( MWException $e ) { } + $this->lastError = $checker->getError(); + return false; } } diff --git a/MathRestbaseInterface.php b/MathRestbaseInterface.php index 59fc1ec..e0d5680 100644 --- a/MathRestbaseInterface.php +++ b/MathRestbaseInterface.php @@ -43,7 +43,7 @@ class MathRestbaseInterface { $this->calculateHash(); $request = array( 'method' => 'GET', - 'url' => self::getUrl( "media/math/render/$type/{$this->hash}" ) + 'url' => $this->getUrl( "media/math/render/$type/{$this->hash}" ) ); $serviceClient = $this->getServiceClient(); $response = $serviceClient->run( $request ); @@ -83,6 +83,9 @@ class MathRestbaseInterface { if ( isset( $json->detail ) && isset( $json->detail->success ) ) { $this->success = $json->detail->success; $this->error = $json->detail; + } else { + $this->success = false; + $this->setErrorMessage( 'Math extension cannot connect to Restbase.' ); } return false; } @@ -104,7 +107,7 @@ class MathRestbaseInterface { 'body' => $post ); $serviceClient = $this->getServiceClient(); - $request['url'] = self::getUrl( "media/math/check/{$this->type}" ); + $request['url'] = $this->getUrl( "media/math/check/{$this->type}" ); $response = $serviceClient->run( $request ); if ( $response['code'] === 200 ) { $res = $response['body']; @@ -156,7 +159,7 @@ class MathRestbaseInterface { * @return string * @throws MWException */ - private static function getUrl( $path, $internal = true ) { + public function getUrl( $path, $internal = true ) { global $wgVirtualRestConfig, $wgMathFullRestbaseURL, $wgVisualEditorFullRestbaseURL; if ( $internal && isset( $wgVirtualRestConfig['modules']['restbase'] ) ) { return "/mathoid/local/v1/$path"; @@ -167,8 +170,9 @@ class MathRestbaseInterface { if ( $wgVisualEditorFullRestbaseURL ) { return "{$wgVisualEditorFullRestbaseURL}v1/$path"; } - throw new MWException( 'Math extension can not find Restbase URL.'. - ' Please specify $wgMathFullRestbaseURL.' ); + $msg = 'Math extension can not find Restbase URL. Please specify $wgMathFullRestbaseURL.'; + $this->setErrorMessage( $msg ); + throw new MWException( $msg ); } /** @@ -190,7 +194,7 @@ class MathRestbaseInterface { try { $request = array( 'method' => 'GET', - 'url' => self::getUrl( '?spec' ) + 'url' => $this->getUrl( '?spec' ) ); } catch ( Exception $e ) { return false; @@ -244,7 +248,7 @@ class MathRestbaseInterface { */ public function getFullSvgUrl() { $this->calculateHash(); - return self::getUrl( "media/math/render/svg/{$this->hash}", false ); + return $this->getUrl( "media/math/render/svg/{$this->hash}", false ); } /** @@ -289,4 +293,11 @@ class MathRestbaseInterface { return $this->type; } + private function setErrorMessage( $msg ) { + $this->error = (object)array( + 'error' => + (object)array( 'message' => $msg ) + ); + } + } diff --git a/MathTexvc.php b/MathTexvc.php index eec8869..fae0576 100644 --- a/MathTexvc.php +++ b/MathTexvc.php @@ -465,4 +465,18 @@ class MathTexvc extends MathRenderer { public function setOutputHash( $hash ) { $this->hash = $hash; } + + /** + * Skip tex check for texvc rendering mode. + * Checking the tex code in texvc mode just adds a dependency to the + * texvccheck binary which does not improve security since the same + * checks are performed by texvc anyhow. Especially given the fact that + * texvccheck was derived from texvc. + * @return bool + */ + public function checkTex() { + return true; + } + + }