Skip to content
← All posts
· Delta1 Labs RefitNebula

Obfuscating a Refit REST client without breaking it

Refit builds an HTTP client from a C# interface, but the wire shape comes from attributes — [Get("/users/{id}")], [Header], [Body], [Query] — not from the interface type or method names. So the interface name, its methods, and the generated implementation all rename freely. The one real hazard is parameter names, which bind to URL path and query keys by name; [AliasAs] decouples them so Nebula can rename everything.

Most “do not obfuscate this” warnings are about serialization: the member name is the wire key, so renaming breaks it. Refit looks like it should be in that category — you hand it a C# interface and it produces an HTTP client — but it is actually one of the friendliest libraries to obfuscate, once you understand where the contract lives. The contract is in the attributes, not in the type and method names. This post walks through exactly what Refit pins to the wire, what it does not, and the one place renaming will silently corrupt your requests if you are not careful.

How Refit turns an interface into a request

You describe a REST API as a plain C# interface and decorate each method with an HTTP attribute. Modern Refit (v6+) runs a source generator at build time that emits a concrete implementation of the interface; older versions build a System.Reflection-based proxy at runtime. Either way, the request is assembled from the attributes:

using Refit;

[Headers("Accept: application/json")]
public interface IUserApi
{
    [Get("/users/{id}")]
    Task<User> GetUser(int id, [Query] bool includeOrders);

    [Get("/users")]
    Task<Page<User>> ListUsers(
        [AliasAs("page")] int pageNumber,
        [AliasAs("page_size")] int pageSize);

    [Post("/users")]
    [Headers("Idempotency-Key: auto")]
    Task<User> CreateUser([Body] CreateUserRequest body);
}

When Refit executes GetUser(42, true), it reads [Get("/users/{id}")], substitutes the parameter named id into the {id} placeholder, and because includeOrders is not a path segment, appends it as a query key. The resulting request line is:

GET /users/42?includeOrders=true
Accept: application/json

The strings IUserApi, GetUser, and includeOrders as symbols are nowhere in that request. The verb came from the attribute name, the path came from a string literal, and the header came from [Headers]. That separation is what makes obfuscation safe.

What renames freely

Three things carry no wire meaning and can be renamed (and, for the implementation, string-encrypted) without changing a single byte of any request:

The interface type name

IUserApi is a CLR type name. Refit never serializes it; it only needs a type to generate against. Rename it to N0 and the generated client still issues GET /users/42.

The method names

GetUser, ListUsers, CreateUser are labels. The HTTP verb and path come from [Get]/[Post] and the literal template, so renaming GetUser to a1 leaves the request identical.

The generated implementation

The source generator emits an internal class (historically AutoGeneratedIUserApi) that implements the interface. It is internal plumbing with no public contract, so it is an ideal rename and string-encryption target — the URL templates it holds are string literals that encrypt cleanly and decrypt to the same path at runtime.

CLR symbols — rename freelyinterface IUserApiTask GetUser(...)Task ListUsers(...)+ generated impl class→ renamed & encrypted, request unchangedAttributes — pin the wire[Get("/users/{id}")][AliasAs("page_size")] pageSize[Body] CreateUserRequest bodystring literals → verb + path + query keysGET /users/42?page_size=20defineKeep-list gate — only where reflection reads the interface by nametypeof(IUserApi).Name · nameof(IUserApi.GetUser)else: no keep rule needed

The one real hazard: parameter names

Here is the trap. A method parameter with no routing attribute binds to the URL by its CLR name:

  • A parameter whose name matches a {placeholder} fills that placeholder.
  • A parameter with no placeholder and no [Body] is appended to the query string under its own name.

So in this un-aliased method:

[Get("/users")]
Task<Page<User>> ListUsers(int page, int pageSize);   // fragile

Refit produces GET /users?page=1&pageSize=20. The query keys page and pageSize are the parameter names. An obfuscator that renames parameters — and many will, because parameter names are metadata an attacker can read — turns this into:

GET /users?a=1&b=20

The server, which still expects page and pageSize, now sees two unknown keys and silently drops your paging. No exception, no compile error — just a wrong request. Path placeholders fail the same way: if GetUser(int id) becomes GetUser(int a), Refit can no longer find a parameter named id to fill {id}.

The fix: alias every bound parameter

Pin the wire name explicitly so it no longer depends on the CLR identifier. [AliasAs] sets the name for a path or query parameter; [Query] can also carry the alias. Once every bound parameter is aliased, the parameter name becomes free to rename:

[Get("/users/{id}")]
Task<User> GetUser([AliasAs("id")] int id, [AliasAs("includeOrders")] bool includeOrders);

[Get("/users")]
Task<Page<User>> ListUsers(
    [AliasAs("page")] int pageNumber,
    [AliasAs("page_size")] int pageSize);

Now the alias string literal defines the wire name, and the CLR parameter can be renamed to anything. This is the single most important habit for an obfuscation-friendly Refit client: treat [AliasAs] as mandatory on every bound parameter, exactly as you would treat [ProtoMember(N)] on a protobuf type.

Dependency injection keeps working

If you register the client by type and resolve it by interface, renaming is transparent — the DI container matches on the Type, which the obfuscator rewrites consistently on both the registration and the resolution side:

builder.Services
    .AddRefitClient<IUserApi>()
    .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"));

// elsewhere, resolved by interface — rename-safe
public sealed class OrderService(IUserApi users)
{
    public Task<User> Load(int id) => users.GetUser(id, includeOrders: true);
}

The only thing that breaks is reflection on the interface by name — typeof(IUserApi).Name, nameof(IUserApi.GetUser), or any manual RestService.For<T> wiring keyed off a string you compare against the type name. If you log the interface name, or drive proxy registration from nameof, that string no longer matches after rename.

The Nebula configuration

For a properly aliased client you need no keep rule at all — the interface, members, and implementation all rename, and the string-encrypted templates still produce the correct request. Add a keep rule only for the reflection-by-name case. A realistic nebula.config.json:

{
  "input": "bin/Release/net8.0/Acme.ApiClient.dll",
  "output": "dist/Acme.ApiClient.dll",
  "rename": {
    "enabled": true,
    "renameParameters": true
  },
  "stringEncryption": {
    "enabled": true
  },
  "controlFlow": {
    "enabled": true
  },
  "keep": [
    {
      "comment": "Only if code does typeof(IUserApi).Name / nameof for logging or manual proxy wiring.",
      "type": "Acme.ApiClient.IUserApi",
      "keepName": true,
      "keepMembers": false
    }
  ]
}

Note keepMembers: false: even when you must keep the type name because something reflects on it, the method names can still rename, because Refit does not read them. If nothing reflects on the interface at all, delete the entire keep block.

Run the protector against the compiled assembly:

nebula protect --config nebula.config.json

Verifying the wire is unchanged

The proof that obfuscation did not touch the contract is that the request line is byte-identical before and after. Point the client at a request-capturing endpoint (or a logging DelegatingHandler) and compare. Before obfuscation, GetUser(42, true) emits:

GET /users/42?includeOrders=true
Accept: application/json

After nebula protect — with IUserApi renamed to N0, GetUser to a, both parameters renamed, and the /users/{id} template string-encrypted — the exact same bytes go out:

GET /users/42?includeOrders=true
Accept: application/json

The URL template was a string literal; Nebula encrypted the stored constant, but it decrypts to /users/{id} at runtime before Refit fills it. The [AliasAs] literals carried the query key names across the parameter rename. Nothing the server reads changed.

Summary

Refit is rename-friendly because the HTTP contract lives in attributes and string literals, not in CLR identifiers. The interface type name, every method name, and the generated implementation can be renamed and string-encrypted freely. The one hazard is parameter names binding to path placeholders and query keys — so put [AliasAs("...")] on every bound parameter to decouple the wire name from the CLR name. After that, let Nebula Pro rename the whole client, and reach for a keep-list only where something genuinely reflects on the interface by name.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.