как решить «потенциально опасное значение Request.Form было обнаружено от клиента» в этом коде «requestContext.HttpContext.Request.Params» - PullRequest
0 голосов
/ 07 сентября 2018

Я использовал ckeditor для получения текста от пользователя. это поле называется «Body», а в модели - allowhtml. Для зашифрованного скрытого редукса используется вот так:

и у меня есть ошибка: потенциально опасное значение Request.Form было обнаружено из ошибки клиента в DecryptingControllerFactory:

 public class DecryptingControllerFactory : DefaultControllerFactory
{
    private readonly IEncryptSettingsProvider _settings;

    public DecryptingControllerFactory()
    {
        _settings = new EncryptSettingsProvider();
    }

    public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
    {
        var parameters = requestContext.HttpContext.Request.Params;
        var encryptedParamKeys = parameters.AllKeys.Where(x => x.StartsWith(_settings.EncryptionPrefix)).ToList();

        IRijndaelStringEncrypter decrypter = null;

        foreach (var key in encryptedParamKeys)
        {
            if (decrypter == null)
            {
                decrypter = GetDecrypter(requestContext);
            }

            var oldKey = key.Replace(_settings.EncryptionPrefix, string.Empty);
            var oldValue = decrypter.Decrypt(parameters[key]);
            if (requestContext.RouteData.Values[oldKey] != null)
            {
                if (requestContext.RouteData.Values[oldKey].ToString() != oldValue)
                    throw new ApplicationException("Form values is modified!");
            }
            requestContext.RouteData.Values[oldKey] = oldValue;
        }

        if (decrypter != null)
        {
            decrypter.Dispose();
        }

        return base.CreateController(requestContext, controllerName);
    }

    private IRijndaelStringEncrypter GetDecrypter(System.Web.Routing.RequestContext requestContext)
    {
        var decrypter = new RijndaelStringEncrypter(_settings, requestContext.GetActionKey());
        return decrypter;
    }

}

и ошибка в этой строке:

var parameters = requestContext.HttpContext.Request.Params;

Ошибка: исключение типа «System.Web.HttpRequestValidationException» возникло в System.Web.dll, но не было обработано в коде пользователя

Дополнительная информация: потенциально опасное значение Request.Form было обнаружено от клиента как решить эту проблему?

1 Ответ

0 голосов
/ 13 октября 2018

Я решил проблему с кодом ниже:

 Func<NameValueCollection> formGetter;
        Func<NameValueCollection> queryStringGetter;
        ValidationUtility.GetUnvalidatedCollections(HttpContext.Current, out formGetter, out queryStringGetter);
        var form = formGetter();
        var encryptedParamKeys = form.AllKeys.Where(x => x.StartsWith(_settings.EncryptionPrefix)).ToList();
 var oldValue = decrypter.Decrypt(form[key]);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...