Skip to content

Commit 5cddda5

Browse files
committed
Do not use unserialize()
1 parent 596cad8 commit 5cddda5

File tree

6 files changed

+43
-4
lines changed

6 files changed

+43
-4
lines changed

src/Controllers/WidgetController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function showWidget(Request $request)
2222

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

2727
return call_user_func_array([$factory, $widgetName], $widgetParams);
2828
}

src/Factories/AbstractWidgetFactory.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,28 @@ protected function wrapContentInContainer($content)
155155

156156
return '<'.$container['element'].' id="'.$this->javascriptFactory->getContainerId().'" '.$container['attributes'].'>'.$content.'</'.$container['element'].'>';
157157
}
158+
159+
/**
160+
* Encrypt widget params to be transported via HTTP.
161+
*
162+
* @param array $params
163+
* @return string
164+
*/
165+
public function encryptWidgetParams($params)
166+
{
167+
return $this->app->make('encrypter')->encrypt(json_encode($params));
168+
}
169+
170+
/**
171+
* Decrypt widget params that were transported via HTTP.
172+
*
173+
* @param string $params
174+
* @return array
175+
*/
176+
public function decryptWidgetParams($params)
177+
{
178+
$params = json_decode($this->app->make('encrypter')->decrypt($params), true);
179+
180+
return $params ? $params : [];
181+
}
158182
}

src/Factories/JavascriptFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function constructAjaxCall()
9090
$queryParams = [
9191
'id' => WidgetId::get(),
9292
'name' => $this->widgetFactory->widgetName,
93-
'params' => serialize($this->widgetFactory->widgetFullParams),
93+
'params' => $this->widgetFactory->encryptWidgetParams($this->widgetFactory->widgetFullParams),
9494
];
9595

9696
$url = $this->ajaxLink.'?'.http_build_query($queryParams);

tests/Support/TestApplicationWrapper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TestApplicationWrapper implements ApplicationWrapperContract
2525
*
2626
* @param $key
2727
* @param $minutes
28-
* @param callable $callback
28+
* @param Closure $callback
2929
*
3030
* @return mixed
3131
*/
@@ -91,6 +91,10 @@ public function make($abstract, array $parameters = [])
9191
if ($abstract == 'arrilot.async-widget') {
9292
return new AsyncWidgetFactory($this);
9393
}
94+
95+
if ($abstract == 'encrypter') {
96+
return new TestEncrypter();
97+
}
9498

9599
throw new InvalidArgumentException("Binding {$abstract} cannot be resolved while testing");
96100
}

tests/Support/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function ajaxUrl($widgetName, $widgetParams = [], $id = 1)
1717
return '/arrilot/load-widget?'.http_build_query([
1818
'id' => $id,
1919
'name' => $widgetName,
20-
'params' => serialize($widgetParams),
20+
'params' => json_encode($widgetParams),
2121
]);
2222
}
2323
}

tests/Support/TestEncrypter.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Arrilot\Widgets\Test\Support;
4+
5+
class TestEncrypter
6+
{
7+
public function encrypt($value)
8+
{
9+
return $value;
10+
}
11+
}

0 commit comments

Comments
 (0)