From 1f0b0812ff0a0549235eeced8a112ea28d5c78b6 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 08:44:52 +0000 Subject: [PATCH] toto.php: XML-escape the values put into ONVIF requests Every request (19 builders) put the user name, profile / preset / node tokens, preset name, stream and protocol into the SOAP XML unescaped. A user name such as "a&b" or a token containing "&" or "<" made the request malformed. The camera rejected it, and the UI reported "Provided Credentials are Wrong!". - _xesc() / _xescAll(): htmlspecialchars(ENT_XML1 | ENT_QUOTES, UTF-8) on string values. Numbers and booleans are replaced as before. - The password itself is not affected: only its digest is sent. Tested against a fake ONVIF camera with user "us&er<1>", password 'p&ss"word' and profile token "main&0", adding the camera on a custom port. Before, the camera got malformed XML after GetSystemDateAndTime. Now the camera, its login and its stream are stored. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/php/onvif/toto.php | 90 ++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 38 deletions(-) diff --git a/lib/php/onvif/toto.php b/lib/php/onvif/toto.php index ad7b9b6..1ccb217 100644 --- a/lib/php/onvif/toto.php +++ b/lib/php/onvif/toto.php @@ -173,10 +173,10 @@ class Ponvif { "%%PASSWORD%%", "%%NONCE%%", "%%CREATED%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], - $REQ['TIMESTAMP']), + $REQ['TIMESTAMP'])), $post_string); if ($this->isFault($response=$this->_send_request($this->mediauri,$post_string))) { if ($this->intransingent) throw new Exception('GetCapabilities: Communication error'); @@ -192,10 +192,10 @@ class Ponvif { "%%PASSWORD%%", "%%NONCE%%", "%%CREATED%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], - $REQ['TIMESTAMP']), + $REQ['TIMESTAMP'])), $post_string); //echo "POST STRING:" . $post_string . "\n\n"; if ($this->isFault($response=$this->_send_request($this->mediauri,$post_string))) { @@ -213,10 +213,10 @@ class Ponvif { "%%PASSWORD%%", "%%NONCE%%", "%%CREATED%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], - $REQ['TIMESTAMP']), + $REQ['TIMESTAMP'])), $post_string); if ($this->isFault($response=$this->_send_request($this->mediauri,$post_string))) { if ($this->intransingent) throw new Exception('GetProfiles: Communication error'); @@ -232,10 +232,10 @@ class Ponvif { "%%PASSWORD%%", "%%NONCE%%", "%%CREATED%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], - $REQ['TIMESTAMP']), + $REQ['TIMESTAMP'])), $post_string); if ($this->isFault($this->_send_request($this->mediauri,$post_string))) { if ($this->intransingent) throw new Exception('GetServices: Communication error'); @@ -249,10 +249,10 @@ class Ponvif { "%%PASSWORD%%", "%%NONCE%%", "%%CREATED%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], - $REQ['TIMESTAMP']), + $REQ['TIMESTAMP'])), $post_string); if ($this->isFault($response=$this->_send_request($this->mediauri,$post_string))) { if ($this->intransingent) throw new Exception('GetDeviceInformation: Communication error'); @@ -272,13 +272,13 @@ class Ponvif { "%%PROFILETOKEN%%", "%%STREAM%%", "%%PROTOCOL%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], $profileToken, $stream, - $protocol), + $protocol)), $post_string ); if ($this->isFault($response=$this->_send_request($this->mediauri,$post_string))) { @@ -296,11 +296,11 @@ class Ponvif { "%%NONCE%%", "%%CREATED%%", "%%PROFILETOKEN%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], - $profileToken), + $profileToken)), $post_string); if ($this->isFault($response=$this->_send_request($this->mediauri,$post_string))) { if ($this->intransingent) throw new Exception('GetSnapshotUri: Communication error'); @@ -319,11 +319,11 @@ class Ponvif { "%%NONCE%%", "%%CREATED%%", "%%PROFILETOKEN%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], - $profileToken), + $profileToken)), $post_string); if ($this->isFault($response=$this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('GetPresets: Communication error'); @@ -349,11 +349,11 @@ class Ponvif { "%%NONCE%%", "%%CREATED%%", "%%NODETOKEN%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], - $ptzNodeToken), + $ptzNodeToken)), $post_string); if ($this->isFault($response=$this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('GetNode: Communication error'); @@ -375,7 +375,7 @@ class Ponvif { "%%SPEEDPANTILTX%%", "%%SPEEDPANTILTY%%", "%%SPEEDZOOMX%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], @@ -383,7 +383,7 @@ class Ponvif { $presetToken, $speed_pantilt_x, $speed_pantilt_y, - $speed_zoom_x), + $speed_zoom_x)), $post_string); if ($this->isFault($this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('GotoPreset: Communication error'); @@ -400,12 +400,12 @@ class Ponvif { "%%CREATED%%", "%%PROFILETOKEN%%", "%%PRESETTOKEN%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], $profileToken, - $presetToken), + $presetToken)), $post_string); if ($this->isFault($this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('RemovePreset: Communication error'); @@ -422,12 +422,12 @@ class Ponvif { "%%CREATED%%", "%%PROFILETOKEN%%", "%%PRESETNAME%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], $profileToken, - $presetName), + $presetName)), $post_string); if ($this->isFault($this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('SetPreset: Communication error'); @@ -447,7 +447,7 @@ class Ponvif { "%%TRANSLATIONPANTILTY%%", "%%SPEEDPANTILTX%%", "%%SPEEDPANTILTY%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], @@ -455,7 +455,7 @@ class Ponvif { $translation_pantilt_x, $translation_pantilt_y, $speed_pantilt_x, - $speed_pantilt_y), + $speed_pantilt_y)), $post_string); if ($this->isFault($this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('RelativeMove: Communication error'); @@ -473,13 +473,13 @@ class Ponvif { "%%PROFILETOKEN%%", "%%SPEEDZOOM%%", "%%ZOOM%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], $profileToken, $speedZoom, - $zoom), + $zoom)), $post_string); if ($this->isFault($this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('RelativeMoveZoom: Communication error'); @@ -494,11 +494,11 @@ class Ponvif { "%%NONCE%%", "%%CREATED%%", "%%PROFILETOKEN%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], - $profileToken), + $profileToken)), $post_string); if ($this->isFault($response = $this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('ptz Get Status: Communication error'); @@ -524,7 +524,7 @@ class Ponvif { "%%SPEEDPANTILTX%%", "%%SPEEDPANTILTY%%", "%%SPEEDZOOMX%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], @@ -534,7 +534,7 @@ class Ponvif { $zoom, $PSpeed, $TSpeed, - $ZSpeed), + $ZSpeed)), $post_string); if ($this->isFault($this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('AbsoluteMove: Communication error'); @@ -552,13 +552,13 @@ class Ponvif { "%%PROFILETOKEN%%", "%%VELOCITYPANTILTX%%", "%%VELOCITYPANTILTY%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], $profileToken, $velocity_pantilt_x, - $velocity_pantilt_y), + $velocity_pantilt_y)), $post_string); if ( $this->isFault($this->_send_request($this->ptzuri,$post_string))) { if( $this->intransingent ) throw new Exception('ContinuousMove: Communication error'); @@ -575,12 +575,12 @@ class Ponvif { "%%CREATED%%", "%%PROFILETOKEN%%", "%%ZOOM%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], $profileToken, - $zoom), + $zoom)), $post_string); if ($this->isFault($this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('ContinuousMoveZoom: Communication error'); @@ -598,13 +598,13 @@ class Ponvif { "%%PROFILETOKEN%%", "%%PANTILT%%", "%%ZOOM%%"), - array($REQ['USERNAME'], + $this->_xescAll(array($REQ['USERNAME'], $REQ['PASSDIGEST'], $REQ['NONCE'], $REQ['TIMESTAMP'], $profileToken, $pantilt, - $zoom), + $zoom)), $post_string); if ($this->isFault($this->_send_request($this->ptzuri,$post_string))) { if ($this->intransingent) throw new Exception('Stop: Communication error'); @@ -707,6 +707,20 @@ class Ponvif { return $data; } + /* + Values put into the XML of a request (user name, profile / preset / node tokens, preset name ...) + must be XML-escaped: a user name like "a&b" or a token with "&" or "<" made the request + malformed - the camera rejected it and the UI reported "Credentials are Wrong!". + Only strings are escaped: numbers and booleans are replaced as before. + */ + protected function _xesc($v) { + return is_string($v) ? htmlspecialchars($v, ENT_XML1 | ENT_QUOTES, 'UTF-8') : $v; + } + + protected function _xescAll($values) { + return array_map(array($this, '_xesc'), $values); + } + protected function _passwordDigest( $username, $password, $timestamp = "default", $nonce = "default" ) { if ($timestamp=='default') $timestamp=date('Y-m-d\TH:i:s.000\Z'); if ($nonce=='default') $nonce=mt_rand();