Process snapshots
Run any other tool, such as a TLS scanner or a protocol conformance suite, and pin what it did like every other check:
using Nullean.Snapshots;
var result = await ProcessSnapshot.RunAsync(new ProcessCommand("openssl", ["s_client", "-connect", "localhost:8443", "-tls1_2"])
{
Timeout = TimeSpan.FromSeconds(30),
});
result.Render().Should().MatchSnapshot(ProcessSnapshot.Format);
- .verified.process
The command is a file name and a list of arguments, never one shell string: no shell runs, so nothing is split, quoted,
globbed or expanded, and an argument with spaces is one argument. WorkingDirectory, Environment (a null value
removes a variable), Input (written to standard input, then closed), InheritEnvironment, Redact and MaxOutput are
the other options; the last three are the safety defaults below.
A snapshot is the command line, the exit code and the two streams, when they are not empty:
## openssl s_client -connect localhost:8443 -tls1_2
exit: 0
--- stdout
Protocol : TLSv1.2
- A non-zero exit code is part of the result, not an error: pin it, or fail on it yourself.
- When the timeout passes the process and everything it started are killed, and the snapshot says
status: timed out after 30swith whatever was written until then. Cancelling the token kills it too and throws. - A program that cannot start, such as a missing executable, is a result too (
status: could not start), so a missing tool shows up as a snapshot difference. - stdout and stderr are decoded as UTF-8, with line endings normalised.
A snapshot is committed and a failed comparison is printed in CI logs, so what a program writes must not carry a secret. Three defaults keep that from depending on every caller remembering:
- The environment is not inherited. The program starts with
PATH,HOME, the temp folders and the locale (and the system folders on Windows) plus what you put inEnvironment, so aGITHUB_TOKENor a cloud key in the build's environment is not there forenvor a verbose tool to print.InheritEnvironment = true(--inherit-env) passes it all. - Known secrets are redacted. Every value in
Redact(--redact-env NAMEtakes the value from this process's variableNAME), and the value of any variable whose name looks like a secret (TOKEN,SECRET,PASSWORD,KEY,AUTH,CREDENTIAL,COOKIE,SESSION) that you pass inEnvironment, or that is inherited, becomes<redacted>in the command line, stdout and stderr, in the result itself and not only in the rendering. Values shorter than 4 characters found by name are left alone. Redaction matches the exact value: a secret the program transforms (encoded, split over lines) is not found. - Output is capped.
MaxOutput(--max-output, default 1,000,000 characters) keeps the start of each stream, drops the rest and ends with[output truncated at N characters]. The program is still read to the end, so it does not block. A secret cut in two by the limit leaves nothing of itself behind.
A fourth is opt-in because only the caller knows which programs a workflow means to run:
- An allow-list of programs.
--allow-command dotnet --allow-command "node*"(repeatable;*is any characters) letssnap process runstart only a program whose name, exactly as written after--, matches one of them. Any other program is not started: the run fails with exit code 1 andnot run: command 'curl' is not allowed (allowed: dotnet, node*), and the JUnit report and the::errorannotation say so too. The name is matched as written, never resolved, sodotnetdoes not allow./dotnetor/tmp/dotnet. Put it in the workflow file, where a pull request that widens it shows in review. Without it every program may run. It limits which program starts, not what that program does:bashon the list is a shell.
None of this makes it safe to run a command chosen by someone you do not trust: a process can read files and reach the
network. Run snap process run only from workflows and tests that your reviewers see, and give it no secrets it does not need.
The library captures; you reduce. Raw tool output is rarely the same twice (connection ids, timestamps, resolved addresses), and the argument list is part of the snapshot, so keep secrets out of it. Pin a short summary of what you care about:
ProcessSnapshot.Render(result, text => string.Join('\n', text.Split('\n').Where(l => l.StartsWith("Protocol", StringComparison.Ordinal))))
reduce runs on each stream. Scrubbers (SnapshotOptions.Scrub, Scrubbers.Guids) apply as for any snapshot.
snap process run does the same without a test, for a check that is not .NET code. --snapshot says where the result is
pinned (a path inside a test project; its name keys the verified file) and the command follows --:
snap process run --snapshot tests/Shop.Tests/process/tls12.process --timeout 30 -- openssl s_client -connect localhost:8443 -tls1_2
--cwd, --env name=value (repeatable), --inherit-env, --redact-env NAME (repeatable), --max-output, --allow-command (repeatable) and --variant ({name}.{variant}.verified.process) are the other options. Like
snap http run it writes a received file for snap review, never accepts, and never writes the run manifest. Exit codes:
0 matches, 1 differs or is new, 2 usage. To reduce the output, run a wrapper script that prints the summary.
--junit <file> writes a JUnit report with one case, failed when the result differs from the snapshot, and in a GitHub Actions job (or
with --github-actions) a difference is an error annotation on the --snapshot file. The report holds only what the snapshot shows, so
redacted values stay redacted; see reporting in CI.