nodejs

Seeded NestJS defects — unvalidated DTOs, request state on singletons, a blocked event loop.

11 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.

terminal
$ GH_TOKEN=... npx redlinegate metrics score-seeds --repo <org>/<repo> --pr <n>

Flags and output are on redline metrics.

The 11 defects

SeedSeverityRule it violatesDefect
1BLOCKERnodejs/request-state-on-singletonrequest-scoped state on a singleton provider bleeds across users
2BLOCKERnodejs/config-outside-configservicesecret read ad hoc from process.env instead of validated config
3BLOCKERnodejs/await-in-loopawait in a loop for independent operations
4BLOCKERnodejs/blocking-event-loopblocking the event loop in a request path
5BLOCKERcore/customer-data-in-logscustomer identifier in logs
7BLOCKERnodejs/missing-dto-validationuntyped request body with no ValidationPipe DTO
8BLOCKERnodejs/error-detail-swallowederror swallowed and converted to a success response
10BLOCKERcore/type-checker-suppressiontype checker silenced with no explanation and no ticket
11BLOCKERcore/unsafe-assertiondouble assertion overrides the checker with no SAFETY justification
6HIGHnodejs/raw-http-clientoutbound call with no timeout and no shared HTTP client
9HIGHnodejs/missing-guardnew endpoint with no guard — auth is assumed to happen elsewhere

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 11 defects here, 9 BLOCKER and 2 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.

The full file

seeded/nodejs/seeded-violations.service.ts · 81 lines · 2.8 KB
// 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.
import { Body, Controller, Get, Injectable, Post } from '@nestjs/common';
import { readFileSync } from 'node:fs';

@Injectable()
export class SeededService {
  // SEED 1 [BLOCKER] (nodejs/request-state-on-singleton) request-scoped state on a singleton provider bleeds across users
  private currentUserId: string | null = null;

  // SEED 2 [BLOCKER] (nodejs/config-outside-configservice) secret read ad hoc from process.env instead of validated config
  private readonly apiKey = process.env.PAYMENTS_API_KEY ?? 'dev-fallback-key';

  async charge(userId: string, amounts: number[]) {
    this.currentUserId = userId;

    // SEED 3 [BLOCKER] (nodejs/await-in-loop) await in a loop for independent operations
    const results = [];
    for (const amount of amounts) {
      results.push(await this.post(amount));
    }

    // SEED 4 [BLOCKER] (nodejs/blocking-event-loop) blocking the event loop in a request path
    const template = readFileSync('/etc/receipt.tpl', 'utf8');

    // SEED 5 [BLOCKER] (core/customer-data-in-logs) customer identifier in logs
    console.log(`charged msisdn=${userId} total=${results.length}`);

    return template;
  }

  private async post(amount: number) {
    // SEED 6 [HIGH] (nodejs/raw-http-client) outbound call with no timeout and no shared HTTP client
    const res = await fetch('https://payments.internal/charge', {
      method: 'POST',
      headers: { authorization: this.apiKey },
      body: JSON.stringify({ amount }),
    });
    return res.json();
  }
}

@Controller('payments')
export class SeededController {
  constructor(private readonly service: SeededService) {}

  // SEED 7 [BLOCKER] (nodejs/missing-dto-validation) untyped request body with no ValidationPipe DTO
  @Post()
  create(@Body() body: any) {
    // SEED 8 [BLOCKER] (nodejs/error-detail-swallowed) error swallowed and converted to a success response
    try {
      return this.service.charge(body.userId, body.amounts);
    } catch {
      return { ok: true };
    }
  }

  // SEED 9 [HIGH] (nodejs/missing-guard) new endpoint with no guard — auth is assumed to happen elsewhere
  @Get('all')
  all() {
    return { secrets: process.env };
  }
}

// SEED 10 [BLOCKER] (core/type-checker-suppression) type checker silenced with no explanation and no ticket
// @ts-ignore
export function coerce(value: unknown): string {
  return value.toString();
}

interface Charge {
  userId: string;
  amount: number;
}

// SEED 11 [BLOCKER] (core/unsafe-assertion) double assertion overrides the checker with no SAFETY justification
export function asCharge(payload: Record<string, string>): Charge {
  return payload as unknown as Charge;
}