Skip to content

Commit

Permalink
improve config type checks (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
yahesh authored Oct 31, 2024
1 parent 3549be8 commit 47fd1da
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
* [add skip_files tests (#53)](https://github.com/nextcloud/encryption-recovery-tools/pull/53)
* [update CHANGELOG.md (#54)](https://github.com/nextcloud/encryption-recovery-tools/pull/54)
* [increase decryption block size (#55)](https://github.com/nextcloud/encryption-recovery-tools/pull/55)
* [improve config type checks (#58)](https://github.com/nextcloud/encryption-recovery-tools/pull/58)

## v29.0.0 (2024-05-31)

* [add Nextcloud29 test (#50)](https://github.com/nextcloud/encryption-recovery-tools/pull/50)
* [Nextcloud 29 release (#49)](https://github.com/nextcloud/encryption-recovery-tools/pull/49)
* [improve startup sequence (#48)](https://github.com/nextcloud/encryption-recovery-tools/pull/48)
* [fix another typo (#47)](https://github.com/nextcloud/encryption-recovery-tools/pull/47)
* [improve description of how to configure the recover.php scripts (#41)](https://github.com/nextcloud/encryption-recovery-tools/pull/41)
* [fix typo (#43)](https://github.com/nextcloud/encryption-recovery-tools/pull/43)
* [be more verbose on startup (#44)](https://github.com/nextcloud/encryption-recovery-tools/pull/44)
* [document debug mode (#45)](https://github.com/nextcloud/encryption-recovery-tools/pull/45)
* [align CHANGELOG.md with the release notes (#46)](https://github.com/nextcloud/encryption-recovery-tools/pull/46)
* [fix another typo (#47)](https://github.com/nextcloud/encryption-recovery-tools/pull/47)
* [improve startup sequence (#48)](https://github.com/nextcloud/encryption-recovery-tools/pull/48)
* [Nextcloud 29 release (#49)](https://github.com/nextcloud/encryption-recovery-tools/pull/49)
* [add Nextcloud29 test (#50)](https://github.com/nextcloud/encryption-recovery-tools/pull/50)

## v28.0.0 (2024-01-19)

Expand Down
23 changes: 22 additions & 1 deletion end-to-end-encryption/recover.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ function config($key, $value) {
if (false !== getenv($key)) {
// handle specific environment variables differently
switch ($key) {
// handle as integers
case "BLOCKSIZE":
case "TAGSIZE":
case "VERSION_1":
$tmp = filter_var(getenv($key), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
if ((null !== $tmp) && (0 < $tmp)) {
$value = $tmp;
}
break;

// handle as arrays
case "EXTERNAL_STORAGES":
$value = [];
Expand All @@ -265,7 +275,10 @@ function config($key, $value) {
// handle as booleans
case "DEBUG_MODE":
case "DEBUG_MODE_VERBOSE":
$value = filter_var(getenv($key), FILTER_VALIDATE_BOOLEAN);
$tmp = filter_var(getenv($key), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if (null !== $tmp) {
$value = $tmp;
}
break;

// handle user mnemonics specifically
Expand All @@ -285,6 +298,14 @@ function config($key, $value) {
}
break;

// handle as float
case "VERSION_12":
$tmp = filter_var(getenv($key), FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
if ((null !== $tmp) && (0 < $tmp)) {
$value = $tmp;
}
break;

default:
$value = getenv($key);
}
Expand Down
17 changes: 14 additions & 3 deletions server-side-encryption/recover.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ function config($key, $value) {
if (false !== getenv($key)) {
// handle specific environment variables differently
switch ($key) {
// handle as integers
case "BLOCKSIZE":
$tmp = filter_var(getenv($key), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
if ((null !== $tmp) && (0 < $tmp)) {
$value = $tmp;
}
break;

// handle as associative array of integers
case "CIPHER_SUPPORT":
$value = [];
Expand All @@ -331,8 +339,8 @@ function config($key, $value) {
if (false !== strpos($entry, "=")) {
$left = substr($entry, 0, strpos($entry, "="));
$right = substr($entry, strpos($entry, "=")+1);
$right = filter_var($right, FILTER_VALIDATE_INT, ["flags" => FILTER_FLAG_ALLOW_OCTAL | FILTER_FLAG_ALLOW_HEX]);
if (false !== $right) {
$right = filter_var($right, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_OCTAL | FILTER_FLAG_ALLOW_HEX | FILTER_NULL_ON_FAILURE);
if ((null !== $right) && (0 < $right)) {
$value[$left] = $right;
}
}
Expand All @@ -343,7 +351,10 @@ function config($key, $value) {
case "DEBUG_MODE":
case "DEBUG_MODE_VERBOSE":
case "SUPPORT_MISSING_HEADERS":
$value = filter_var(getenv($key), FILTER_VALIDATE_BOOLEAN);
$tmp = filter_var(getenv($key), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if (null !== $tmp) {
$value = $tmp;
}
break;

// handle as associative array of strings
Expand Down
26 changes: 26 additions & 0 deletions tests/src/end-to-end-encryption/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,30 @@ public function test_putenv() {
config("USER_MNEMONICS", []);
self::assertSame($expected, USER_MNEMONICS);
}

public function test_putenv_overwrite_float() {
define("TESTING", true);

$expected = 1.2;
putenv("VERSION_12=K");

include(__DIR__."/../../../end-to-end-encryption/recover.php");

self::assertSame($expected, VERSION_12);
}

public function test_putenv_overwrite_int() {
define("TESTING", true);

$expected1 = 8192;
putenv("BLOCKSIZE=K");

$expected2 = 16;
putenv("TAGSIZE=K");

include(__DIR__."/../../../end-to-end-encryption/recover.php");

self::assertSame($expected1, BLOCKSIZE);
self::assertSame($expected2, TAGSIZE);
}
}
11 changes: 11 additions & 0 deletions tests/src/server-side-encryption/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ public function test_putenv_overwrite_hex() {
self::assertSame($expected, CIPHER_SUPPORT);
}

public function test_putenv_overwrite_int() {
define("TESTING", true);

$expected = 8192;
putenv("BLOCKSIZE=K");

include(__DIR__."/../../../server-side-encryption/recover.php");

self::assertSame($expected, BLOCKSIZE);
}

public function test_putenv_overwrite_oct() {
define("TESTING", true);

Expand Down

0 comments on commit 47fd1da

Please sign in to comment.