Esempio n. 1
0
        /// <summary>
        /// Adds a random padding as an http header to the response.
        /// </summary>
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            base.OnResultExecuting(context);

            string resultString = string.Empty;

            // Only works for object results
            if (context.Result is ObjectResult objectResult && objectResult.Value is string objectResultString)
            {
                resultString = objectResultString;
            }

            // Nothing needs doing if we're above the minimum length
            if (resultString.Length >= _Config.ByteCountMinimum)
            {
                _Logger.WriteNoPaddingNeeded(resultString.Length, _Config.ByteCountMinimum);
                return;
            }

            // Calculate length of padding to add
            var paddingLength = _Rng.Next(_Config.ByteCountMinimum, _Config.ByteCountMaximum) - resultString.Length;

            _Logger.WriteLengthOfResponsePadding(paddingLength);

            // Get the padding bytes
            var padding = _PaddingGenerator.Generate(paddingLength);

            _Logger.WritePaddingContent(padding);

            // Add padding here
            context.HttpContext.Response.Headers.Add(PaddingHeader, padding);
            _Logger.WritePaddingAdded();
        }
        /// <summary>
        /// Adds a random padding as an http header to the response.
        /// </summary>
        public override void OnResultExecuting(ResultExecutingContext context)
        {
            base.OnResultExecuting(context);

            string resultString = string.Empty;

            // Only works for object results
            if (context.Result is ObjectResult objectResult && objectResult.Value is string objectResultString)
            {
                resultString = objectResultString;
            }

            // Nothing needs doing if we're above the minimum length
            if (resultString.Length >= _Config.MinimumLengthInBytes)
            {
                _Logger.LogInformation("No padding needed as response length of {Length} is greater than the minimum of {MinimumLengthInBytes}..",
                                       resultString.Length, _Config.MinimumLengthInBytes);

                return;
            }

            // Calculate length of padding to add
            var paddingLength = _Rng.Next(_Config.MinimumLengthInBytes, _Config.MaximumLengthInBytes) - resultString.Length;

            _Logger.LogInformation("Length of response padding: {PaddingLength}", paddingLength);

            // Get the padding bytes
            var padding = _PaddingGenerator.Generate(paddingLength);

            _Logger.LogDebug("Response padding: {Padding}", padding);

            // Add padding here
            context.HttpContext.Response.Headers.Add(PaddingHeader, padding);

            _Logger.LogInformation("Added padding to the response.");
        }