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

[PM-13693] Fixed null warnings #4896

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

[PM-13693] Fixed null warnings #4896

wants to merge 12 commits into from

Conversation

MJebran
Copy link

@MJebran MJebran commented Oct 15, 2024

🎟️ Tracking

github issue link:
#2013

link to 14 specified issues mentioned in the above link:
https://pvs-studio.com/en/blog/posts/csharp/0947/

📔 Objective

We fixed 4 issues that could have cased null error, issues were warnings themselves

Changed a method that was not Undernet test, looking for conformation to add tests.

Collaborators (mob programmed)
https://github.com/luris26
https://github.com/BensonB12

🦮 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

MJebran and others added 5 commits October 2, 2024 15:23
…le fixed\n\nCo-authored-by: Luris Solis <[email protected]>\nCo-authored-by: Benson Bird <[email protected]>
…D parameter and rename methods\n\nCo-authored-by: Luris Solis <[email protected]>\nCo-authored-by: Benson Bird <[email protected]>
…d of the null checks because it already assumes that x.u is not null\n\nCo-authored-by: Luris Solis <[email protected]>\nCo-authored-by: Benson Bird <[email protected]>
@MJebran MJebran requested review from a team as code owners October 15, 2024 18:22
@bitwarden-bot
Copy link

Thank you for your contribution! We've added this to our internal Community PR board for review.
ID: PM-13693

@bitwarden-bot bitwarden-bot changed the title Fixed these issues https://pvs-studio.com/en/blog/posts/csharp/0947/ [PM-13693] Fixed these issues https://pvs-studio.com/en/blog/posts/csharp/0947/ Oct 15, 2024
@CLAassistant
Copy link

CLAassistant commented Oct 15, 2024

CLA assistant check
All committers have signed the CLA.

@MJebran MJebran changed the title [PM-13693] Fixed these issues https://pvs-studio.com/en/blog/posts/csharp/0947/ [PM-13693] Fixed null warnings Oct 15, 2024
Copy link
Member

@justindbaur justindbaur left a comment

Choose a reason for hiding this comment

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

Just one note, thanks for the contribution!

@@ -166,7 +166,7 @@ private async Task<HttpResponseMessage> CallFreshdeskApiAsync(HttpRequestMessage
}
}
await Task.Delay(30000 * (retriedCount + 1));
return await CallFreshdeskApiAsync(request, retriedCount++);
return await CallFreshdeskApiAsync(request, retriedCount + 1);
Copy link
Member

Choose a reason for hiding this comment

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

We should probably update this to just increment retriedCount a single time. GitHub isn't letting me to an actual suggestion but it would look like this:

retriedCount++;
await Task.Delay(30000 * retriedCount);
return await CallFreshdeskApiAsync(request, retriedCount);

Comment on lines +684 to +685
IEnumerable<KeyValuePair<int, int>> folderRelationships,
Guid importingUserId)
Copy link
Member

Choose a reason for hiding this comment

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

Love this change, I really dislike trying to grab info from the first item in a list. Much nicer this way.

Comment on lines +31 to +33
UsesKeyConnector = x.u.UsesKeyConnector,
AccessSecretsManager = x.ou.AccessSecretsManager,
HasMasterPassword = x.u != null && !string.IsNullOrWhiteSpace(x.u.MasterPassword)
HasMasterPassword = !string.IsNullOrWhiteSpace(x.u.MasterPassword)
Copy link
Member

Choose a reason for hiding this comment

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

Generated SQLite from before change

SELECT "o"."Id",
  "o"."UserId",
  "o"."OrganizationId",
  "u"."Name",
  COALESCE("u"."Email", "o"."Email") AS "Email",
  "u"."AvatarColor",
  "u"."TwoFactorProviders",
  "u"."Premium",
  "o"."Status",
  "o"."Type",
  "o"."ExternalId",
  "s"."ExternalId" AS "SsoExternalId",
  "o"."Permissions",
  "o"."ResetPasswordKey",
  "u"."Id" IS NOT NULL AND "u"."UsesKeyConnector" AS "UsesKeyConnector",
  "o"."AccessSecretsManager",
  "u"."Id" IS NOT NULL AND "u"."MasterPassword" IS NOT NULL AND trim("u"."MasterPassword") <> 'P|CfDJ8KUSA3v4Y1dFg8Rnf6FjA5fdvqaglKtClSQ1I3diP2vxzTdwq2_dCgDHF-chiyxdEEkJZ_XZnvZFhIpQGoqQL4f35sOzasRWeL8HXy0O59nSBVpX2WOp565OeY4fYRwuNg' AS "HasMasterPassword"
FROM "OrganizationUser" AS "o"
LEFT JOIN "User" AS "u" ON "o"."UserId" = "u"."Id"
LEFT JOIN "SsoUser" AS "s" ON "o"."UserId" = "s"."UserId" AND "o"."OrganizationId" = "s"."OrganizationId"

After change:

SELECT "o"."Id",
  "o"."UserId",
  "o"."OrganizationId",
  "u"."Name",
  COALESCE("u"."Email",
  "o"."Email") AS "Email",
  "u"."AvatarColor",
  "u"."TwoFactorProviders",
  "u"."Premium",
  "o"."Status",
  "o"."Type",
  "o"."ExternalId",
  "s"."ExternalId" AS "SsoExternalId",
  "o"."Permissions",
  "o"."ResetPasswordKey",
  "u"."UsesKeyConnector",
  "o"."AccessSecretsManager",
  "u"."MasterPassword" IS NOT NULL AND trim("u"."MasterPassword") <> 'P|CfDJ8KUSA3v4Y1dFg8Rnf6FjA5dJYW3Rjd8aosV-K01yVWmZVkVbItCW4oOuqXMvKO2PD9DIw0XKjy8sqgWtIKF1q7cT1JKgc3F2mwImAtRqP5WfrlURovVo9WdKmv2sI9lHUg' AS "HasMasterPassword"
FROM "OrganizationUser" AS "o"
LEFT JOIN "User" AS "u" ON "o"."UserId" = "u"."Id"
LEFT JOIN "SsoUser" AS "s" ON "o"."UserId" = "s"."UserId" AND "o"."OrganizationId" = "s"."OrganizationId"

And the equivalent hand wrote SQL:

SELECT
    OU.[Id],
    OU.[UserId],
    OU.[OrganizationId],
    U.[Name],
    ISNULL(U.[Email], OU.[Email]) Email,
    U.[AvatarColor],
    U.[TwoFactorProviders],
    U.[Premium],
    OU.[Status],
    OU.[Type],
    OU.[AccessSecretsManager],
    OU.[ExternalId],
    SU.[ExternalId] SsoExternalId,
    OU.[Permissions],
    OU.[ResetPasswordKey],
    U.[UsesKeyConnector],
    CASE WHEN U.[MasterPassword] IS NOT NULL THEN 1 ELSE 0 END AS HasMasterPassword
FROM
    [dbo].[OrganizationUser] OU
LEFT JOIN
    [dbo].[User] U ON U.[Id] = OU.[UserId]
LEFT JOIN
    [dbo].[SsoUser] SU ON SU.[UserId] = OU.[UserId] AND SU.[OrganizationId] = OU.[OrganizationId]

So this seems like a good change to me.

Copy link
Member

Choose a reason for hiding this comment

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

I also double checked this because I was concerned about orgUsers in the invited state that are not linked to a user, but they're excluded by the inner join on User.Id. So we should always have a User available. And as has been pointed out, we access x.u before this anyway, so clearly we do.

eliykat
eliykat previously approved these changes Nov 1, 2024
Copy link
Member

@eliykat eliykat left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution @MJebran ! The code owned by team-admin-console-dev looks good to me.

Comment on lines +31 to +33
UsesKeyConnector = x.u.UsesKeyConnector,
AccessSecretsManager = x.ou.AccessSecretsManager,
HasMasterPassword = x.u != null && !string.IsNullOrWhiteSpace(x.u.MasterPassword)
HasMasterPassword = !string.IsNullOrWhiteSpace(x.u.MasterPassword)
Copy link
Member

Choose a reason for hiding this comment

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

I also double checked this because I was concerned about orgUsers in the invited state that are not linked to a user, but they're excluded by the inner join on User.Id. So we should always have a User available. And as has been pointed out, we access x.u before this anyway, so clearly we do.

@@ -57,7 +57,7 @@ public async Task PostImport([FromBody] ImportCiphersRequestModel model)
var userId = _userService.GetProperUserId(User).Value;
var folders = model.Folders.Select(f => f.ToFolder(userId)).ToList();
var ciphers = model.Ciphers.Select(c => c.ToCipherDetails(userId, false)).ToList();
await _cipherService.ImportCiphersAsync(folders, ciphers, model.FolderRelationships);
await _cipherService.ImportFolderCiphersAsync(folders, ciphers, model.FolderRelationships, userId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Initially these were likely meant to be method overloads. If we are changing the name I'd prefer:

Suggested change
await _cipherService.ImportFolderCiphersAsync(folders, ciphers, model.FolderRelationships, userId);
await _cipherService.ImportIntoIndividualVaultAsync(folders, ciphers, model.FolderRelationships, userId);

@@ -85,7 +85,7 @@ public async Task PostImport([FromQuery] string organizationId,

var userId = _userService.GetProperUserId(User).Value;
var ciphers = model.Ciphers.Select(l => l.ToOrganizationCipherDetails(orgId)).ToList();
await _cipherService.ImportCiphersAsync(collections, ciphers, model.CollectionRelationships, userId);
await _cipherService.ImportCollectionCiphersAsync(collections, ciphers, model.CollectionRelationships, userId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Initially these were likely meant to be method overloads. If we are changing the name I'd prefer:

Suggested change
await _cipherService.ImportCollectionCiphersAsync(collections, ciphers, model.CollectionRelationships, userId);
await _cipherService.ImportIntoOrganizationalVaultAsync(collections, ciphers, model.CollectionRelationships, userId);

@@ -678,15 +678,14 @@ await _collectionCipherRepository.UpdateCollectionsForAdminAsync(cipher.Id,
await _pushService.PushSyncCipherUpdateAsync(cipher, collectionIds);
}

public async Task ImportCiphersAsync(
public async Task ImportFolderCiphersAsync(
Copy link
Contributor

Choose a reason for hiding this comment

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

Note to self: These should likely be moved into a tools-owned import service leveraging methods from the CipherService/Repository

djsmith85
djsmith85 previously approved these changes Nov 7, 2024
Copy link
Contributor

@djsmith85 djsmith85 left a comment

Choose a reason for hiding this comment

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

Approving for Tools

eliykat
eliykat previously approved these changes Nov 11, 2024
Copy link
Contributor

@cturnbull-bitwarden cturnbull-bitwarden left a comment

Choose a reason for hiding this comment

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

Billing changes look good. Thank you for your contribution!

Copy link
Member

@eliykat eliykat left a comment

Choose a reason for hiding this comment

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

There are some CI failures that are blocking merge.

  1. formatting - please run dotnet format from the root of the repository and then commit the result.
  2. tests failures - for some reason, removing the null check on UsesKeyConnector in OrganizationUserUserViewQuery has broken the SCIM integration tests. It throws a null reference exception even though when I debug everything seems to have a value. However, this also made me notice that we're using it in the SCIM PostUserCommand to return an invited user's details, however I think the inner join in this query would exclude invited users. I will find some time to look into this further.

MJebran and others added 2 commits November 13, 2024 14:29
Copy link
Contributor

Logo
Checkmarx One – Scan Summary & Detailsecec235d-a0dc-4d07-b8b4-cb2a2aba76a5

No New Or Fixed Issues Found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants