Delphi-RAG-Lint

drag-lint help

Taint analysis (SQL, command, path, deserialization, HTML, log)

Since: 1.37.0-alpha

Six taint rules (SQL, command, path, deserialization, HTML, log) follow index data_flows facts from a source to a sink.

Read this when you want to find tainted values reaching a SQL, command or file-path sink across routines and units. The rules run in lint-all and in lint with a store; the catalogues of sources, sinks and propagators are editable JSON. Since 1.56 the deserialization, HTML-output and log rules are built in, and a value flowing through the else part of an except block is followed (wiki section Taint through exceptions).

How to run it

Full flag list: lint-all in the CLI reference.

Where to click

No IDE surface: this feature runs from the CLI (see How to run it).

Details

Six lint rules follow a value from a catalogued source to a dangerous sink across routines and units: taint-sql-injection (CWE-89), taint-command-injection (CWE-78) taint-path-traversal (CWE-22), taint-deserialization (CWE-502, request body to a component or object reader), taint-html-output (CWE-79, request field to a response write) and taint-log (CWE-117, off by default; turn on with --enable taint-log). They walk the data_flows facts that the indexer writes (extractor 1.26.0-alpha), so they need a project index.

Running it from the CLI

drag-lint lint-all --db <db> --rule taint-sql-injection
drag-lint lint <file.pas> --db <db> --rule taint-path-traversal

lint-all runs the five default-on rules (taint-log is off by default). lint <file> runs them only with --db. A run that disables every taint rule skips the taint stage. Findings carry the path from source to sink.

Since 1.52 each rule reports one finding per sink site (file, line, column, rule), however many sources or paths reach it. The message names the best path (certain before weak, then the shortest) and, when more than one distinct path reaches the sink, ends with a summary such as (4 sources, 6 paths). Nothing is dropped: in --format json the path key holds that best path (source first, sink last) and paths lists every distinct path, best first. A dl:ok marker on the sink line reviews the whole site, as it did each per-path finding before (the marker hash is unchanged). max_paths_per_sink is still accepted in the config and ignored.

Since 1.52.3 a value that a routine writes into one field of a var / out parameter (pUnits[I].Error := Line) is followed as that field only. If no routine on the rest of the path names that field, the hit is dropped: a dcc message stored in TGpaUnit.Error no longer makes a later delete through TGpaUnit.PasPath a path-traversal finding. The hit is kept when the field is named in the code of any routine on the rest of the path (a comment or a string literal that says "error" does not count), when the routine also assigns the parameter whole (pUnits := ... or pUnits[I] := ...), when the value passes through a call's result or out parameter, or when it passes through a field or global. Only a proven absence drops a hit.

Catalogues

The sources, sinks, propagators and sanitizers are JSON files shipped in rules\: taint-sources.json, taint-sinks.json, taint-propagators.json. The taint object of drag-lint-lint.json extends them (sources, sinks, propagators, sanitizers, disable, max_depth, lib_depth, max_rounds, follow_ambiguous, weak_receiver_regex, weak_severity_drop, max_paths_per_sink, second_order). A source row can name categories and trusted_args: the command line, a dropped file and a file dialog are not path-traversal sources, and ParamStr(0) is the module path, not user input. GetEnvironmentVariable is a source by default (a service may read a variable a lower-privileged user sets); a project whose environment is operator-set switches it off per project with "taint": { "disable": ["getenvironmentvariable"] } in its drag-lint-lint.json (drag-lint's own config does).

What it needs

An index of the project (drag-lint index --project <file.dproj>) with the 1.37.0 extractor, so the data_flows table is filled.

Taint through exceptions

A tainted value that leaves a routine inside an exception is followed into the handler that catches it:

procedure Load;                          procedure Run(Q: TFDQuery);
begin                                    begin
  raise EBadName.Create(                   try
    'unknown ' + ParamStr(1));               Load;
end;                                       except
                                             on E: EBadName do
                                               Q.SQL.Text := E.Message;   // taint-sql-injection
                                           end;
                                         end;

The finding is reported at the handler's sink, under the ordinary rule id (taint-sql-injection, taint-html-output, ...), one per sink site and rule. Its message reads "Tainted data reaches Text through exception EBadName: raised in Load (line N), caught in Run (line M) -- CWE-89" and its path runs source -> raise -> handler -> sink.

How the two sides are joined (deterministic, from the index; no heuristics on names other than the class):

  • class: the handler catches the raised class -- the same class, an ancestor of it (on E: Exception catches everything), or a bare except / ExceptObject;
  • reach: the raise is in the handler's own routine or in a routine it calls, at any depth (12 call hops, 4000 routines per handler at most), over the resolved call edges;
  • taint: the raised constructor argument is checked by the same walk as every other taint rule, so the sources, propagators, sanitizers and categories are the ones of the catalogue.

Limits:

  • the exception value must reach the sink inside the handler's routine (directly or through locals); a handler that passes E.Message to another routine which holds the sink is not reported;
  • a sink entry that names a "type" is matched by its shape in the handler and reported weak;
  • raise; and raise E (re-raise) are not followed; calls dispatched at run time (virtual through an interface, event handlers) are not followed by the reach search;
  • the try block is not narrowed: a raise anywhere in the handler's routine or its callees is joined.

The else part of an except block is analysed too (1.56); before, its statements produced no data-flow rows and no flow findings. No CLI surface change and no new rule id.


All features | CLI reference | IDE menus