Skip to content

Commit

Permalink
Added Uri.IsWellFormed check on client handshake host. Prevents excep…
Browse files Browse the repository at this point in the history
…tion on corrupt/null host
  • Loading branch information
statianzo committed May 25, 2011
1 parent f852df5 commit f4f9f24
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
26 changes: 26 additions & 0 deletions src/Fleck.Tests/ClientHandshakeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,31 @@ public void HostnameShouldMatchOnUri()
clientHandshake.Host = "localhost:8181";
Assert.IsTrue(clientHandshake.Validate(null, "ws://localhost:8181/"));
}

[Test]
public void CorruptHostShouldNotValidate()
{
var clientHandshake = new ClientHandshake();
clientHandshake.Key1 = "aaa";
clientHandshake.Key2 = "bbb";
clientHandshake.Origin = "AAA";
clientHandshake.ResourcePath = "BBB";

clientHandshake.Host = "$%%$%NoT^^^A)()(()VALID--==!!URI&&&@@#$#~~~";
Assert.IsFalse(clientHandshake.Validate(null, "ws://localhost:8181/"));
}

[Test]
public void NullHostShouldNotValidate()
{
var clientHandshake = new ClientHandshake();
clientHandshake.Key1 = "aaa";
clientHandshake.Key2 = "bbb";
clientHandshake.Origin = "AAA";
clientHandshake.ResourcePath = "BBB";

clientHandshake.Host = null;
Assert.IsFalse(clientHandshake.Validate(null, "ws://localhost:8181/"));
}
}
}
25 changes: 15 additions & 10 deletions src/Fleck/ClientHandshake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,21 @@ public override string ToString()
return stringShake;
}

public bool Validate(string origin, string host)
{
bool hasRequiredFields = (Host != null) &&
(Key1 != null) &&
(Key2 != null) &&
(Origin != null) &&
(ResourcePath != null);
public bool Validate(string origin, string host)
{
bool hasRequiredFields = (Host != null) &&
(Key1 != null) &&
(Key2 != null) &&
(Origin != null) &&
(ResourcePath != null);
var hostUri = "ws://" + Host;

return hasRequiredFields && new Uri("ws://" + Host) == new Uri(host) && (origin == null || origin == Origin);

}
}
return hasRequiredFields &&
Uri.IsWellFormedUriString(hostUri, UriKind.RelativeOrAbsolute) &&
new Uri(hostUri) == new Uri(host) &&
(origin == null || origin == Origin);

}
}
}

0 comments on commit f4f9f24

Please sign in to comment.