Skip to content

Commit

Permalink
Do not use unserialize()
Browse files Browse the repository at this point in the history
  • Loading branch information
arrilot committed Jul 31, 2016
1 parent 596cad8 commit 5cddda5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Controllers/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function showWidget(Request $request)

$factory = app()->make('arrilot.widget');
$widgetName = $request->input('name', '');
$widgetParams = unserialize($request->input('params', ''));
$widgetParams = $factory->decryptWidgetParams($request->input('params', ''));

return call_user_func_array([$factory, $widgetName], $widgetParams);
}
Expand Down
24 changes: 24 additions & 0 deletions src/Factories/AbstractWidgetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,28 @@ protected function wrapContentInContainer($content)

return '<'.$container['element'].' id="'.$this->javascriptFactory->getContainerId().'" '.$container['attributes'].'>'.$content.'</'.$container['element'].'>';
}

/**
* Encrypt widget params to be transported via HTTP.
*
* @param array $params
* @return string
*/
public function encryptWidgetParams($params)
{
return $this->app->make('encrypter')->encrypt(json_encode($params));
}

/**
* Decrypt widget params that were transported via HTTP.
*
* @param string $params
* @return array
*/
public function decryptWidgetParams($params)
{
$params = json_decode($this->app->make('encrypter')->decrypt($params), true);

return $params ? $params : [];
}
}
2 changes: 1 addition & 1 deletion src/Factories/JavascriptFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function constructAjaxCall()
$queryParams = [
'id' => WidgetId::get(),
'name' => $this->widgetFactory->widgetName,
'params' => serialize($this->widgetFactory->widgetFullParams),
'params' => $this->widgetFactory->encryptWidgetParams($this->widgetFactory->widgetFullParams),
];

$url = $this->ajaxLink.'?'.http_build_query($queryParams);
Expand Down
6 changes: 5 additions & 1 deletion tests/Support/TestApplicationWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TestApplicationWrapper implements ApplicationWrapperContract
*
* @param $key
* @param $minutes
* @param callable $callback
* @param Closure $callback
*
* @return mixed
*/
Expand Down Expand Up @@ -91,6 +91,10 @@ public function make($abstract, array $parameters = [])
if ($abstract == 'arrilot.async-widget') {
return new AsyncWidgetFactory($this);
}

if ($abstract == 'encrypter') {
return new TestEncrypter();
}

throw new InvalidArgumentException("Binding {$abstract} cannot be resolved while testing");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function ajaxUrl($widgetName, $widgetParams = [], $id = 1)
return '/arrilot/load-widget?'.http_build_query([
'id' => $id,
'name' => $widgetName,
'params' => serialize($widgetParams),
'params' => json_encode($widgetParams),
]);
}
}
11 changes: 11 additions & 0 deletions tests/Support/TestEncrypter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Arrilot\Widgets\Test\Support;

class TestEncrypter
{
public function encrypt($value)
{
return $value;
}
}

0 comments on commit 5cddda5

Please sign in to comment.