Code/SboxSuperuser/Engine/Commands/PrivateReplyValidation.cs

Validation helper for private command replies. Checks that a reply request id is set, the status enum is defined, the message is non-empty, within 1024 chars and control-character free, and that parallel key/value arrays are non-null, equal length, at most 16 entries, unique keys up to 64 chars, values up to 1024 chars, with a combined key plus value length cap of 8192.

namespace SboxSuperuser.Engine.Commands;

internal static class PrivateReplyValidation
{
	internal static bool IsValid( Guid requestId, CommandStatus status, string message, string[] keys, string[] values )
	{
		return requestId != Guid.Empty && Enum.IsDefined( status ) && !string.IsNullOrWhiteSpace( message ) && message.Length <= 1024
			&& !message.Any( char.IsControl )
			&& keys is not null && values is not null && keys.Length == values.Length && keys.Length <= 16
			&& keys.All( key => !string.IsNullOrWhiteSpace( key ) && key.Length <= 64 && !key.Any( char.IsControl ) )
			&& values.All( value => value is not null && value.Length <= 1024 && !value.Any( char.IsControl ) )
			&& keys.Distinct( StringComparer.Ordinal ).Count() == keys.Length
			&& keys.Sum( key => key.Length ) + values.Sum( value => value.Length ) <= 8192;
	}
}