Skip to content

Commit 2da835b

Browse files
committed
Crypto params validation
1 parent 7f84113 commit 2da835b

2 files changed

Lines changed: 75 additions & 23 deletions

File tree

lib/Controller/SettingsController.php

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,16 @@ public function saveProperties()
198198
];
199199
}
200200

201+
if (!$this->validateCryptoParams()) {
202+
return [
203+
"status" => "error", "data" => [
204+
"message" => $this->localization->t(
205+
"Hash algorithm parameter is out of range."
206+
)
207+
]
208+
];
209+
}
210+
201211
foreach ($properties as $key => $value) {
202212
$reqValue = $this->request->getParam(str_replace(".", "-", $key));
203213
$appValue = $this->properties[$key];
@@ -213,6 +223,9 @@ public function saveProperties()
213223
"Property '$key' has been set to: " . $value,
214224
["app" => $this->appName]
215225
);
226+
} elseif (!is_bool($appValue) && !isset($reqValue)) {
227+
unset($this->properties[$key]);
228+
216229
}
217230
}
218231

@@ -230,6 +243,48 @@ public function saveProperties()
230243
];
231244
}
232245

246+
/**
247+
* Validate request crypto params.
248+
*
249+
* @return bool TRUE if crypto params are correct FALSE otherwise.
250+
*/
251+
private function validateCryptoParams()
252+
{
253+
$cryptoClass = $this->request->getParam("opt-crypto_class");
254+
$configuration = $this->cryptoClassConfiguration($cryptoClass);
255+
256+
for ($i = 0; $i < count($configuration); ++$i) {
257+
$reqParam = $this->request->getParam(
258+
"opt-crypto_param_" . $i, null
259+
);
260+
$cryptoParam = $configuration[$i];
261+
262+
if (is_null($reqParam) || $reqParam < $cryptoParam->min
263+
|| $reqParam > $cryptoParam->max
264+
) {
265+
return false;
266+
}
267+
}
268+
269+
return true;
270+
}
271+
272+
/**
273+
* Get a crypto class configuration from request.
274+
*
275+
* @param $cryptoClass string Crypto class name.
276+
*
277+
* @return array A crypto class configuration.
278+
*/
279+
private function cryptoClassConfiguration($cryptoClass)
280+
{
281+
/**
282+
* @var $passwordAlgorithm IPasswordAlgorithm
283+
*/
284+
$passwordAlgorithm = new $cryptoClass($this->localization);
285+
return $passwordAlgorithm->configuration();
286+
}
287+
233288
/**
234289
* Clear the application cache memory.
235290
*
@@ -385,12 +440,8 @@ public function cryptoParams()
385440
"Entering cryptoParams()", ["app" => $this->appName]
386441
);
387442

388-
/**
389-
* @var $passwordAlgorithm IPasswordAlgorithm
390-
*/
391443
$cryptoClass = $this->request->getParam("cryptoClass");
392-
$passwordAlgorithm = new $cryptoClass($this->localization);
393-
$configuration = $passwordAlgorithm->configuration();
444+
$configuration = $this->cryptoClassConfiguration($cryptoClass);
394445

395446
if ($cryptoClass === $this->properties[Opt::CRYPTO_CLASS]) {
396447
foreach ($configuration as $key => $value) {

lib/Properties.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,6 @@ private function loadProperties()
119119
);
120120
}
121121

122-
/**
123-
* Is given parameter a boolean parameter.
124-
*
125-
* @param $param string Parameter name.
126-
*
127-
* @return bool Is a boolean parameter.
128-
*/
129-
private function isBooleanParam($param)
130-
{
131-
return in_array(
132-
$param, [
133-
Opt::APPEND_SALT, Opt::CASE_INSENSITIVE_USERNAME,
134-
Opt::NAME_CHANGE, Opt::PASSWORD_CHANGE, Opt::PREPEND_SALT,
135-
Opt::REVERSE_ACTIVE, Opt::USE_CACHE
136-
]
137-
);
138-
}
139-
140122
/**
141123
* Return an array with all supported parameters.
142124
*
@@ -162,6 +144,24 @@ private function getParameterArray()
162144
return $params;
163145
}
164146

147+
/**
148+
* Is given parameter a boolean parameter.
149+
*
150+
* @param $param string Parameter name.
151+
*
152+
* @return bool Is a boolean parameter.
153+
*/
154+
private function isBooleanParam($param)
155+
{
156+
return in_array(
157+
$param, [
158+
Opt::APPEND_SALT, Opt::CASE_INSENSITIVE_USERNAME,
159+
Opt::NAME_CHANGE, Opt::PASSWORD_CHANGE, Opt::PREPEND_SALT,
160+
Opt::REVERSE_ACTIVE, Opt::USE_CACHE
161+
]
162+
);
163+
}
164+
165165
/**
166166
* Store properties in the cache memory.
167167
*/
@@ -229,6 +229,7 @@ public function offsetSet($offset, $value)
229229
*/
230230
public function offsetUnset($offset)
231231
{
232+
$this->config->deleteAppValue($this->appName, $offset);
232233
unset($this->data[$offset]);
233234
}
234235
}

0 commit comments

Comments
 (0)