Skip to content

Commit efbe288

Browse files
committed
protect against bypassing licence verification with reflection
1 parent d825478 commit efbe288

1 file changed

Lines changed: 35 additions & 34 deletions

File tree

ServiceStack.Text/src/ServiceStack.Text/LicenseUtils.cs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,27 @@ public static void AssertEvaluationLicense()
178178
"See https://servicestack.net to upgrade to a valid license.").Trace();
179179
}
180180

181-
private static readonly int[] revokedSubs = { 4018, 4019, 4041, 4331, 4581 };
181+
private static readonly int[] revokedSubs = [4018, 4019, 4041, 4331, 4581];
182182

183183
private class __ActivatedLicense
184184
{
185185
internal readonly LicenseKey LicenseKey;
186186
internal __ActivatedLicense(LicenseKey licenseKey) => LicenseKey = licenseKey;
187+
188+
internal static __ActivatedLicense __get => __activatedLicense;
189+
private static __ActivatedLicense __activatedLicense;
190+
internal static void __setActivatedLicense(__ActivatedLicense licence)
191+
{
192+
__activatedLicense = licence;
193+
Env.UpdateServerUserAgent();
194+
}
187195
}
188196

189197
public static string LicenseWarningMessage { get; private set; }
190198

191199
private static string GetLicenseWarningMessage()
192200
{
193-
var key = __activatedLicense?.LicenseKey;
201+
var key = __ActivatedLicense.__get?.LicenseKey;
194202
if (key == null)
195203
return null;
196204

@@ -204,19 +212,29 @@ private static string GetLicenseWarningMessage()
204212
return null;
205213
}
206214

207-
private static __ActivatedLicense __activatedLicense;
208-
209-
private static void __setActivatedLicense(__ActivatedLicense licence)
210-
{
211-
__activatedLicense = licence;
212-
Env.UpdateServerUserAgent();
213-
}
214215

215216
public static void RegisterLicense(string licenseKeyText)
216217
{
218+
void ValidateLicenseKey(LicenseKey key)
219+
{
220+
var releaseDate = Env.GetReleaseDate();
221+
if (releaseDate > key.Expiry)
222+
throw new LicenseException($"This license has expired on {key.Expiry:d} and is not valid for use with this release."
223+
+ ContactDetails).Trace();
224+
225+
if (key.Type == LicenseType.Trial && DateTime.UtcNow > key.Expiry)
226+
throw new LicenseException($"This trial license has expired on {key.Expiry:d}." + ContactDetails).Trace();
227+
228+
__ActivatedLicense.__setActivatedLicense(new __ActivatedLicense(key));
229+
230+
LicenseWarningMessage = GetLicenseWarningMessage();
231+
if (LicenseWarningMessage != null)
232+
Console.WriteLine(LicenseWarningMessage);
233+
}
234+
217235
JsConfig.InitStatics();
218236

219-
if (__activatedLicense != null) //Skip multiple license registrations. Use RemoveLicense() to reset.
237+
if (__ActivatedLicense.__get != null) //Skip multiple license registrations. Use RemoveLicense() to reset.
220238
return;
221239

222240
string subId = null;
@@ -243,7 +261,7 @@ public static void RegisterLicense(string licenseKeyText)
243261

244262
if (Env.IsAot())
245263
{
246-
__setActivatedLicense(new __ActivatedLicense(new LicenseKey { Type = LicenseType.Indie }));
264+
__ActivatedLicense.__setActivatedLicense(new __ActivatedLicense(new LicenseKey { Type = LicenseType.Indie }));
247265
return;
248266
}
249267

@@ -256,7 +274,7 @@ public static void RegisterLicense(string licenseKeyText)
256274
catch (PlatformNotSupportedException pex)
257275
{
258276
// Allow usage in environments like dotnet script
259-
__setActivatedLicense(new __ActivatedLicense(new LicenseKey { Type = LicenseType.Indie }));
277+
__ActivatedLicense.__setActivatedLicense(new __ActivatedLicense(new LicenseKey { Type = LicenseType.Indie }));
260278
}
261279
catch (Exception ex)
262280
{
@@ -300,23 +318,6 @@ or System.Net.Http.HttpRequestException
300318
Thread.CurrentThread.CurrentCulture = hold;
301319
}
302320
}
303-
304-
private static void ValidateLicenseKey(LicenseKey key)
305-
{
306-
var releaseDate = Env.GetReleaseDate();
307-
if (releaseDate > key.Expiry)
308-
throw new LicenseException($"This license has expired on {key.Expiry:d} and is not valid for use with this release."
309-
+ ContactDetails).Trace();
310-
311-
if (key.Type == LicenseType.Trial && DateTime.UtcNow > key.Expiry)
312-
throw new LicenseException($"This trial license has expired on {key.Expiry:d}." + ContactDetails).Trace();
313-
314-
__setActivatedLicense(new __ActivatedLicense(key));
315-
316-
LicenseWarningMessage = GetLicenseWarningMessage();
317-
if (LicenseWarningMessage != null)
318-
Console.WriteLine(LicenseWarningMessage);
319-
}
320321

321322
private const string IndividualPrefix = "Individual (c) ";
322323
private const string OpenSourcePrefix = "OSS ";
@@ -354,12 +355,12 @@ private static void ValidateFreeLicenseKey(string licenseText)
354355
throw new LicenseException($"This license has expired on {key.Expiry:d} and is not valid for use with this release.\n"
355356
+ "Check https://servicestack.net/free for eligible renewals.").Trace();
356357

357-
__setActivatedLicense(new __ActivatedLicense(key));
358+
__ActivatedLicense.__setActivatedLicense(new __ActivatedLicense(key));
358359
}
359360

360-
internal static string Info => __activatedLicense?.LicenseKey == null
361+
internal static string Info => __ActivatedLicense.__get?.LicenseKey == null
361362
? "NO"
362-
: __activatedLicense.LicenseKey.Type switch {
363+
: __ActivatedLicense.__get.LicenseKey.Type switch {
363364
LicenseType.Free => "FR",
364365
LicenseType.FreeIndividual => "FI",
365366
LicenseType.FreeOpenSource => "FO",
@@ -476,12 +477,12 @@ private static LicenseKey VerifyOpenSourceLicense(string licenseKey)
476477

477478
public static void RemoveLicense()
478479
{
479-
__setActivatedLicense(null);
480+
__ActivatedLicense.__setActivatedLicense(null);
480481
}
481482

482483
public static LicenseFeature ActivatedLicenseFeatures()
483484
{
484-
return __activatedLicense?.LicenseKey.GetLicensedFeatures() ?? LicenseFeature.None;
485+
return __ActivatedLicense.__get?.LicenseKey.GetLicensedFeatures() ?? LicenseFeature.None;
485486
}
486487

487488
public static void ApprovedUsage(int allowedUsage, int actualUsage, string message)

0 commit comments

Comments
 (0)