-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
patch for webservices XXE/XEE vulnerability #2177
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,11 @@ class CWebService extends CComponent | |
* @since 1.1.12 | ||
*/ | ||
public $generatorConfig='CWsdlGenerator'; | ||
/** | ||
* @var boolean whether to check requests for XXE/XEE attacks | ||
* @since 1.1.14 | ||
*/ | ||
public $filterRequests=true; | ||
|
||
private $_method; | ||
|
||
|
@@ -153,7 +158,49 @@ public function generateWsdl() | |
$cache->set($key,$wsdl,$this->wsdlCacheDuration); | ||
return $wsdl; | ||
} | ||
|
||
/** | ||
* Prepares request XML and in case of string serialized XML parses and seeks for DOCTYPE | ||
* tags which may contain danger XML entities definition. | ||
* Code adapted from Zend_Soap_Server by Zend | ||
* @param mixed $request request data | ||
* @return string request XML as string | ||
*/ | ||
protected function filterRequest($request) | ||
{ | ||
//when request is already parsed - there is nothing to do, we only need to save it as string | ||
if($request instanceof DOMDocument) | ||
$xml = $request->saveXML(); | ||
elseif($request instanceof DOMNode) | ||
$xml = $request->ownerDocument->saveXML(); | ||
elseif($request instanceof SimpleXMLElement) | ||
$xml = $request->asXML(); | ||
elseif(is_object($request) || is_string($request)) | ||
{ | ||
//when request is passed as string or unknown object class - we check it for DOCTYPE tags | ||
if(is_object($request)) | ||
$xml = $request->__toString(); | ||
else | ||
$xml = $request; | ||
|
||
//disabling entity loader is crucial to prevent XXE/XEE checks or we will fall into same trap | ||
$oldValue=libxml_disable_entity_loader(true); | ||
$dom = new DOMDocument(); | ||
if(strlen($xml)==0 || !$dom->loadXML($xml)) | ||
throw new CException('Invalid XML: Parse error'); | ||
|
||
foreach($dom->childNodes as $child) | ||
{ | ||
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) | ||
throw new CException('Invalid XML: Detected use of illegal DOCTYPE'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the following code would be better: throw new CException(Yii::t('yii','Invalid XML: Detected use of illegal DOCTYPE.')); // note trailing dot Yii's common approach is to localize exception messages too. |
||
} | ||
libxml_disable_entity_loader($oldValue); | ||
} | ||
else | ||
throw new CException('Unsupported parameter type.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throw new CException(Yii::t('yii','Unsupported parameter type.')); |
||
return $xml; | ||
} | ||
|
||
/** | ||
* Handles the web service request. | ||
*/ | ||
|
@@ -178,16 +225,20 @@ public function run() | |
else | ||
$server->setClass('CSoapObjectWrapper',$provider); | ||
|
||
$request=Yii:app()->request->rawBody; | ||
if($this->filterRequests) | ||
$request=$this->filterRequest($request); | ||
|
||
if($provider instanceof IWebServiceProvider) | ||
{ | ||
if($provider->beforeWebMethod($this)) | ||
{ | ||
$server->handle(); | ||
$server->handle($request); | ||
$provider->afterWebMethod($this); | ||
} | ||
} | ||
else | ||
$server->handle(); | ||
$server->handle($request); | ||
} | ||
catch(Exception $e) | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$xml = $request->saveXML();
→$xml=$request->saveXML();
. Please fix all the other CS and consistency issues in your patch. We're trying to stick to agreements as strictly as possible, isn't it? :-)