-
Notifications
You must be signed in to change notification settings - Fork 3
Security: enforce safe hosted controls for demo UI (#88) #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
teesofttech
merged 2 commits into
master
from
security/issue-88-demo-ui-server-side-secrets
Jul 18, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| namespace PayBridge.SDK.Example.Models; | ||
|
|
||
| public enum DemoRuntimeMode | ||
| { | ||
| Mock = 0, | ||
| Sandbox = 1, | ||
| Live = 2 | ||
| } | ||
|
|
||
| public sealed class DemoUiSecurityOptions | ||
| { | ||
| public const string SectionName = "DemoUiSecurity"; | ||
|
|
||
| // Hosted environments must require authenticated access to demo APIs. | ||
| public bool RequireAuthenticatedAccess { get; set; } = true; | ||
|
|
||
| // Shared API key for non-local environments. Keep in secure stores only. | ||
| public string ApiKey { get; set; } = string.Empty; | ||
|
|
||
| // Live mode is blocked by default and can only be enabled server-side. | ||
| public DemoRuntimeMode Mode { get; set; } = DemoRuntimeMode.Sandbox; | ||
|
|
||
| public bool AllowLiveMode { get; set; } = false; | ||
|
|
||
| // Basic anti-forgery guard for state-changing browser requests. | ||
| public bool RequireCsrfHeader { get; set; } = true; | ||
| public string CsrfHeaderName { get; set; } = "X-Console-CSRF"; | ||
| public string CsrfHeaderValue { get; set; } = string.Empty; | ||
|
|
||
| // Global API throttling. | ||
| public int RequestsPerMinute { get; set; } = 60; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
PayBridge.SDK.Example/Services/DemoUiSecurityOptionsValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using Microsoft.Extensions.Options; | ||
| using PayBridge.SDK.Example.Models; | ||
|
|
||
| namespace PayBridge.SDK.Example.Services; | ||
|
|
||
| public sealed class DemoUiSecurityOptionsValidator : IValidateOptions<DemoUiSecurityOptions> | ||
| { | ||
| public ValidateOptionsResult Validate(string? name, DemoUiSecurityOptions options) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(options); | ||
|
|
||
| var failures = new List<string>(); | ||
| if (options.RequestsPerMinute < 1) | ||
| { | ||
| failures.Add("DemoUiSecurity:RequestsPerMinute must be greater than zero."); | ||
| } | ||
|
|
||
| if (options.Mode == DemoRuntimeMode.Live && !options.AllowLiveMode) | ||
| { | ||
| failures.Add("DemoUiSecurity:Mode cannot be Live unless DemoUiSecurity:AllowLiveMode is true."); | ||
| } | ||
|
|
||
| if (options.RequireAuthenticatedAccess && string.IsNullOrWhiteSpace(options.ApiKey)) | ||
| { | ||
| failures.Add("DemoUiSecurity:ApiKey is required when DemoUiSecurity:RequireAuthenticatedAccess is true."); | ||
| } | ||
| else if (options.RequireAuthenticatedAccess && IsPlaceholderSecret(options.ApiKey)) | ||
| { | ||
| failures.Add("DemoUiSecurity:ApiKey cannot use placeholder values in hosted mode."); | ||
| } | ||
|
|
||
| if (options.RequireCsrfHeader) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(options.CsrfHeaderName)) | ||
| { | ||
| failures.Add("DemoUiSecurity:CsrfHeaderName is required when DemoUiSecurity:RequireCsrfHeader is true."); | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(options.CsrfHeaderValue)) | ||
| { | ||
| failures.Add("DemoUiSecurity:CsrfHeaderValue is required when DemoUiSecurity:RequireCsrfHeader is true."); | ||
| } | ||
| else if (IsPlaceholderSecret(options.CsrfHeaderValue)) | ||
| { | ||
| failures.Add("DemoUiSecurity:CsrfHeaderValue cannot use placeholder values when CSRF protection is enabled."); | ||
| } | ||
| } | ||
|
Comment on lines
+23
to
+47
|
||
|
|
||
| return failures.Count == 0 | ||
| ? ValidateOptionsResult.Success | ||
| : ValidateOptionsResult.Fail(failures); | ||
| } | ||
|
|
||
| private static bool IsPlaceholderSecret(string value) | ||
| { | ||
| var trimmed = value.Trim(); | ||
| return trimmed.StartsWith("YOUR_", StringComparison.OrdinalIgnoreCase) || | ||
| trimmed.StartsWith("CHANGE_ME", StringComparison.OrdinalIgnoreCase) || | ||
| trimmed.Equals("REPLACE_ME", StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
PayBridge.SDK.Test/Unit/DemoUiSecurityOptionsValidatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| using FluentAssertions; | ||
| using PayBridge.SDK.Example.Models; | ||
| using PayBridge.SDK.Example.Services; | ||
| using Xunit; | ||
|
|
||
| namespace PayBridge.SDK.Test.Unit; | ||
|
|
||
| [Trait("Category", "Unit")] | ||
| public class DemoUiSecurityOptionsValidatorTests | ||
| { | ||
| [Fact] | ||
| public void Validate_rejects_live_mode_when_not_explicitly_enabled() | ||
| { | ||
| var validator = new DemoUiSecurityOptionsValidator(); | ||
| var options = NewOptions(); | ||
| options.Mode = DemoRuntimeMode.Live; | ||
| options.AllowLiveMode = false; | ||
|
|
||
| var result = validator.Validate(null, options); | ||
|
|
||
| result.Failed.Should().BeTrue(); | ||
| result.Failures.Should().Contain(item => item.Contains("Mode cannot be Live")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_rejects_missing_api_key_when_hosted_access_is_required() | ||
| { | ||
| var validator = new DemoUiSecurityOptionsValidator(); | ||
| var options = NewOptions(); | ||
| options.RequireAuthenticatedAccess = true; | ||
| options.ApiKey = string.Empty; | ||
|
|
||
| var result = validator.Validate(null, options); | ||
|
|
||
| result.Failed.Should().BeTrue(); | ||
| result.Failures.Should().Contain(item => item.Contains("ApiKey is required")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_rejects_placeholder_api_key_when_hosted_access_is_required() | ||
| { | ||
| var validator = new DemoUiSecurityOptionsValidator(); | ||
| var options = NewOptions(); | ||
| options.RequireAuthenticatedAccess = true; | ||
| options.ApiKey = "YOUR_DEMO_API_KEY"; | ||
|
|
||
| var result = validator.Validate(null, options); | ||
|
|
||
| result.Failed.Should().BeTrue(); | ||
| result.Failures.Should().Contain(item => item.Contains("ApiKey cannot use placeholder")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_rejects_missing_csrf_values_when_csrf_protection_is_enabled() | ||
| { | ||
| var validator = new DemoUiSecurityOptionsValidator(); | ||
| var options = NewOptions(); | ||
| options.RequireCsrfHeader = true; | ||
| options.CsrfHeaderName = string.Empty; | ||
| options.CsrfHeaderValue = string.Empty; | ||
|
|
||
| var result = validator.Validate(null, options); | ||
|
|
||
| result.Failed.Should().BeTrue(); | ||
| result.Failures.Should().Contain(item => item.Contains("CsrfHeaderName is required")); | ||
| result.Failures.Should().Contain(item => item.Contains("CsrfHeaderValue is required")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_rejects_placeholder_csrf_value_when_csrf_protection_is_enabled() | ||
| { | ||
| var validator = new DemoUiSecurityOptionsValidator(); | ||
| var options = NewOptions(); | ||
| options.RequireCsrfHeader = true; | ||
| options.CsrfHeaderValue = "YOUR_DEMO_CSRF_TOKEN"; | ||
|
|
||
| var result = validator.Validate(null, options); | ||
|
|
||
| result.Failed.Should().BeTrue(); | ||
| result.Failures.Should().Contain(item => item.Contains("CsrfHeaderValue cannot use placeholder")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_accepts_secure_sandbox_defaults() | ||
| { | ||
| var validator = new DemoUiSecurityOptionsValidator(); | ||
| var options = NewOptions(); | ||
|
|
||
| var result = validator.Validate(null, options); | ||
|
|
||
| result.Succeeded.Should().BeTrue(); | ||
| } | ||
|
|
||
| private static DemoUiSecurityOptions NewOptions() => new() | ||
| { | ||
| RequireAuthenticatedAccess = true, | ||
| ApiKey = "demo-api-key", | ||
| Mode = DemoRuntimeMode.Sandbox, | ||
| AllowLiveMode = false, | ||
| RequireCsrfHeader = true, | ||
| CsrfHeaderName = "X-Console-CSRF", | ||
| CsrfHeaderValue = "csrf-token", | ||
| RequestsPerMinute = 60 | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.