Files
FOSSBilling/tests-legacy/library/Box/Box_CryptTest.php
T
0d81f5d450 Enhance security and validation for sessions and file uploads (#3315)
* Sanitise email HTML; add session decoder

* Use signed session restore token for redirects

* Restrict invoice and service access to client

* Perform CSRF check regardless of session

* Validate uploaded files by extension/mime

* Use 32-byte random for reset hashes

* Use PBKDF2 SHA-256 for key derivation

* Add versioned encryption and migrate data

* Refactor Servicecustom API and update download validation

* Prefer token login when credentials present

* Update cspell word list with filetype terms

* Ignore token auth for guest requests

* Validate decrypted text and add fixed IV in test

* Bump `last_patch` setting to 50

* Update src/modules/Api/Controller/Client.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix CSRF/token auth interaction: token auth always bypasses CSRF regardless of session (#3318)

* Replace hand-rolled PHP session parser with unserialize() (#3317)

* Initial plan

* Replace buggy phpSessionDecode() with unserialize()

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
Co-authored-by: Adam Daley <admdly@users.noreply.github.com>

* Fix migrateEncryptedColumn failing on tables without updated_at (#3316)

* Initial plan

* Fix migrateEncryptedColumn to skip updated_at when column doesn't exist

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
Co-authored-by: Adam Daley <admdly@users.noreply.github.com>

* Guard `phpSessionDecode()` return type in `deleteSessionIfMatching` (#3319)

* Initial plan

* Guard phpSessionDecode() return type in deleteSessionIfMatching

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
2026-03-11 00:59:05 +00:00

57 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
#[Group('Core')]
final class Box_CryptTest extends PHPUnit\Framework\TestCase
{
public function testCrypt(): void
{
$key = 'le password';
$text = 'foo bar';
$crypt = new Box_Crypt();
$encoded = $crypt->encrypt($text, $key);
$decoded = $crypt->decrypt($encoded, $key);
$this->assertEquals($text, $decoded);
}
public function testDecryptLegacyCiphertext(): void
{
$key = 'le password';
$text = 'foo bar';
$crypt = new Box_Crypt();
$this->assertSame($text, $crypt->decrypt($this->encryptWithLegacyKey($text, $key), $key));
}
public function testDecryptCurrentUnversionedCiphertext(): void
{
$key = 'le password';
$text = 'foo bar';
$crypt = new Box_Crypt();
$this->assertSame($text, $crypt->decrypt($this->encryptWithCurrentKey($text, $key), $key));
}
private function encryptWithLegacyKey(string $text, string $pass): string
{
return $this->encryptWithKey($text, pack('H*', hash('md5', $pass)));
}
private function encryptWithCurrentKey(string $text, string $pass): string
{
return $this->encryptWithKey($text, hash_pbkdf2('sha256', $pass, 'fossbilling_salt', 100000, 32, true));
}
private function encryptWithKey(string $text, string $key): string
{
$iv = str_repeat("\x01", openssl_cipher_iv_length(Box_Crypt::METHOD));
$ciphertext = openssl_encrypt($text, Box_Crypt::METHOD, $key, OPENSSL_RAW_DATA, $iv);
return base64_encode($iv . $ciphertext);
}
}