Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit a6ce604

Browse files
BaranekDvyskocilpavel
authored andcommitted
fix: Security improvements in script calls
1 parent 59074f7 commit a6ce604

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
#### Changed
66
- Improve WAYF searching by localized name and domain
77
- Implemented filter EnsureVoMember
8+
- Security improvements in script calls
89

910
#### Fixed
1011
- Detailed endpoint format when spaced in EndpointMapToArray

lib/Auth/Process/UpdateUserExtSource.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use SimpleSAML\Error\Exception;
1212
use SimpleSAML\Logger;
1313
use SimpleSAML\Module;
14+
use SimpleSAML\Module\perun\ChallengeManager;
1415
use SimpleSAML\Module\perun\UpdateUESThread;
1516

1617
/**
@@ -26,6 +27,7 @@ class UpdateUserExtSource extends ProcessingFilter
2627
private $attrMap;
2728
private $attrsToConversion;
2829
private $pathToKey;
30+
private $signatureAlg;
2931

3032
const SCRIPT_NAME = 'updateUes';
3133

@@ -53,6 +55,12 @@ public function __construct($config, $reserved)
5355
$this->attrsToConversion = [];
5456
}
5557

58+
if (isset($config['signatureAlg'])) {
59+
$this->signatureAlg = (array)$config['signatureAlg'];
60+
} else {
61+
$this->signatureAlg = 'RS512';
62+
}
63+
5664
$this->attrMap = (array)$config['attrMap'];
5765
$this->pathToKey = $config['pathToKey'];
5866
}
@@ -81,7 +89,11 @@ public function process(&$request)
8189
}
8290

8391
$jwk = JWKFactory::createFromKeyFile($this->pathToKey);
84-
$algorithmManager = new AlgorithmManager([new RS512()]);
92+
$algorithmManager = new AlgorithmManager(
93+
[
94+
ChallengeManager::getAlgorithm('Signature\\Algorithm', $this->signatureAlg)
95+
]
96+
);
8597
$jwsBuilder = new JWSBuilder($algorithmManager);
8698

8799
$data = [
@@ -103,7 +115,7 @@ public function process(&$request)
103115
$jws = $jwsBuilder
104116
->create()
105117
->withPayload($payload)
106-
->addSignature($jwk, ['alg' => 'RS512'])
118+
->addSignature($jwk, ['alg' => $this->signatureAlg])
107119
->build();
108120

109121
$serializer = new CompactSerializer();

lib/ChallengeManager.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,13 @@ public function deleteChallengeFromDb($id): bool
7676

7777
return true;
7878
}
79+
80+
public static function getAlgorithm($path, $className)
81+
{
82+
$classPath = sprintf('Jose\\Component\\%s\\%s', $path, $className);
83+
if (! class_exists($classPath)) {
84+
throw new \Exception('Invalid algorithm specified: ' . $classPath);
85+
}
86+
return new $classPath();
87+
}
7988
}

www/getChallenge.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use SimpleSAML\Configuration;
34
use SimpleSAML\Logger;
45
use SimpleSAML\Module\perun\ChallengeManager;
56

@@ -12,7 +13,7 @@
1213
exit;
1314
}
1415

15-
if (empty($body['id'] || strlen($body['id']) > 30 || !ctype_print($body['id']))) {
16+
if (empty($body['id']) || strlen($body['id']) > 30 || !ctype_print($body['id'])) {
1617
Logger::error('Perun.getChallenge: Invalid id');
1718
http_response_code(400);
1819
exit;
@@ -24,14 +25,19 @@
2425
exit;
2526
}
2627

28+
const CONFIG_FILE_NAME = 'challenges_config.php';
29+
const HASH_ALG = 'hashAlg';
30+
const CHALLENGE_LENGTH = 'challengeLength';
31+
2732
$id = $body['id'];
2833
$scriptName = $body['scriptName'];
2934

30-
const RANDOM_BYTES_LENGTH = 32;
31-
const TABLE_NAME = 'scriptChallenges';
35+
$config = Configuration::getConfig(CONFIG_FILE_NAME);
36+
$hashAlg = $config->getString(HASH_ALG, 'sha512');
37+
$challengeLength = $config->getInteger(CHALLENGE_LENGTH, 32);
3238

3339
try {
34-
$challenge = hash('sha256', random_bytes(RANDOM_BYTES_LENGTH));
40+
$challenge = hash($hashAlg, random_bytes($challengeLength));
3541
} catch (Exception $ex) {
3642
Logger::error('Perun.getChallenge: Error while generating a challenge');
3743
http_response_code(500);

www/updateUes.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,29 @@
3838
$id = null;
3939

4040
const UES_ATTR_NMS = 'urn:perun:ues:attribute-def:def';
41-
const CONFIG_FILE_NAME = 'keys.php';
41+
const CONFIG_FILE_NAME = 'challenges_config.php';
4242

4343
try {
4444
$config = Configuration::getConfig(CONFIG_FILE_NAME);
4545
$keyPub = $config->getString('updateUes');
46+
$signatureAlg = $config->getString('signatureAlg', 'RS512');
4647

47-
$algorithmManager = new AlgorithmManager([new RS512()]);
48+
$algorithmManager = new AlgorithmManager(
49+
[
50+
ChallengeManager::getAlgorithm('Signature\\Algorithm', $signatureAlg)
51+
]
52+
);
4853
$jwsVerifier = new JWSVerifier($algorithmManager);
4954
$jwk = JWKFactory::createFromKeyFile($keyPub);
5055

5156
$serializerManager = new JWSSerializerManager([new CompactSerializer()]);
5257
$jws = $serializerManager->unserialize($token);
5358

54-
$headerCheckerManager = new HeaderCheckerManager([new AlgorithmChecker(['RS512'])], [new JWSTokenSupport()]);
59+
$headerCheckerManager = new HeaderCheckerManager(
60+
[new AlgorithmChecker([$signatureAlg])],
61+
[new JWSTokenSupport()]
62+
);
63+
5564
$headerCheckerManager->check($jws, 0);
5665

5766
$isVerified = $jwsVerifier->verifyWithKey($jws, $jwk, 0);

0 commit comments

Comments
 (0)