Skip to content

Conversation

neochrome
Copy link
Contributor

Fixes #504

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use a case insensitive regex? Also, do header values not allow international characters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
You're right, I overlooked the case insensitive part and will remedy that
ASAP. Regarding international hostnames, they are as far as I can tell
actually ecoded in plain ASCII using punycode and a special prefix. It's up
to each application to convert such names back into unicode for display
purposes.
On Feb 7, 2014 5:23 PM, "Phil Haack" notifications@github.com wrote:

In RestSharp/RestRequest.cs:

@@ -356,6 +357,10 @@ public IRestRequest AddParameter (string name, object value, ParameterType type)
///
public IRestRequest AddHeader (string name, string value)
{

  •       if (name == "Host" && (value.Length > 255 || !Regex.IsMatch(value, @"^\w[a-z0-9-]{0,62}(.\w[a-z0-9-]{0,62})*(:\d+)?$")))
    

Shouldn't this use a case insensitive regex? Also, do header values not
allow international characters?

Reply to this email directly or view it on GitHubhttps://github.com//pull/505/files#r9544514
.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I wonder if there's an existing method somewhere for validating this. You'd think System.Net.Http or something would have it.

If not, I'd also probably look at creating a static regex for this and calling IsMatch on that rather than Regex.IsMatch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I found System.Uri.CheckHostName() which seems to perform the validation that is needed. However it doesn't cater for port if given as part of the header value, and it doesn't fail too long host names either (exceeding 255 chars).
I guess we could switch to that one and just optionally split host values on : before testing.
What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@neochrome
Copy link
Contributor Author

@haacked what do you think about this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the empty string case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, multiple colon case? "foo:bar:baz"

hallem added a commit that referenced this pull request Oct 18, 2014
Added parameter check when setting Host header
@hallem hallem merged commit aeb665a into restsharp:master Oct 18, 2014
@hallem
Copy link
Member

hallem commented Oct 18, 2014

Tested with the additional cases and it passed. Will merge this in and then make a commit to add them.

hallem pushed a commit that referenced this pull request Oct 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid host header string doesn't throw ArgumentException

3 participants