csharp
Seeded .NET defects — async void, sync-over-async deadlocks, per-request HttpClient.
17 deliberate defects, each with a marker naming the rule in standards/ it breaks. It measures recall: how many of the defects the reviewer flags at their severity or higher. The pass condition is that all 9 BLOCKER defects are flagged.
Because each marker names a rule, the scorer also grades attribution. A reviewer that finds the bug but cites the wrong rule counts as caught, and separately as misattributed.
Running it
Nothing here is ever merged. Open a throwaway pull request on a pilot repository already onboarded for this stack, adding this directory and seeded/clean together, wait for the automated review to finish, then score it.
seed-canary.yml does exactly this on a schedule and closes the pull request afterwards, including when the run fails.
$ GH_TOKEN=... npx redlinegate metrics score-seeds --repo <org>/<repo> --pr <n>
Flags and output are on redline metrics.
The 17 defects
| Seed | Severity | Rule it violates | Defect |
|---|---|---|---|
| 1 | BLOCKER | csharp/task-returned-from-using | context disposed before the query runs |
| 1 | BLOCKER | core/hardcoded-secrets | hardcoded connection string with credentials |
| 2 | BLOCKER | csharp/insecure-random | predictable reset code |
| 2 | BLOCKER | csharp/httpclient-per-request | HttpClient instantiated per request — socket exhaustion |
| 3 | BLOCKER | csharp/async-void | async void — the exception escapes and takes the process down |
| 4 | BLOCKER | csharp/sql-string-concatenation | SQL built by concatenation with request input |
| 5 | BLOCKER | csharp/sync-over-async | sync-over-async deadlocks and starves the thread pool |
| 6 | BLOCKER | csharp/swallowed-exceptions | exception swallowed, failure reported as success |
| 10 | BLOCKER | core/type-checker-suppression | compiler warning suppressed with no explanation and no ticket |
| 3 | HIGH | csharp/rethrow-loses-stack | stack trace restarts here |
| 4 | HIGH | csharp/reserved-exception-type | generic Exception forces catch (Exception) |
| 5 | HIGH | csharp/culture-implicit-parse-format | parsed with the server culture |
| 6 | HIGH | csharp/case-compare-via-tolower | culture-dependent comparison |
| 7 | HIGH | csharp/ef-inmemory-test-double | in-memory provider hides real query failures |
| 7 | HIGH | csharp/entity-in-controller-response | entity returned straight from the controller |
| 8 | HIGH | csharp/datetime-now | DateTime.Now in new code instead of DateTimeOffset.UtcNow |
| 9 | HIGH | csharp/exception-detail-in-response | exception detail leaked to the caller |
Scoring
The score reports BLOCKER recall, false positives, and rule attribution. The canary appends it to data/seed-scores.jsonl and fails the run if BLOCKER recall drops below 1.0 or the clean corpus attracts a false positive. Of the 17 defects here, 9 BLOCKER and 8 HIGH — only the BLOCKER count is a pass condition.
Adding a defect
Add the code and one marker line above it. The format, and what scripts/validate.mjs checks, are on Seeded corpus.
Related
- The csharp standard — every rule these defects test.
- seed-canary.yml — runs this corpus every week.
- Telemetry & validation — where the scores end up.
The full file
// DO NOT MERGE — Redline validation seed (anti-slop rules).
using System;
using System.Globalization;
public class AntiSlop
{
public Task<List<Order>> GetOpenAsync(CancellationToken ct)
{
using var db = _factory.CreateDbContext();
// SEED 1 [BLOCKER] (csharp/task-returned-from-using) context disposed before the query runs
return db.Orders.Where(o => o.IsOpen).ToListAsync(ct);
}
public string CreateResetCode()
{
// SEED 2 [BLOCKER] (csharp/insecure-random) predictable reset code
var code = new Random().Next(100000, 999999);
return code.ToString(CultureInfo.InvariantCulture);
}
public async Task SaveAsync(Order order, CancellationToken ct)
{
try { await _repo.SaveAsync(order, ct); }
catch (DbUpdateException ex)
{
_logger.LogError(ex, "Save failed");
// SEED 3 [HIGH] (csharp/rethrow-loses-stack) stack trace restarts here
throw ex;
}
}
public async Task<Customer> GetAsync(Guid id, CancellationToken ct)
{
return await _db.Customers.FindAsync([id], ct)
// SEED 4 [HIGH] (csharp/reserved-exception-type) generic Exception forces catch (Exception)
?? throw new Exception("Customer not found");
}
public decimal Amount(string[] cols)
{
// SEED 5 [HIGH] (csharp/culture-implicit-parse-format) parsed with the server culture
return decimal.Parse(cols[2]);
}
// SEED 6 [HIGH] (csharp/case-compare-via-tolower) culture-dependent comparison
public bool IsAdmin(string role) => role.ToUpper() == "ADMIN";
public ShopDb TestDb() =>
new ShopDb(new DbContextOptionsBuilder<ShopDb>()
// SEED 7 [HIGH] (csharp/ef-inmemory-test-double) in-memory provider hides real query failures
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options);
}
// DO NOT MERGE — Redline validation seed.
// Every `SEED n [SEVERITY] (rule-id)` marker must be flagged at that severity or higher,
// citing that rule id. Score with scripts/score-seeds.mjs.
using System;
using System.Data.SqlClient;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace Redline.Seed;
[ApiController]
[Route("accounts")]
public class SeededViolationsController : ControllerBase
{
// SEED 1 [BLOCKER] (core/hardcoded-secrets) hardcoded connection string with credentials
private const string ConnectionString =
"Server=prod-sql;Database=billing;User Id=sa;Password=Pr0d-S3cret-2026!;";
// SEED 2 [BLOCKER] (csharp/httpclient-per-request) HttpClient instantiated per request — socket exhaustion
private readonly HttpClient _http = new HttpClient();
// SEED 3 [BLOCKER] (csharp/async-void) async void — the exception escapes and takes the process down
public async void FireAndForget(string id)
{
await _http.GetAsync($"https://internal/audit/{id}");
}
[HttpGet]
public IActionResult Find(string name)
{
using var conn = new SqlConnection(ConnectionString);
conn.Open();
// SEED 4 [BLOCKER] (csharp/sql-string-concatenation) SQL built by concatenation with request input
var cmd = new SqlCommand("SELECT * FROM accounts WHERE name = '" + name + "'", conn);
// SEED 5 [BLOCKER] (csharp/sync-over-async) sync-over-async deadlocks and starves the thread pool
var balance = _http.GetStringAsync("https://internal/balance").Result;
try
{
cmd.ExecuteNonQuery();
}
catch
{
// SEED 6 [BLOCKER] (csharp/swallowed-exceptions) exception swallowed, failure reported as success
}
// SEED 7 [HIGH] (csharp/entity-in-controller-response) entity returned straight from the controller
return Ok(new AccountEntity { Msisdn = name, Balance = balance });
}
// SEED 8 [HIGH] (csharp/datetime-now) DateTime.Now in new code instead of DateTimeOffset.UtcNow
[HttpGet("now")]
public DateTime Now() => DateTime.Now;
// SEED 9 [HIGH] (csharp/exception-detail-in-response) exception detail leaked to the caller
[HttpGet("boom")]
public IActionResult Boom()
{
try
{
throw new InvalidOperationException("db=prod-sql user=sa");
}
catch (Exception ex)
{
return StatusCode(500, ex.ToString());
}
}
}
public class AccountEntity
{
public string Msisdn { get; set; } = "";
public string Balance { get; set; } = "";
}
public static class Coercion
{
// SEED 10 [BLOCKER] (core/type-checker-suppression) compiler warning suppressed with no explanation and no ticket
#pragma warning disable CS0168
public static string Read(string raw)
{
Exception unused;
return raw;
}
#pragma warning restore CS0168
}