Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests explicitly convering confused deputy attacks #102

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/EncryptedMultiRowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,37 @@ public function engineProvider()
];
}

/**
* @dataProvider engineProvider
*/
public function testFieldsAreNotSwappable(CipherSweet $engine): void
{
$eR = new EncryptedMultiRows($engine);
$eR
->addOptionalTextField('foo', 'field1')
->addOptionalTextField('foo', 'field2');

$plain = ['foo' => ['field1' => 'example', 'field2' => 'message']];
$encrypted = $eR->encryptManyRows($plain);
$swapped = [];
[$swapped['foo']['field1'], $swapped['foo']['field2']] = [$encrypted['foo']['field2'], $encrypted['foo']['field1']];
// Sanity check: Did we actually swap them?
$this->assertSame($swapped['foo']['field2'], $encrypted['foo']['field1']);
$this->assertSame($swapped['foo']['field1'], $encrypted['foo']['field2']);

// Is decryption successful still?
$decrypted = $eR->decryptManyRows($encrypted);
$this->assertSame($plain['foo']['field1'], $decrypted['foo']['field1']);
$this->assertSame($plain['foo']['field2'], $decrypted['foo']['field2']);

// Okay, let's decryptRow() on the swapped values. This must throw.
try {
$eR->decryptManyRows($swapped);
$this->fail('Expected decryptRow() to fail.');
} catch (CipherSweetException|SodiumException) {
}
}

/**
* @dataProvider engineProvider
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/EncryptedRowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,37 @@ public function testJsonField(CipherSweet $engine)
$this->assertSame($plaintext, $eR->decryptRow($some));
}

/**
* @dataProvider engineProvider
*/
public function testFieldsAreNotSwappable(CipherSweet $engine): void
{
$eR = new EncryptedRow($engine, 'foo');
$eR
->addOptionalTextField('field1')
->addOptionalTextField('field2');

$plain = ['field1' => 'example', 'field2' => 'message'];
$encrypted = $eR->encryptRow($plain);
$swapped = [];
[$swapped['field1'], $swapped['field2']] = [$encrypted['field2'], $encrypted['field1']];
// Sanity check: Did we actually swap them?
$this->assertSame($swapped['field2'], $encrypted['field1']);
$this->assertSame($swapped['field1'], $encrypted['field2']);

// Is decryption successful still?
$decrypted = $eR->decryptRow($encrypted);
$this->assertSame($plain['field1'], $decrypted['field1']);
$this->assertSame($plain['field2'], $decrypted['field2']);

// Okay, let's decryptRow() on the swapped values. This must throw.
try {
$eR->decryptRow($swapped);
$this->fail('Expected decryptRow() to fail.');
} catch (CipherSweetException|SodiumException) {
}
}

/**
* @dataProvider engineProvider
*/
Expand Down
Loading