Skip to content

[PM-21075] Add crypto abstraction layer for database seeder #6114

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rkac-bw
Copy link
Contributor

@rkac-bw rkac-bw commented Jul 23, 2025

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-21075

📔 Objective

This PR adds a crypto abstraction layer to the database seeder, enabling Bitwarden encryption
for seeded data and preparing for future Rust SDK integration.

Key improvements:

  • Seeded users can now authenticate successfully
  • Added vault item seeding capabilities (passwords, notes, cards, identities)
  • Possible future Rust SDK integration with feature flag support
  • Currently supports SQL Server and PostgreSQL (MySQL and SQLite to follow)

📝 Implementation Details

  • Introduced ISeederCryptoService interface to abstract crypto operations
  • Implemented C# version with proper Bitwarden encryption (PBKDF2, HKDF, AES-256-CBC)
  • Added RustSeederCryptoService stub showing one possible integration approach
  • Updated all recipes to use dependency injection
  • Added feature flag (Seeder:UseRustSdk) for future implementation switching
  • Commented out RustSdk file copy operations to prevent build errors (files don't exist yet)

📸 Screenshots

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Hinton and others added 2 commits July 1, 2025 17:21
- Introduce ISeederCryptoService interface for crypto operations
- Implement C# crypto service with proper Bitwarden encryption
- Add RustSeederCryptoService stub for future SDK integration
- Fix authentication by implementing correct double-hashing
- Add vault item seeding capabilities
- Update all recipes to use dependency injection
- Add feature flag for implementation switching
- Comment out RustSdk file copy operations to prevent build errors
- Currently supports SQL Server and PostgreSQL
@rkac-bw rkac-bw requested review from Hinton and withinfocus July 23, 2025 10:58
Copy link
Contributor

github-actions bot commented Jul 23, 2025

Logo
Checkmarx One – Scan Summary & Details474c7c95-78e3-409a-bb72-3c4564cbd041

New Issues (17)

Checkmarx found the following issues in this Pull Request

Severity Issue Source File / Package Checkmarx Insight
MEDIUM CSRF /src/Api/Vault/Controllers/CiphersController.cs: 986
detailsMethod PutRestoreAdmin at line 986 of /src/Api/Vault/Controllers/CiphersController.cs gets a parameter from a user request from id. This paramete...
ID: unLYD72kpSb9o6bDstIUhABVKcQ%3D
Attack Vector
MEDIUM CSRF /src/Api/Vault/Controllers/CiphersController.cs: 917
detailsMethod PutDeleteAdmin at line 917 of /src/Api/Vault/Controllers/CiphersController.cs gets a parameter from a user request from id. This parameter...
ID: bH8ExpIJG0gxsKetabrjvHlpBp0%3D
Attack Vector
MEDIUM CSRF /src/Api/Auth/Controllers/AccountsController.cs: 620
detailsMethod ResendNewDeviceOtpAsync at line 620 of /src/Api/Auth/Controllers/AccountsController.cs gets a parameter from a user request from request. ...
ID: rwYEF7iClkJ473dX9hQ1VMY94BQ%3D
Attack Vector
MEDIUM CSRF /src/Api/AdminConsole/Controllers/OrganizationUsersController.cs: 557
detailsMethod PutResetPassword at line 557 of /src/Api/AdminConsole/Controllers/OrganizationUsersController.cs gets a parameter from a user request from ...
ID: 4JP4nTJlqvmazkb5hjvu59t2h0o%3D
Attack Vector
MEDIUM CSRF /src/Api/AdminConsole/Controllers/OrganizationUsersController.cs: 419
detailsMethod BulkConfirm at line 419 of /src/Api/AdminConsole/Controllers/OrganizationUsersController.cs gets a parameter from a user request from model...
ID: zr07HEdTRkag6z7pugG34fYG%2BLU%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/OrganizationWithUsersAndVaultItemsRecipe.cs: 171
detailsMethod CreateOrganizationWithUsersAndItems at line 171 of /util/Seeder/Recipes/OrganizationWithUsersAndVaultItemsRecipe.cs uses a weak method Next...
ID: Qre4znLWu735UdPAxhSCLiwWGS4%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/OrganizationWithUsersAndVaultItemsRecipe.cs: 245
detailsMethod Lambda at line 245 of /util/Seeder/Recipes/OrganizationWithUsersAndVaultItemsRecipe.cs uses a weak method Next to produce random values. T...
ID: zU1AqbYoIni%2FmhnfRAyVi7dndpQ%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/VaultItemsRecipe.cs: 104
detailsMethod Lambda at line 104 of /util/Seeder/Recipes/VaultItemsRecipe.cs uses a weak method Next to produce random values. These values are then use...
ID: 5zMzZbc9XtZBZ4fczmUKFmT%2BRl0%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/VaultItemsRecipe.cs: 66
detailsMethod Lambda at line 66 of /util/Seeder/Recipes/VaultItemsRecipe.cs uses a weak method Next to produce random values. These values are then used...
ID: SEdNh7ld%2FStPDiX4CC7YZGmjeYQ%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/VaultItemsRecipe.cs: 54
detailsMethod Lambda at line 54 of /util/Seeder/Recipes/VaultItemsRecipe.cs uses a weak method Next to produce random values. These values are then used...
ID: sfFjp2FEeRLwCL5KBhySUjrHH9w%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/VaultItemsRecipe.cs: 44
detailsMethod Lambda at line 44 of /util/Seeder/Recipes/VaultItemsRecipe.cs uses a weak method Next to produce random values. These values are then used...
ID: 0rJKBAO%2Fjuig1MXQJ4SbHAb5ZpY%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/VaultItemsRecipe.cs: 33
detailsMethod Lambda at line 33 of /util/Seeder/Recipes/VaultItemsRecipe.cs uses a weak method Next to produce random values. These values are then used...
ID: i8etzdl%2BSKY%2BQl3jKhlKPuiTVuU%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/OrganizationWithUsersAndVaultItemsRecipe.cs: 261
detailsMethod Lambda at line 261 of /util/Seeder/Recipes/OrganizationWithUsersAndVaultItemsRecipe.cs uses a weak method Next to produce random values. T...
ID: MP8YazixMDKpRM9iTobgM2ztXv0%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/UserWithVaultItemsRecipe.cs: 48
detailsMethod Lambda at line 48 of /util/Seeder/Recipes/UserWithVaultItemsRecipe.cs uses a weak method Next to produce random values. These values are t...
ID: %2FfClD3KjWZZY8utBpEi9IsprmKo%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/UserWithVaultItemsRecipe.cs: 59
detailsMethod Lambda at line 59 of /util/Seeder/Recipes/UserWithVaultItemsRecipe.cs uses a weak method Next to produce random values. These values are t...
ID: Wgc95QXSpGVsg31ojd9kA78PEB8%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/UserWithVaultItemsRecipe.cs: 69
detailsMethod Lambda at line 69 of /util/Seeder/Recipes/UserWithVaultItemsRecipe.cs uses a weak method Next to produce random values. These values are t...
ID: O4B%2FxLricPvvbj9Dl1YCyQVOTqs%3D
Attack Vector
MEDIUM Use_of_Cryptographically_Weak_PRNG /util/Seeder/Recipes/UserWithVaultItemsRecipe.cs: 81
detailsMethod Lambda at line 81 of /util/Seeder/Recipes/UserWithVaultItemsRecipe.cs uses a weak method Next to produce random values. These values are t...
ID: 5C67JXqrBTZD6UHDw%2Fe%2FqM5NTc0%3D
Attack Vector
Fixed Issues (12)

Great job! The following issues were fixed in this Pull Request

Severity Issue Source File / Package
HIGH CVE-2022-37620 Npm-html-minifier-4.0.0
MEDIUM CSRF /src/Api/AdminConsole/Controllers/OrganizationUsersController.cs: 342
MEDIUM CSRF /src/Api/Billing/Controllers/VNext/OrganizationBillingVNextController.cs: 89
MEDIUM CSRF /src/Api/Billing/Controllers/VNext/AccountBillingVNextController.cs: 57
MEDIUM CSRF /src/Api/Billing/Controllers/VNext/ProviderBillingVNextController.cs: 80
MEDIUM CSRF /src/Api/Billing/Controllers/VNext/OrganizationBillingVNextController.cs: 43
MEDIUM CSRF /src/Api/Billing/Controllers/VNext/ProviderBillingVNextController.cs: 38
MEDIUM CSRF /src/Api/AdminConsole/Controllers/OrganizationUsersController.cs: 466
MEDIUM CSRF /src/Api/Vault/Controllers/SecurityTaskController.cs: 63
MEDIUM Use_Of_Hardcoded_Password /src/Core/Constants.cs: 201
LOW CVE-2025-5889 Npm-brace-expansion-2.0.1
LOW CVE-2025-5889 Npm-brace-expansion-1.1.11

Copy link
Contributor

@withinfocus withinfocus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stopped about half-way, but there are solution-wide capabilities your AI generator wasn't aware of that replace a lot of this -- code in test and utility projects mainly to handle database configuration, data protection, and so on. This can thin out a lot and focus on the recipes utilizing the SDK. We'll need to get its invocation smoothed out too.

.gitignore Outdated
Comment on lines 228 to 229
NativeMethods.g.cs
util/RustSdk/rust/target
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ You should be able to use the same ignore you set up below I'd think.

@@ -133,6 +133,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Seeder", "util\Seeder\Seede
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbSeederUtility", "util\DbSeederUtility\DbSeederUtility.csproj", "{17A89266-260A-4A03-81AE-C0468C6EE06E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RustSdk", "util\RustSdk\RustSdk.csproj", "{D1513D90-E4F5-44A9-9121-5E46E3E4A3F7}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Seems unnecessary to have a whole separate project for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RustSdk project was part of @Hinton's SDK integration work

@@ -9,7 +9,7 @@ namespace Bit.Api.IntegrationTest.AdminConsole.Controllers;

public class OrganizationUsersControllerPerformanceTest(ITestOutputHelper testOutputHelper)
{
[Theory(Skip = "Performance test")]
[Theory()]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ Not sure why this was done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test did not work with the new injection, so skipped it for build

.idea/

# macOS
.DS_Store
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ Newlines at the end of all files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed now

services.AddSingleton<IPasswordHasher<User>, PasswordHasher<User>>();

// Register ISeederCryptoService with feature flag support
// TODO: When Rust SDK is ready, check configuration to switch implementations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ If we're gonna move forward with a PR like this, we are gonna use the SDK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, just wanted to move closer to that point with this work

/// <summary>
/// Factory for creating configured service providers for seeder operations
/// </summary>
public static class ServiceProviderFactory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ I'm guessing this is generated? We'd want to follow patterns we use in other projects to access a database and this is wildly different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was having issues with dataprotection, sort of a workaround at the moment, will look closer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably don't need data protection as it's backwards compatible.

"globalSettings": {
"databaseProvider": "postgres",
"postgreSql": {
"connectionString": "Host=localhost;Database=vault_dev;Username=postgres;Password=ComplexPassword#123;Include Error Detail=true"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ See the dev/ folder and the contributing docs for example on how we set defaults in these and then use secrets. We will want this utility to utilize all the central practices.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was work around also, will work on regular secrets population

}

// Create identities
var identities = CipherSeeder.SampleData.Identities.OrderBy(_ => random.Next()).Take(identityCount);

Check warning

Code scanning / Checkmarx One

Use of Cryptographically Weak PRNG

Use of Cryptographically Weak PRNG
}

// Create secure notes
var notes = CipherSeeder.SampleData.SecureNotes.OrderBy(_ => random.Next()).Take(noteCount);

Check warning

Code scanning / Checkmarx One

Use of Cryptographically Weak PRNG

Use of Cryptographically Weak PRNG
var random = new Random();

// Create shared login items
var logins = CipherSeeder.SampleData.Logins.OrderBy(_ => random.Next()).Take(sharedLoginCount);

Check warning

Code scanning / Checkmarx One

Use of Cryptographically Weak PRNG

Use of Cryptographically Weak PRNG
- Re-add Skip attribute to performance test
- Clarify SDK integration TODO comment
- Clean up gitignore patterns
- Add project-specific gitignore for RustSdk
- Fix file formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants