# Obfuscating a Refit REST client without breaking it


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:

```csharp
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.

<figure class="my-8"><svg viewBox="0 0 700 300" role="img" aria-label="The interface type and method names rename freely while the attribute verb, URL literal, and aliased parameters define the actual HTTP request; a keep-list gate applies only where reflection reads the interface by name" style="width:100%;height:auto"><defs><marker id="rf" markerWidth="9" markerHeight="9" refX="7" refY="3" orient="auto"><path d="M0 0l6 3-6 3z" fill="#64748b"/></marker></defs><text x="18" y="24" style="font:600 13px system-ui;fill:#475569">CLR symbols — rename freely</text><rect x="14" y="38" width="250" height="150" rx="8" fill="#3b82f6" fill-opacity="0.07" stroke="#3b82f6" stroke-width="1.5" stroke-dasharray="5 4"/><text x="30" y="64" style="font:600 13px ui-monospace,monospace;fill:#475569">interface IUserApi</text><text x="30" y="66" style="font:11px system-ui;fill:#64748b"></text><text x="30" y="90" style="font:13px ui-monospace,monospace;fill:#475569">Task GetUser(...)</text><text x="30" y="118" style="font:13px ui-monospace,monospace;fill:#475569">Task ListUsers(...)</text><text x="30" y="146" style="font:11px system-ui;fill:#64748b">+ generated impl class</text><text x="30" y="170" style="font:11px system-ui;fill:#3b82f6">→ renamed &amp; encrypted, request unchanged</text><text x="388" y="24" style="font:600 13px system-ui;fill:#475569">Attributes — pin the wire</text><rect x="384" y="38" width="300" height="150" rx="8" fill="#64748b" fill-opacity="0.07" stroke="#64748b" stroke-width="1.5"/><text x="400" y="64" style="font:600 12px ui-monospace,monospace;fill:#475569">[Get(&quot;/users/{id}&quot;)]</text><text x="400" y="90" style="font:12px ui-monospace,monospace;fill:#475569">[AliasAs(&quot;page_size&quot;)] pageSize</text><text x="400" y="116" style="font:12px ui-monospace,monospace;fill:#475569">[Body] CreateUserRequest body</text><text x="400" y="150" style="font:11px system-ui;fill:#64748b">string literals → verb + path + query keys</text><text x="400" y="170" style="font:11px system-ui;fill:#64748b">GET /users/42?page_size=20</text><path d="M264 113 h114" stroke="#64748b" stroke-width="1.5" fill="none" marker-end="url(#rf)"/><text x="276" y="106" style="font:11px system-ui;fill:#64748b">define</text><rect x="14" y="214" width="670" height="68" rx="8" fill="#f59e0b" fill-opacity="0.08" stroke="#f59e0b" stroke-width="1.5"/><text x="30" y="240" style="font:600 12px system-ui;fill:#475569">Keep-list gate — only where reflection reads the interface by name</text><text x="30" y="262" style="font:12px ui-monospace,monospace;fill:#475569">typeof(IUserApi).Name  ·  nameof(IUserApi.GetUser)</text><text x="400" y="262" style="font:11px system-ui;fill:#64748b">else: no keep rule needed</text></svg></figure>

## 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:

```csharp
[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:

```csharp
[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:

```csharp
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`:

```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.

