I have a simple page that should display an image depending the path send it in the querystring. Im getting a 404 error code after I add the query string.
www.domain.com/ViewImage.aspx?Image=/img/image.jpg
ViewImage.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="baseline">
<img alt="image" id="Img2" runat="server" class="fullimage" src="" />
</td>
</tr>
</table>
</form>
</body>
</html>
ViewImage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
var src = Server.UrlDecode(this.Request.QueryString["Image"]);
this.Img2.Src = validInput(src);
}
protected string validInput(string input)
{
var regex = "[\'<>\"]";
if (null != input && !input.Contains("\"") && input.StartsWith("/"))
{
return !Regex.IsMatch(input, regex) ? AntiXssEncoder.XmlAttributeEncode(input):string.Empty;
}
return string.Empty;
}
Expected result
<img alt="image" id="Img2" runat="server" class="fullimage" src="/img/image.jpg" />
Current result error 404
This is what I've tried and check:
Verified the page exist
www.domain.com/ViewImage.aspx is working fine, no src image set, but is finding the page correctly.
Verified the image exists
www.domain.com/img/image.jpg is working fine, img is displaying right.
Tried with a wrong path
www.domain.com/ViewImage.aspx?Image=/asdasdas/asdas.jpg Im not getting the 404 error and Image src is set right
Tried with only the folder and no image in the url
www.domain.com/ViewImage.aspx?Image=/img, this is not getting a 404 error, but if add the last slash I get the 404 error also.
www.domain.com/ViewImage.aspx?Image=/img/
This last url is getting a 404 error.
Notes :
- We are using SSL in the server (https), not sure if this matters.
- I'm thinking that probably the error can/should be fixed in the IIS configuration, not sure what or how to.
Any suggestions?
UrlDecodingwhat You haven'tUrlEncodedwww.domain.com/ViewImage.aspx?Image=%2Fimg%2Fimage.jpgweb.config; those details could help.