From 68bc797de3c67cc3e7e15f5939ce9f7a1508fe1f Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Wed, 18 Nov 2026 21:03:15 -0700 Subject: [PATCH] Make every opt-out fail closed (0.4.0) A switch that does not clearly mean "on" now turns reporting off, so an opt-out can never be read as consent: - Environment: TRACE_USAGE_REPORTING set to anything but blank or a clear "on" (true/on/yes/1) disables; DO_NOT_TRACK set to anything but blank or a clear "no" (0/false/off/no) disables. Before, only a fixed list of values disabled, so TRACE_USAGE_REPORTING=disabled or DO_NOT_TRACK=on left reporting on. - An environment that cannot be read (e.g. a SecurityException in a sandbox) now disables; it used to leave reporting on. - New TraceClient.AllowsReporting(object) for a program's own setting: only true, or a string true/on/yes/1, allows reporting; anything else, including null, a number or a quoted "false", turns it off. The README and doc-comment examples now pass the setting through it. Counterpart of Stephenson-Software/trace-client-java#51. Co-Authored-By: Claude Opus 5.5 --- README.md | 14 ++-- src/TraceClient/TraceClient.cs | 88 ++++++++++++++++------ src/TraceClient/TraceClient.csproj | 2 +- tests/TraceClient.Tests/TraceClientTest.cs | 72 ++++++++++++++++-- 4 files changed, 137 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index c837385..de8867c 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ using StephensonSoftware.Trace; var trace = new TraceClient("https://trace.danielstephenson.dev", "my-game", "1.4.0", key: settings.UsageReportingKey, - enabled: settings.UsageReportingEnabled, + enabled: TraceClient.AllowsReporting(settings.UsageReportingEnabled), log: message => Debug.WriteLine(message)); // optional if (trace.IsEnabled) @@ -76,7 +76,7 @@ nothing is written. The simplest way is a file the program chooses: ```csharp var trace = new TraceClient(url, "my-game", "1.4.0", key: settings.UsageReportingKey, - enabled: settings.UsageReportingEnabled, + enabled: TraceClient.AllowsReporting(settings.UsageReportingEnabled), installIdFile: Path.Combine(settings.DataDirectory, "trace-install-id")); ``` @@ -139,13 +139,15 @@ the client does not write the key into any message. ## Turning it off Reporting is **opt-out**, and the person running the program always has the -last word. The constructor checks these in order; the first that applies is +last word. **Every switch fails closed:** a value that does not clearly mean +"on" — a typo, a quoted `"false"`, an empty value — turns reporting off, and +nothing in this client ever turns a switch back on. The constructor checks these in order; the first that applies is what `DisabledReason` returns, verbatim, so the program can log it: | Switch | `DisabledReason` | |---|---| -| Environment, for every trace-reporting program at once: `TRACE_USAGE_REPORTING=off` (also `false`, `0`, `no`) or `DO_NOT_TRACK=1` (also `true`, `yes`; the [consoledonottrack.com](https://consoledonottrack.com) convention), case-insensitive. Always checked first; any other value leaves the program's setting in charge. | `environment` | -| The program's own setting: `enabled: false`. | `config` | +| Environment, for every trace-reporting program at once, case-insensitive and trimmed, always checked first: `TRACE_USAGE_REPORTING` set to anything but blank, `true`, `on`, `yes` or `1` (so `off`, `false`, `0`, `no`, `disabled`, a typo, …); or `DO_NOT_TRACK` set to anything but blank, `0`, `false`, `off` or `no` (so `1`, `true`, `yes`, …; the [consoledonottrack.com](https://consoledonottrack.com) convention). An environment that cannot be read (e.g. a `SecurityException` in a sandbox) counts as opting out. | `environment` | +| The program's own setting: `enabled: false`. Read it with `TraceClient.AllowsReporting(value)`, which fails closed the same way: only `true`, or a string `true`/`on`/`yes`/`1` (any case, trimmed), allows reporting; `null`, `false`, a quoted `"false"`, a number or a typo turns it off. | `config` | | No key, or a blank one. | `no key` | `DisabledReason` is `null` when the client reports. A disabled client does @@ -173,7 +175,7 @@ distribution. ## The wire format `POST {baseUrl}/api/metrics` with `Authorization: Bearer `, -`User-Agent: trace-client/0.3.0 ()` and a body of +`User-Agent: trace-client/0.4.0 ()` and a body of ```json {"application":"my-game","name":"level-complete","value":42,"tags":{"version":"1.4.0","install":"0f8b6c1e-3a52-4c8e-9a0d-6e2f1b7c4d90"}} diff --git a/src/TraceClient/TraceClient.cs b/src/TraceClient/TraceClient.cs index 6394132..099f5f4 100644 --- a/src/TraceClient/TraceClient.cs +++ b/src/TraceClient/TraceClient.cs @@ -1,5 +1,5 @@ /* - * trace-client 0.3.0 -- https://github.com/Stephenson-Software/trace-client-csharp + * trace-client 0.4.0 -- https://github.com/Stephenson-Software/trace-client-csharp * * One call to report that a program was used. Copy this file into a project as * is, or reference the project; either way there is nothing else to add. @@ -38,11 +38,18 @@ namespace StephensonSoftware.Trace /// accumulated. /// /// Reporting is opt-out, and the person running the program has the - /// last word. The constructor checks, in this order, and the first match is - /// what says: the environment - /// (TRACE_USAGE_REPORTING=off or DO_NOT_TRACK=1) -- reason - /// environment; the program's own setting, enabled: false -- - /// reason config; no key -- reason no key. + /// last word. Every switch fails closed: a value that does not clearly mean + /// "on" -- a typo, a quoted "false", an empty value -- turns reporting + /// off, and nothing in this client ever turns a switch back on. The + /// constructor checks, in this order, and the first match is what + /// says: the environment + /// (TRACE_USAGE_REPORTING set to anything but blank, true, + /// on, yes or 1; or DO_NOT_TRACK set to anything + /// but blank, 0, false, off or no; or an + /// environment that cannot be read) -- reason environment; the + /// program's own setting, enabled: false -- reason config, + /// read it with so it fails closed too; no key + /// -- reason no key. /// Every event carries the program's own version as the tag /// version -- the third constructor argument, required, so a /// command event can be tied to a release as well as a @@ -56,7 +63,7 @@ namespace StephensonSoftware.Trace /// /// var trace = new TraceClient("https://trace.example.org", "my-game", ProgramVersion, /// key: settings.UsageReportingKey, - /// enabled: settings.UsageReportingEnabled); + /// enabled: TraceClient.AllowsReporting(settings.UsageReportingEnabled)); /// trace.Report("startup"); /// ... /// trace.Dispose(); // on shutdown: sends what is queued, bounded by the timeout @@ -65,7 +72,7 @@ namespace StephensonSoftware.Trace public sealed class TraceClient : IDisposable { /// The client version, as sent in the User-Agent header. - public const string Version = "0.3.0"; + public const string Version = "0.4.0"; /// How many reports may wait to be sent before new ones are dropped. public const int QueueCapacity = 256; @@ -83,10 +90,14 @@ public sealed class TraceClient : IDisposable /// The program's version and installation ID are tag values too, so the constructor rejects either past it. public const int MaxLength = 255; - /// Environment variable that turns reporting off: off, false, 0, no. + /// Environment variable that turns reporting off when set to anything but blank, + /// true, on, yes or 1 (case-insensitive): off, false, + /// 0, no, a typo, ... public const string EnvUsageReporting = "TRACE_USAGE_REPORTING"; - /// Environment variable that turns reporting off: 1, true, yes. See https://consoledonottrack.com. + /// Environment variable that turns reporting off when set to anything but blank, + /// 0, false, off or no (case-insensitive): 1, true, + /// yes, ... See https://consoledonottrack.com. public const string EnvDoNotTrack = "DO_NOT_TRACK"; /// Reason given when an environment variable turned reporting off. @@ -140,7 +151,9 @@ public sealed class TraceClient : IDisposable /// The program's own version, trimmed. Sent as the tag version /// on every event unless the event carries its own. /// The program's write key. Without one the client is a no-op. - /// The program's own opt-out. false yields a client that reports nothing. + /// The program's own opt-out. false yields a client that reports nothing. + /// When the setting comes from a file or other loosely typed source, pass it through + /// so that anything but a clear "on" turns reporting off. /// Where dropped reports and installation ID file failures are mentioned. /// Optional; treat as debug-level. /// Called on the caller's thread or on the client's background thread, so it must be @@ -343,41 +356,68 @@ public bool IsEnabled } /// - /// Whether the environment asks for usage reporting to be off, via - /// or set to one of - /// the values listed there, case-insensitive. Anything else (including empty), or - /// an environment that cannot be read, leaves the program's own setting in charge. + /// Whether the environment asks for usage reporting to be off. Fails closed: + /// set to anything but blank, true, on, + /// yes or 1, or set to anything but blank, + /// 0, false, off or no (case-insensitive, trimmed), turns + /// reporting off. An environment that cannot be read (e.g. a + /// in a sandbox) may hold + /// DO_NOT_TRACK=1, so it counts as opting out too. Never throws. /// public static bool EnvironmentDisables() { try { - return IsOff(EnvironmentSource(EnvUsageReporting)) || IsYes(EnvironmentSource(EnvDoNotTrack)); + string usageReporting = EnvironmentSource(EnvUsageReporting); + if (!string.IsNullOrWhiteSpace(usageReporting) && !IsOn(usageReporting)) + { + return true; + } + string doNotTrack = EnvironmentSource(EnvDoNotTrack); + return !string.IsNullOrWhiteSpace(doNotTrack) && !IsNegative(doNotTrack); } catch (Exception) { - return false; // e.g. a SecurityException in a sandbox; the program's setting stays in charge + return true; // an environment that cannot be read may say DO_NOT_TRACK=1 } } - private static bool IsOff(string value) + /// + /// Whether a program's own reporting setting, as read from its configuration, + /// allows reporting -- for the constructor's enabled. Fails closed: only + /// true, or a string reading true, on, yes or 1 + /// (any case, surrounding space ignored), allows it. Anything else turns reporting + /// off: false, a quoted "false", off, no, a typo, a + /// number, or null. Never throws. + /// + /// The raw value: a , a , + /// null or anything else. + public static bool AllowsReporting(object setting) { - if (value == null) + if (setting is bool) { - return false; + return (bool)setting; } - string v = value.Trim().ToLowerInvariant(); - return v == "off" || v == "false" || v == "0" || v == "no"; + string text = setting as string; + return text != null && IsOn(text); } - private static bool IsYes(string value) + // true, on, yes or 1, ignoring case and surrounding space. + private static bool IsOn(string value) { if (value == null) { return false; } string v = value.Trim().ToLowerInvariant(); - return v == "1" || v == "true" || v == "yes"; + return v == "true" || v == "on" || v == "yes" || v == "1"; + } + + // false, off, no or 0, ignoring case and surrounding space. + private static bool IsNegative(string value) + { + string v = value.Trim().ToLowerInvariant(); + return v == "false" || v == "off" || v == "no" || v == "0"; } /// diff --git a/src/TraceClient/TraceClient.csproj b/src/TraceClient/TraceClient.csproj index ed9d303..e3368a4 100644 --- a/src/TraceClient/TraceClient.csproj +++ b/src/TraceClient/TraceClient.csproj @@ -10,7 +10,7 @@ 7.3 StephensonSoftware.Trace StephensonSoftware.Trace - 0.3.0 + 0.4.0 true true diff --git a/tests/TraceClient.Tests/TraceClientTest.cs b/tests/TraceClient.Tests/TraceClientTest.cs index f61bdba..d1dc515 100644 --- a/tests/TraceClient.Tests/TraceClientTest.cs +++ b/tests/TraceClient.Tests/TraceClientTest.cs @@ -409,7 +409,11 @@ public void DisabledReason_TreatsABlankKeyAsNoKey(string key) [InlineData("0")] [InlineData("no")] [InlineData(" No ")] - public void TraceUsageReporting_DisablesReportingForEveryAcceptedValue(string value) + [InlineData("disabled")] + [InlineData("of")] + [InlineData("\"on\"")] + [InlineData("2")] + public void TraceUsageReporting_DisablesReportingForAnythingButAClearOn(string value) { _environment[TraceClient.EnvUsageReporting] = value; @@ -429,7 +433,11 @@ public void TraceUsageReporting_DisablesReportingForEveryAcceptedValue(string va [InlineData("TRUE")] [InlineData("yes")] [InlineData(" Yes ")] - public void DoNotTrack_DisablesReportingForEveryAcceptedValue(string value) + [InlineData("on")] + [InlineData("y")] + [InlineData("2")] + [InlineData("nope")] + public void DoNotTrack_DisablesReportingForAnythingButAClearNo(string value) { _environment[TraceClient.EnvDoNotTrack] = value; @@ -442,11 +450,16 @@ public void DoNotTrack_DisablesReportingForEveryAcceptedValue(string value) [Theory] [InlineData("TRACE_USAGE_REPORTING", "on")] [InlineData("TRACE_USAGE_REPORTING", "")] + [InlineData("TRACE_USAGE_REPORTING", " ")] [InlineData("TRACE_USAGE_REPORTING", "1")] + [InlineData("TRACE_USAGE_REPORTING", " TRUE ")] + [InlineData("TRACE_USAGE_REPORTING", "Yes")] [InlineData("DO_NOT_TRACK", "0")] [InlineData("DO_NOT_TRACK", "")] [InlineData("DO_NOT_TRACK", "false")] - public void OtherEnvironmentValues_LeaveTheProgramSettingInCharge(string variable, string value) + [InlineData("DO_NOT_TRACK", " OFF ")] + [InlineData("DO_NOT_TRACK", "No")] + public void BlankOrClearlyOnEnvironmentValues_LeaveTheProgramSettingInCharge(string variable, string value) { _environment[variable] = value; @@ -988,19 +1001,62 @@ public void Report_SendsTheNameAndTagsUntrimmed() } [Fact] - public void EnvironmentDisables_IsFalseWhenTheEnvironmentCannotBeRead() + public void EnvironmentDisables_IsTrueWhenTheEnvironmentCannotBeRead() { // A sandbox that forbids reading the environment must not stop the - // program from starting, nor silently decide for it: its own setting rules. + // program from starting. It may hold DO_NOT_TRACK=1, so it fails closed. TraceClient.EnvironmentSource = name => throw new System.Security.SecurityException("sandboxed"); - Assert.False(TraceClient.EnvironmentDisables()); + Assert.True(TraceClient.EnvironmentDisables()); var client = new TraceClient(_server.BaseUrl, "MyGame", "1.2.3", key: "k"); - Assert.True(client.IsEnabled); - Assert.Null(client.DisabledReason); + Assert.False(client.IsEnabled); + Assert.Equal(TraceClient.ReasonEnvironment, client.DisabledReason); client.Close(); } + [Theory] + [InlineData(true)] + [InlineData("true")] + [InlineData("TRUE")] + [InlineData(" on ")] + [InlineData("Yes")] + [InlineData("1")] + public void AllowsReporting_IsTrueOnlyForAClearOn(object setting) + { + Assert.True(TraceClient.AllowsReporting(setting)); + } + + [Theory] + [InlineData(null)] + [InlineData(false)] + [InlineData("false")] + [InlineData("\"false\"")] + [InlineData("\"true\"")] + [InlineData("off")] + [InlineData("no")] + [InlineData("0")] + [InlineData("")] + [InlineData(" ")] + [InlineData("ture")] + [InlineData("enabled")] + [InlineData(1)] + [InlineData(0)] + [InlineData(1.0)] + public void AllowsReporting_FailsClosedForEverythingElse(object setting) + { + Assert.False(TraceClient.AllowsReporting(setting)); + } + + [Fact] + public void AllowsReporting_FeedsTheConfigSwitch() + { + var client = new TraceClient(_server.BaseUrl, "MyGame", "1.2.3", key: "k", + enabled: TraceClient.AllowsReporting("\"false\"")); + + Assert.False(client.IsEnabled); + Assert.Equal(TraceClient.ReasonConfig, client.DisabledReason); + } + [Fact] public void Report_DropsWhatTheServerWouldRejectForItsSize() {