Skip to content

Commit

Permalink
Verify Subscription has same UserAddress that it was created with
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Mar 24, 2015
1 parent fbcfc0f commit 833e4c2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/ServiceStack/HttpError.cs
Expand Up @@ -119,6 +119,11 @@ public static Exception Conflict(string message)
return new HttpError(HttpStatusCode.Conflict, message);
}

public static Exception Forbidden(string message)
{
return new HttpError(HttpStatusCode.Forbidden, message);
}

public ResponseStatus ToResponseStatus()
{
return Response.GetResponseStatus()
Expand Down
1 change: 1 addition & 0 deletions src/ServiceStack/LocalizedStrings.cs
Expand Up @@ -35,6 +35,7 @@ public static class ErrorMessages

//Server Events
public static string SubscriptionNotExistsFmt = "Subscription '{0}' does not exist";
public static string SubscriptionForbiddenFmt = "Access to Subscription '{0}' is forbidden";

//Validation
public static string RequestAlreadyProcessedFmt = "Request '{0}' has already been processed";
Expand Down
36 changes: 35 additions & 1 deletion src/ServiceStack/ServerEventsFeature.cs
Expand Up @@ -32,6 +32,7 @@ public class ServerEventsFeature : IPlugin
public Action<IResponse, string> OnPublish { get; set; }
public bool NotifyChannelOfSubscriptions { get; set; }
public bool LimitToAuthenticatedUsers { get; set; }
public bool ValidateUserAddress { get; set; }

public ServerEventsFeature()
{
Expand All @@ -44,6 +45,7 @@ public ServerEventsFeature()
HeartbeatInterval = TimeSpan.FromSeconds(10);

NotifyChannelOfSubscriptions = true;
ValidateUserAddress = true;
}

public void Register(IAppHost appHost)
Expand Down Expand Up @@ -77,6 +79,20 @@ public void Register(IAppHost appHost)
appHost.RegisterService(typeof(ServerEventsSubscribersService), SubscribersPath);
}
}

public bool CanAccessSubscription(IRequest req, string subscriptionId)
{
if (!ValidateUserAddress)
return true;

var sub = req.TryResolve<IServerEvents>().GetSubscriptionInfo(subscriptionId);
return sub.UserAddress == req.UserHostAddress;
}

public bool CanAccessSubscription(IRequest req, SubscriptionInfo sub)
{
return !ValidateUserAddress || sub.UserAddress == req.UserHostAddress;
}
}

public class ServerEventsHandler : HttpAsyncTaskHandler
Expand Down Expand Up @@ -204,12 +220,25 @@ public override Task ProcessRequestAsync(IRequest req, IResponse res, string ope

res.ApplyGlobalResponseHeaders();

var serverEvents = req.TryResolve<IServerEvents>();

var feature = HostContext.GetPlugin<ServerEventsFeature>();
if (feature.OnHeartbeatInit != null)
feature.OnHeartbeatInit(req);

if (req.Response.IsClosed)
return EmptyTask;

var subscriptionId = req.QueryString["id"];
if (!req.TryResolve<IServerEvents>().Pulse(subscriptionId))
if (!feature.CanAccessSubscription(req, subscriptionId))
{
res.StatusCode = 403;
res.StatusDescription = "Invalid User Address";
res.EndHttpHandlerRequest(skipHeaders: true);
return EmptyTask;
}

if (!serverEvents.Pulse(subscriptionId))
{
res.StatusCode = 404;
res.StatusDescription = "Subscription {0} does not exist".Fmt(subscriptionId);
Expand Down Expand Up @@ -258,9 +287,14 @@ public class ServerEventsUnRegisterService : Service
public object Any(UnRegisterEventSubscriber request)
{
var subscription = ServerEvents.GetSubscriptionInfo(request.Id);

if (subscription == null)
throw HttpError.NotFound(ErrorMessages.SubscriptionNotExistsFmt.Fmt(request.Id));

var feature = HostContext.GetPlugin<ServerEventsFeature>();
if (!feature.CanAccessSubscription(base.Request, subscription))
throw HttpError.Forbidden(ErrorMessages.SubscriptionForbiddenFmt.Fmt(request.Id));

ServerEvents.UnRegister(subscription.SubscriptionId);

return subscription.Meta;
Expand Down

0 comments on commit 833e4c2

Please sign in to comment.