Apply security validation and gating at the boundaries of security-sensitive behavior:
Example (input validation + path safety):
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
[ValidateNotNullOrEmpty]
[ValidatePattern(@"^\d{4}-\d{2}-\d{2}$")] // YYYY-MM-DD
public string ScheduledEventsApiVersion { get; set; }
[ValidateNotNullOrEmpty]
public string ContainerSubscriptionId { get; set; } // validate as GUID in code during binding
public static bool IsFilePathWithinDirectory(string filePath, string destinationDirectory)
{
if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(destinationDirectory)) return false;
var fullDestinationDirectory = Path.GetFullPath(destinationDirectory);
if (!fullDestinationDirectory.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal) &&
!fullDestinationDirectory.EndsWith(Path.AltDirectorySeparatorChar.ToString(), StringComparison.Ordinal))
{
fullDestinationDirectory += Path.DirectorySeparatorChar;
}
var fullFilePath = Path.GetFullPath(filePath);
return fullFilePath.StartsWith(fullDestinationDirectory, StringComparison.Ordinal);
}
Operational rule of thumb: if a value or security credential is required for a protected operation, validate it early and explicitly gate the security side-effect to the correct target; never rely on downstream service failures to enforce safety.