3

I've got a Web API project. One of my endpoints allows a string search, which could contain special characters.

[RoutePrefix("api/Search")]
[ValidateInput(false)] // this is *supposed* to allow us to search using "unsafe" characters, like %, & etc.
public class SearchController : ApiController
{
    ...
    [HttpGet]
    [Route("{searchValue}", Name = "GenericSearch")]
    public async Task<IHttpActionResult> Search(string searchValue)
    {
        ...
    }
}

When I call api/Search/fred, this works as expected.

When I call api/Search/fred%25, I get this error:

[HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (%).] System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +561 System.Web.PipelineStepManager.ValidateHelper(HttpContext context) +54

This is despite the fact that my controller is decorated with [ValidateInput(false)], and based on other answers I found elsewhere, I added requestValidationMode to my Web.config:

<system.web>
  ...
  <httpRuntime requestValidationMode="2.0" />
</system.web>

What other secret switch do I need to flip?

Shaul Behr
  • 36,951
  • 69
  • 249
  • 387

1 Answers1

3

You are missing the attribute on httpRuntime for allowing the Invalid Characters.

<httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" />
Mark C.
  • 6,332
  • 4
  • 35
  • 71
  • Thanks! Is there a way to do it so that I don't remove validation on all endpoints, just the ones I specify? And does the `[ValidateInput(false)]` do anything at all? – Shaul Behr May 09 '17 at 12:12
  • Only on my phone at the moment give me a min – Mark C. May 09 '17 at 12:17
  • 1
    Found it [here](http://stackoverflow.com/a/9698482/7850): use a `location` element to confine the settings to urls under that path. If you'd like to update your answer with that, I'll give you answer credit. – Shaul Behr May 09 '17 at 12:28
  • And for those who are interested, `[ValidateInput(false)]` has no effect whatsoever. – Shaul Behr May 09 '17 at 12:35
  • Nice! Good find – Mark C. May 09 '17 at 12:36