Skip to content

Less memory more efficient#221

Closed
fabiodefilipposoftware wants to merge 1 commit into
SpecterOps:2.Xfrom
fabiodefilipposoftware:patch-1
Closed

Less memory more efficient#221
fabiodefilipposoftware wants to merge 1 commit into
SpecterOps:2.Xfrom
fabiodefilipposoftware:patch-1

Conversation

@fabiodefilipposoftware
Copy link
Copy Markdown

@fabiodefilipposoftware fabiodefilipposoftware commented May 16, 2026

Delete "lower" and add StringComparison.OrdinalIgnoreCase No duplicates, less memory and for speed

Description

Motivation and Context

This PR addresses: [GitHub issue or Jira ticket number]

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Chore (a change that does not modify the application functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced LDAP search result filtering for improved consistency and reliability.

Review Change Stack

Delete "lower" and add StringComparison.OrdinalIgnoreCase
No duplicates, less memory and for speed
@github-actions
Copy link
Copy Markdown


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 16, 2026

Walkthrough

This PR refactors the LDAP result filtering logic in LdapProducer to use case-insensitive string comparison more efficiently. The skip conditions for specific distinguished names are rewritten to pass StringComparison.OrdinalIgnoreCase directly to Contains and StartsWith methods instead of lowercasing the input string first, reducing intermediate string allocations while preserving exact filtering behavior.

Changes

LDAP Filtering Optimization

Layer / File(s) Summary
LDAP filtering with case-insensitive comparison
src/Producers/LdapProducer.cs
Skip conditions for cn=domainupdates,cn=system and combined cn=policies,cn=system + (cn=user or cn=machine) checks are updated to use StringComparison.OrdinalIgnoreCase in string comparisons instead of manual lowercasing. EOF formatting is adjusted.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit hops through LDAP trees,
Case-insensitive with such ease,
No ToLower() strings to allocate,
StringComparison makes it great! 🐰✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is incomplete; it lacks details on testing methodology, specific issue links, and unchecked contributing prerequisites despite referencing them. Complete the description by providing specific test cases, checking the relevant type of change checkbox, and confirming all checklist items have been properly addressed.
Title check ❓ Inconclusive The title 'Less memory more efficient' is vague and generic, failing to clearly describe the specific code change made in the pull request. Use a more specific title that describes the actual change, such as 'Replace ToLower() with StringComparison.OrdinalIgnoreCase in LDAP filtering' to clearly communicate the code modification.
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/Producers/LdapProducer.cs`:
- Around line 150-152: The code uses distinguishedName.Contains(...,
StringComparison.OrdinalIgnoreCase) (and a StartsWith overload) which isn't
available on net472; replace the Contains call with a case-insensitive IndexOf
check (e.g., distinguishedName.IndexOf("cn=domainupdates,cn=system",
StringComparison.OrdinalIgnoreCase) >= 0) and keep or ensure the StartsWith uses
the StringComparison overload (distinguishedName.StartsWith("cn=user",
StringComparison.OrdinalIgnoreCase)) or, if you prefer maximum compatibility,
use a length check + string.Equals on the substring with
StringComparison.OrdinalIgnoreCase; update the checks around
distinguishedName.Contains and distinguishedName.StartsWith accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8ef92340-ee0a-4d99-8911-91de6483511b

📥 Commits

Reviewing files that changed from the base of the PR and between f989500 and 9ed82c7.

📒 Files selected for processing (1)
  • src/Producers/LdapProducer.cs

Comment on lines +150 to +152
if (distinguishedName.Contains("cn=domainupdates,cn=system", StringComparison.OrdinalIgnoreCase))
continue;
if (lower.Contains("cn=policies,cn=system") && (lower.StartsWith("cn=user") || lower.StartsWith("cn=machine")))
if (distinguishedName.Contains("cn=policies,cn=system", StringComparison.OrdinalIgnoreCase) && (distinguishedName.StartsWith("cn=user", StringComparison.OrdinalIgnoreCase) || distinguishedName.StartsWith("cn=machine", StringComparison.OrdinalIgnoreCase)))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify target framework supports Contains with StringComparison overload

# Check target framework in project file
echo "=== Target Framework ==="
fd -e csproj --exec cat {} \; | grep -i "TargetFramework"

# Search for other ToLower() patterns that could be optimized similarly
echo -e "\n=== Other ToLower() + comparison patterns ==="
rg -n -C2 '\.ToLower\(\)\s*\.\s*(Contains|StartsWith|EndsWith|Equals|IndexOf)' --type cs

Repository: SpecterOps/SharpHound

Length of output: 187


Fix: string.Contains(string, StringComparison) is unavailable on .NET Framework 4.7.2.

The code at lines 150–152 uses Contains() and StartsWith() with the StringComparison.OrdinalIgnoreCase parameter, which was introduced in .NET Core 2.1 and .NET Standard 2.1. The target framework is net472 (.NET Framework 4.7.2), which does not support this overload. This will cause a compilation error.

Options:

  • Keep the original ToLower() approach for framework compatibility.
  • Upgrade the target framework to .NET 5.0+ or .NET Standard 2.1+.
  • Use a custom case-insensitive comparison method compatible with net472.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Producers/LdapProducer.cs` around lines 150 - 152, The code uses
distinguishedName.Contains(..., StringComparison.OrdinalIgnoreCase) (and a
StartsWith overload) which isn't available on net472; replace the Contains call
with a case-insensitive IndexOf check (e.g.,
distinguishedName.IndexOf("cn=domainupdates,cn=system",
StringComparison.OrdinalIgnoreCase) >= 0) and keep or ensure the StartsWith uses
the StringComparison overload (distinguishedName.StartsWith("cn=user",
StringComparison.OrdinalIgnoreCase)) or, if you prefer maximum compatibility,
use a length check + string.Equals on the substring with
StringComparison.OrdinalIgnoreCase; update the checks around
distinguishedName.Contains and distinguishedName.StartsWith accordingly.

@github-actions github-actions Bot locked and limited conversation to collaborators May 16, 2026
@fabiodefilipposoftware fabiodefilipposoftware deleted the patch-1 branch May 16, 2026 23:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant