Files
wahrwelt-a-d-setup/docs/vulnerabilities/csharp.md
T
2026-10-10 07:29:48 +03:00

27 KiB

C# / .NET: find, fix and verify

English | Русский

Catalog · Scanner · Patch verification

20 sections. Each follows Search → Vulnerable example → Fix → Verification. Run rg from a local service checkout; searches only read files. Derive the actor/owner from a verified session, not request data. Adapt examples to the actual API and contract.

Contents

CS-DESERIALIZE. Type-controlled .NET deserialization

Search: scanner rule CS-DESERIALIZE. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-DESERIALIZE for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

var formatter = new BinaryFormatter();

Fix: Use explicit DTOs and a supported serializer with schema/size checks; remove unrestricted type activation and migrate legacy data.

var value = JsonSerializer.Deserialize<MessageDto>(json); // validate fields

Verification: Unknown types fail before side effects; valid records parse.

A/D variant: Deserialization triggers setter side effects. FAUST / todo-list, source and organizer patch. TypeNameHandling.Objects permits runtime-type selection; setting Filter model fields writes to the database. Checking the result after object construction may be too late.

Patch: A concrete DTO without external type selection, explicit validation and a separate persistence operation. Test: Unknown types create neither objects nor rows; normal import/export remains compatible. Search: CS-DESERIALIZE.

source/write-up 1; source/write-up 2; source/write-up 3; source/write-up 4; source/write-up 5.

CS-ENUM. Enum.TryParse requires an allowed-value check

Search: scanner rule CS-ENUM. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-ENUM for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

bool ok = Enum.TryParse(input, out FilterKind kind);

Fix: Validate allowed named values; parsing success alone does not prove enum membership.

if (!TryParseAllowedFilter(input, out FilterKind kind)) return BadRequest(); // helper validates named allowlist

Verification: Unnamed numeric values and unsupported flags fail; supported filters work.

A/D variant: Numeric enum values outside the allowed set. Reference: sneakers, RuCTF author write-up. TryParse may accept a number with no declared name. Scanner: CS-ENUM, including already-corrected call sites.

if (!Enum.TryParse<FilterKind>(input, out var kind) ||
    !Enum.IsDefined(typeof(FilterKind), kind))
    return BadRequest();

Flags require an explicit allowed mask and combination policy. Test: Every allowed name, undeclared numbers, negative/oversized values and combinations. Value validation is not authorization.

source/write-up 1.

CS-SQL. Dynamic .NET SQL

Search: scanner rule CS-SQL. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-SQL for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

context.Notes.FromSqlRaw("SELECT * FROM notes WHERE name=" + name);

Fix: Use provider-supported parameters and allowlisted identifiers.

context.Notes.FromSqlRaw("SELECT * FROM notes WHERE name={0}", name);

Verification: Quotes cannot change SQL structure; tenant restrictions remain.

Expanded example and limitations
// Vulnerable: interpolation has already put client data into SQL source.
var rows = await db.Notes
    .FromSqlRaw($"SELECT * FROM Notes WHERE Title = '{title}'")
    .ToListAsync(ct);

Minimal patch for a simple search: owner-scoped LINQ:

using Microsoft.EntityFrameworkCore;

var rows = await db.Notes
    .Where(n => n.OwnerId == currentUserId && n.Title == title)
    .ToListAsync(ct);

When raw SQL is necessary, FromSqlInterpolated receives a FormattableString and parameterizes values. Do not first convert it to string and then pass it to FromSqlRaw. FromSql appeared in EF Core 7; select the installed version's API. Column names require fixed selection.

Check: Apostrophes/Unicode remain literal search data, foreign rows are absent, and pagination/old records work. Test custom providers and LINQ translation against the actual database, not only an in-memory substitute.

CS-GUID-BYTES. Byte-order Guid protocol boundary

Search: scanner rule CS-GUID-BYTES. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-GUID-BYTES for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

var value = new Guid(wireBytes);

Fix: Agree on byte order with peers; use canonical text or explicit conversion, not arbitrary reversal.

var value = Guid.ParseExact(wireText, "D");

Verification: Asymmetric UUID vectors agree across C#/Python, database and cache.

CS-PROCESS. .NET process creation

Search: scanner rule CS-PROCESS. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-PROCESS for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

Process.Start("sh", "-c " + command);

Fix: Prefer a library operation or fixed executable plus ArgumentList. UseShellExecute=false does not neutralize explicit sh/cmd or option injection.

var digest = SHA256.HashData(Encoding.UTF8.GetBytes(text));

Verification: Quotes stay data and expected output format is preserved.

Expanded example and limitations
// Vulnerable on Linux: UseShellExecute=false does not prevent explicit /bin/sh.
var psi = new System.Diagnostics.ProcessStartInfo("/bin/sh") { UseShellExecute = false };
psi.ArgumentList.Add("-c");
psi.ArgumentList.Add("printf '%s' '" + text + "' | sha256sum");
System.Diagnostics.Process.Start(psi);

Patch for text SHA-256, .NET 6+:

using System.Security.Cryptography;
using System.Text;

static string DigestText(string text)
{
    ArgumentNullException.ThrowIfNull(text);
    byte[] sum = SHA256.HashData(Encoding.UTF8.GetBytes(text));
    return Convert.ToHexString(sum).ToLowerInvariant();
}

Preserve external formatting and encoding. If a process is genuinely required, separate executable and ArgumentList; users must still not select dangerous program options. Content SHA-256 is not standalone password storage.

Check: Known digest, empty text, Unicode, quotes/newlines; no child process or environment leakage on failure.

Reference: primary source.

CS-IGNORE-FILTERS. Global query filters disabled

Search: scanner rule CS-IGNORE-FILTERS. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-IGNORE-FILTERS for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

var rows = db.Notes.IgnoreQueryFilters().ToList();

Fix: Do not disable tenant/auth filters merely for soft-delete; admin bypass requires explicit authorization and query restrictions.

var rows = db.Notes.Where(n => n.OwnerId == userId).ToList();

Verification: Test two tenants, deleted records and exports against the actual provider SQL.

Reference: primary source.

CS-ALLOWANON. AllowAnonymous route

Search: scanner rule CS-ALLOWANON. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-ALLOWANON for details. Bounded textual heuristic, not a language parser or taint analysis. Comments, strings and same-named APIs can match; inspect actual execution and trust boundaries.

Vulnerable example:

[AllowAnonymous]
public IActionResult PrivateNote() => View();

Fix: Review effective endpoint metadata/ownership; AllowAnonymous may override Authorize. Login/health may be intentionally public.

[Authorize]
public IActionResult PrivateNote() => View();

Verification: Public login works; private endpoints and nested operations require authorization.

Reference: primary source.

CS-MASS-ASSIGN. Client entities update the database wholesale

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'Update\(|TryUpdateModelAsync|Bind\(|FromBody|SaveChanges' .

Compare DTOs, entities and mutable fields; ensure a second client object is not used after safely loading the owned one.

Vulnerable example:

db.Update(clientNote) applies OwnerId/Role from JSON.

Fix: Editable-fields DTO, lookup by ID+owner and explicit assignment; use concurrency tokens if ownership can change.

Verification: Owned fields change; another user's record/owner does not. Distinguish absent fields from null according to contract.

Expanded example and limitations
// Vulnerable: the supplied entity may contain OwnerId and administrative fields.
db.Update(clientNote);
await db.SaveChangesAsync(ct);

Patch: Separate editable-fields DTO and an owner-scoped entity lookup. Declare the DTO in the project:

public sealed record EditNoteRequest(string? Title, string? Body);

Controller action fragment with parameters id and EditNoteRequest dto:

if (dto.Title is null || dto.Body is null)
    return BadRequest();

var note = await db.Notes.SingleOrDefaultAsync(
    n => n.Id == id && n.OwnerId == currentUserId, ct);
if (note is null)
    return NotFound();

note.Title = dto.Title;
note.Body = dto.Body;
await db.SaveChangesAsync(ct);
return NoContent();

This is a full update of both fields, not JSON Merge Patch. For partial updates distinguish omitted fields from explicit null. Do not pass a second request object to Update after safely loading the first. Concurrent ownership changes require a suitable transaction, conditional mutation or concurrency token.

Check: Owners edit title/body; foreign records/owners remain unchanged; supplied OwnerId is ignored; empty strings persist if allowed; wrong types fail. Unknown JSON fields may be ignored by the serializer—do not promise rejection without explicit configuration.

Reference: primary source.

CS-XML. XML parser permits DTD or external resolver

Search: scanner rule CS-XML. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-XML for details. Regex: DtdProcessing.Parse, XmlResolver=new XmlUrlResolver and ProhibitDtd=false. Defaults depend on framework/API; absence of a match is not proof of safety.

Additional manual search for forms outside automatic coverage:

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'XmlReaderSettings|DtdProcessing|XmlResolver|XmlDocument|XslCompiledTransform' .

Review explicit settings and the actual .NET version; modern safe defaults do not apply to every older API.

Vulnerable example:

An enabled DTD/resolver fetches external resources or expands the document.

Fix: Prohibit DTD, remove external resolver and bound input; pass these settings to the actual XmlReader.

Verification: Ordinary XML works; external resources/DTD fail and size is bounded.

Reference: primary source.

Scanner example before:

var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse, XmlResolver = new XmlUrlResolver() };

Fix template:

var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null, MaxCharactersInDocument = 1048576 };

Primary documentation: reference 1.

CS-HTML-RAW. HTML output without escaping

Search: scanner rule CS-HTML-RAW. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-HTML-RAW for details. Regex: Html.Raw and new HtmlString. Sanitization is not inferred from a call; trusted static markup may be legitimate.

Additional manual search for forms outside automatic coverage:

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'Html.Raw|HtmlString|IHtmlContent|WriteLiteral' .

Determine data origin and Razor's actual HTML, JavaScript or attribute context.

Vulnerable example:

Html.Raw(model.Text) treats user-controlled HTML as trusted.

Fix: Use regular Razor output or an allowlisted HTML sanitizer for supported rich text; consider URL/JS context.

Verification: Untrusted tags, attributes and URLs cannot execute script; permitted text renders.

Scanner example before:

@Html.Raw(Model.About)

Fix template:

@Model.About

Primary documentation: reference 1.

CS-PATH. Path.Combine and GetFullPath do not check ownership

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'Path.Combine|GetFullPath|File.Read|File.Write|PhysicalFile' .

Compare full path/root with OS, separator and symlink semantics; authorize the selected ID.

Vulnerable example:

An absolute second path replaces the root, or string StartsWith accepts a similarly named sibling directory.

Fix: Server-side names, component-aware containment and symlink policy; open writes without replacement races.

Verification: Windows/Linux forms, shared prefixes, outward links and own/foreign files.

CS-ARCHIVE. ZipArchive entries escape the directory

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'ZipArchive|ExtractToFile|ExtractToDirectory|FullName' .

Review the actual API version and custom entry loop; a safe helper does not protect separately implemented writes.

Vulnerable example:

entry.FullName becomes a path without containment or size limits.

Fix: Validate root/links and actual expanded volume; use a separate temporary tree and atomic publication.

Verification: Ordinary archives, absolute/traversal names, duplicate entries and expanded-size overflow.

Reference: primary source.

CS-CLAIMS. Incomplete JWT or claims validation

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'TokenValidationParameters|ValidateIssuer|ValidateAudience|RequireExpirationTime|FindFirst' .

Review server-side issuer/audience/key/algorithm policy and missing claims; a signature does not authorize an object.

Vulnerable example:

A token intended for another purpose is accepted for authentication, or missing subject becomes a default user.

Fix: Explicit library validation contract, required subject/purpose and resource authorization; rotate keys without indefinite legacy fallback.

Verification: Wrong issuer/audience/expiry/purpose plus valid sessions and old data.

CS-ASYNC-VOID. async void hides authorization errors

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'async void|Task.Run|Authorize|async Task|GetAwaiter' .

Ensure callers await verification; background exceptions must not leave successful responses.

Vulnerable example:

Authorization or writes start without await while the handler continues returning data.

Fix: A Task-returning pipeline with await, fail-closed errors and request-scoped cancellation.

Verification: False/error/delayed checks never return protected data; legitimate requests complete.

CS-REGEX-TIMEOUT. Untrusted regex input without a budget

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'new Regex|Regex.Match|Regex.IsMatch|Regex.Replace' .

Review timeout overloads/NonBacktracking support in the installed runtime and input length limits.

Vulnerable example:

A complex expression consumes a worker on a long near-match.

Fix: Bound size, simplify patterns, use supported timeout/NonBacktracking modes and treat RegexMatchTimeoutException as rejection.

Verification: Timeout never becomes permission; ordinary strings and required regex constructs still work.

CS-AUTH-FILTER-REDUCE. Simplification removes a mandatory authorization condition

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'Distinct|Remove|Reduce|AdminFilter|Equals' .

Compare mandatory access conditions before and after query-tree simplification; inspect structural equality of filters.

Vulnerable example:

Reference: sneakers, RuCTF author write-up. Filter-object equality and expression simplification could remove AdminFilter.

Search: distinct/remove/set/dedup/reduce on query ASTs, record structural equality where identity was expected, and combinations of client/server filters.

Fix: Add mandatory authorization outside user-controlled simplification and verify it before execution. Changing record to class in one write-up is not a universal remedy.

Verification: Equivalent, duplicate and nested filters never broaden access; predicate order does not affect permissions.

A/D variant: Source and version: source/write-up 1.

Reference: primary source.

CS-COLLIDING-PRINCIPAL. A normalized character sum replaces a unique user ID

Search: manual review; no separate scanner rule.

rg -n --hidden -g '!vendor/**' -g '!node_modules/**' -g '!.git/**' -e 'UserId|UserName|ToLower|Reverse|Sum' .

Compare normalized names, computed IDs and owner keys across export/read/update.

Vulnerable example:

FAUST / todo-list, source and organizer patch. Name transformations and character sums identify owners; different names can collide.

Fix: Authorize with stable unique principals from trusted storage and migrate existing records coherently. Fix every read/export path.

Verification: Two names sharing the old ID never share access.

A/D variant: Source and version: source/write-up 1; source/write-up 2; source/write-up 3; source/write-up 4; source/write-up 5.

Reference: primary source.

CS-JWT-VALIDATION. Weakened .NET token validation

Search: scanner rule CS-JWT-VALIDATION. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-JWT-VALIDATION for details. Regex: explicit false for ValidateLifetime/Issuer/Audience and RequireSignedTokens/ExpirationTime. ValidateIssuerSigningKey=false alone is not treated as missing signature verification.

Vulnerable example: Disabling lifetime/issuer/audience validation or signature requirements accepts tokens outside the intended contract.

new TokenValidationParameters { ValidateLifetime = false, ValidateAudience = false, RequireSignedTokens = false };

Fix: Validate signature, key, time, issuer and audience; custom validators must retain these invariants.

new TokenValidationParameters { ValidateLifetime = true, ValidateAudience = true, ValidAudience = "notes", ValidateIssuer = true, ValidIssuer = "auth", RequireSignedTokens = true, RequireExpirationTime = true, IssuerSigningKey = trustedKey };

Verification: Unsigned, expired and foreign tokens fail; valid tokens pass.

Primary documentation: reference 1.

CS-TLS-VALIDATION. HTTPS validation callback always accepts

Search: scanner rule CS-TLS-VALIDATION. Run from DurkaHelper: python3 -I -S scripts/defense_scan.py /path/to/service --patches; use --explain CS-TLS-VALIDATION for details. Regex: DangerousAcceptAnyServerCertificateValidator and simple true-lambdas for certificate callbacks. Arbitrary methods and block-bodied lambdas need manual review.

Vulnerable example: An unconditional certificate-accepting callback bypasses standard TLS validation.

handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;

Fix: Use default certificate checks and trusted CA; custom trust must not unconditionally accept.

var handler = new HttpClientHandler();
var client = new HttpClient(handler);

Verification: Valid certificates work; wrong host/CA and expiry fail.

Primary documentation: reference 1.

CS-LDAP-FILTER. DirectorySearcher filter concatenates a value

Search: scanner rule CS-LDAP-FILTER. Use --explain CS-LDAP-FILTER for the exact detector contract. Regex with lexical exclusions: new DirectorySearcher with a first string literal concatenated with an identifier. Does not inspect Filter assignments, interpolated strings, helper implementations or arbitrary type aliases.

Vulnerable example:

var query = new DirectorySearcher("(uid=" + name + ")");

Fix: Use a reviewed LDAP filter builder with filter-context escaping and fixed attribute/scope policy; the helper is application-specific.

var query = new DirectorySearcher(BuildReviewedLdapFilter(name)); // helper implements filter-context escaping

Verification: Metacharacters cannot add conditions or broaden results; supported lookups remain compatible.

Primary reference 1