A strongly-typed SignalR hub is the deceptive case. The loose-string style at least looks dangerous — Clients.All.SendAsync("ReceiveMessage", msg) has the magic string right there in the source, and it is obvious a rename pass must leave it alone. But migrate to Hub<IChatClient> and the magic string disappears. You write Clients.All.ReceiveMessage(msg), a plain method call the compiler checks, and it is tempting to assume there is no string left for an obfuscator to break. There is. SignalR manufactures it at runtime.
This post follows that manufactured string — and two others like it — from your C# down to the bytes on the wire, so you can see exactly which names a client depends on and pin precisely those while Nebula renames the rest.
The dispatcher builds a name table once
Start on the inbound side, where a client invokes a server method. The first time a connection reaches a hub, SignalR constructs a DefaultHubDispatcher<THub> whose constructor reflects over the hub type and records every invokable method in a dictionary. The real code does roughly this:
// Simplified from DefaultHubDispatcher<THub>.DiscoverHubMethods().
foreach (var methodInfo in HubType.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
// The key is [HubMethodName] if present, else the CLR method name.
var methodName =
methodInfo.GetCustomAttribute<HubMethodNameAttribute>()?.Name
?? methodInfo.Name;
_methods[methodName] = new HubMethodDescriptor(executor, ...);
}
_methods is keyed with StringComparer.OrdinalIgnoreCase. Every inbound frame carries a Target string, and dispatch is one lookup:
if (!_methods.TryGetValue(hubMethodInvocationMessage.Target, out var descriptor))
{
await SendInvocationError(/* ... */,
$"Unknown hub method '{hubMethodInvocationMessage.Target}'.");
return;
}
So the inbound contract is the key in that dictionary. The key defaults to the CLR method name, which means renaming the method changes the key — the connection still opens, the handshake still succeeds, and the invocation is rejected at runtime with Unknown hub method. Nothing in that path is validated at compile time.
The strongly-typed proxy is generated with Reflection.Emit
Now the outbound side — the deceptive one. With a strongly-typed hub you declare the callbacks as an interface:
public interface IChatClient
{
Task ReceiveMessage(ChatMessage message);
Task UserJoined(string connectionId);
}
public class ChatHub : Hub<IChatClient>
{
public Task SendMessage(ChatMessage message) =>
Clients.Group(message.RoomId).ReceiveMessage(message); // looks like a method call
}
Clients.Group(...) returns an IChatClient, but there is no class in your source that implements it. SignalR builds one at runtime with TypedClientBuilder<IChatClient>, which uses System.Reflection.Emit to define a type whose every method forwards to the underlying proxy — passing the method's own name as a string. The emitted body for ReceiveMessage is equivalent to:
// Emitted at runtime by TypedClientBuilder<IChatClient>, cached per interface.
Task IChatClient.ReceiveMessage(ChatMessage message)
{
return _proxy.SendCoreAsync(
"ReceiveMessage", // == MethodInfo.Name of the interface member
new object[] { message });
}
That literal "ReceiveMessage" is the Name of the interface method, read by reflection when the proxy type is built. It becomes the Target of the outbound frame, and the client registers against the same string:
connection.on("ReceiveMessage", msg => appendLine(msg.author, msg.text));
The strongly-typed syntax did not remove the wire string — it moved it from a literal in your code to the name of an interface member, resolved by reflection at runtime. Rename IChatClient.ReceiveMessage to a and the emitted proxy sends a frame targeting "a", while every client is still listening for "ReceiveMessage". No exception is thrown on the server; the message simply lands nowhere.
The arguments carry property names too
There is a third set of wire strings, and it is the one most keep-lists forget. Hub method arguments are serialized by the hub protocol — System.Text.Json by default — and a complex argument is written as a JSON object keyed by its property names. The ChatMessage above does not travel as an opaque blob; it travels as:
{ "type": 1, "target": "ReceiveMessage",
"arguments": [ { "roomId": "r1", "author": "ada", "text": "hi" } ] }
Each property name in that nested object comes straight from the DTO:
public class ChatMessage
{
public string RoomId { get; set; } = "";
public string Author { get; set; } = "";
public string Text { get; set; } = "";
}
The receiving side binds those JSON keys back to properties by name. Rename Text to a and the server serializes { "a": "hi" } on the way out, while the JavaScript client reads msg.text and gets undefined; inbound, a client that sends { "text": "hi" } binds to nothing and the property arrives as its default. The DTOs that cross the hub boundary are a System.Text.Json contract, and their wire-visible property names are load-bearing exactly like the method names are.
Pinning the surface
Three kinds of names, then, must survive: public hub method names, strongly-typed client interface member names, and the property names of DTOs that cross the boundary. Two tools pin them.
The first is [HubMethodName], which decouples the inbound wire name from the C# identifier so the identifier is free to rename:
[HubMethodName("SendMessage")] // wire name fixed here…
public Task DispatchChatMessage(ChatMessage message) => // …identifier free to rename
Clients.Group(message.RoomId).ReceiveMessage(message);
The dispatcher now registers the method under "SendMessage" no matter what the method is called. This is good hygiene even without obfuscation — your real-time API stops depending on an internal method name. The strongly-typed client interface has no equivalent attribute, so pin the interface type and its member names directly; likewise mark the DTO property names with [JsonPropertyName] or keep them.
The second tool is the Nebula keep-list, which states that surface declaratively and renames everything else:
{
"schemaVersion": 1,
"inputs": [ "bin/Release/net8.0/Chat.Server.dll" ],
"outputDirectory": "protected",
"encryptStrings": true,
"controlFlowObfuscation": true,
"renaming": {
"rules": [
{ "pattern": "Chat.Server.Hubs.*", "action": "keep", "members": "public" },
{ "pattern": "Chat.Server.Contracts.I*Client", "action": "keep", "members": "public" },
{ "pattern": "Chat.Server.Contracts.*Message", "action": "keep", "members": "public" },
{ "pattern": "Chat.Server.*", "action": "rename" }
]
}
}
The first rule keeps the public method names on your hubs while members: "public" lets the hub's private fields, constructor plumbing and helpers rename. The second pins the strongly-typed client interfaces and their member names. The third pins the property names of the boundary DTOs (matched here by a *Message naming convention — match whatever yours actually use). The catch-all renames everything behind them: your IMessageStore and its implementation, repositories, interceptors and DI registration never reach the wire and are fully protected. Run it on the release build, not on developer machines:
<Target Name="NebulaProtect" AfterTargets="Publish" Condition="'$(CI)' == 'true'">
<Exec Command="nebula protect --config nebula.config.json" />
</Target>
Verify the invocation, not just the build
A keep-list is a claim; a green build does not test it, because the names only matter against a live connection. Verify in two passes.
First, decompile the protected assembly in Glass.NET, Delta1 Labs' free .NET decompiler. The ChatHub should still show the public method names you pinned (or the [HubMethodName] string literals in the custom-attribute blob), IChatClient should still list ReceiveMessage and UserJoined, and ChatMessage should still show RoomId, Author and Text. If any came out as a, the keep pattern missed it. Behind that surface, the hub bodies, IMessageStore and DTO backing fields should show renamed identifiers and — with encryptStrings on — opaque constants.
Second, run the real round trip. Point your actual JavaScript or .NET client at the protected server, invoke SendMessage, and assert the client's On("ReceiveMessage", ...) handler fires with every DTO property populated. That single end-to-end call exercises all three wire strings at once; if the method name, an interface member or a DTO property had been renamed away, this is where it surfaces — before production does.
The rule underneath is the same one every reflection-driven framework obeys: a name is load-bearing exactly when something outside the CLR's own token resolution reads it as a string. SignalR reads three — the inbound method name, the interface member name its emitted proxy sends, and the DTO property names its protocol serializes. Pin those, and Nebula renames the hub bodies, the services and the DTO internals freely, protecting your own real-time application without moving a single name a client depends on.
Top comments (0)