<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Canopy IO</title>
    <description>The latest articles on DEV Community by Canopy IO (@canopy-io).</description>
    <link>https://dev.to/canopy-io</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4239523%2F48be7d9a-441c-42c4-9a51-ef52dccaeecd.png</url>
      <title>DEV Community: Canopy IO</title>
      <link>https://dev.to/canopy-io</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/canopy-io"/>
    <language>en</language>
    <item>
      <title>A retry shouldn't create a second API key</title>
      <dc:creator>Canopy IO</dc:creator>
      <pubDate>Tue, 01 Dec 2026 16:30:24 +0000</pubDate>
      <link>https://dev.to/canopy/a-retry-shouldnt-create-a-second-api-key-bmd</link>
      <guid>https://dev.to/canopy/a-retry-shouldnt-create-a-second-api-key-bmd</guid>
      <description>&lt;p&gt;Creating an API key is a request you can't safely repeat. If the answer is lost on the way back, the caller doesn't know whether a key was made, and retrying blindly can make a second one: a live credential nobody holds the secret for and nobody knows to revoke.&lt;/p&gt;

&lt;p&gt;The usual answer is an &lt;code&gt;Idempotency-Key&lt;/code&gt; header. The caller picks a value for the operation and sends it on the first try and every retry. The server remembers what it answered and gives the same answer again instead of creating twice.&lt;/p&gt;

&lt;p&gt;Canopy has supported it for a while on the routes that create API keys, OAuth clients, identities and invites. When we read our own code closely, it held up for one kind of retry and not the other.&lt;/p&gt;

&lt;h2&gt;
  
  
  What already worked
&lt;/h2&gt;

&lt;p&gt;The first request ran, and its response was stored in Redis for 24 hours, under the Application, the calling principal and the key. A later request with the same key got that stored response back, raw secret included, so the caller could finish what it started.&lt;/p&gt;

&lt;p&gt;Sending the same key with a different request was refused with &lt;code&gt;422&lt;/code&gt;. We digest the method, the path and the body, so a key reused by mistake can't be answered with another request's response.&lt;/p&gt;

&lt;p&gt;The scope matters as much as the replay. The calling principal is part of the cache key, so one API key's stored response, with the secret it minted, is never handed to another caller that happens to pick the same value.&lt;/p&gt;

&lt;h2&gt;
  
  
  What didn't
&lt;/h2&gt;

&lt;p&gt;The response was stored after the request finished. Nothing marked the key as taken while the request was still running.&lt;/p&gt;

&lt;p&gt;That's fine when a retry comes after the first answer is stored. It's not fine for the retry you actually get: a client gives up waiting, the first request is still working, and the retry arrives. Both look in the cache, both find nothing, and both create a key.&lt;/p&gt;

&lt;p&gt;A Redis outage had the same effect. The request ran as if no key had been sent, so a retry during the outage could create twice, and nothing told the caller.&lt;/p&gt;

&lt;h2&gt;
  
  
  Claim the key first
&lt;/h2&gt;

&lt;p&gt;The fix is to take the key before running anything. A request claims it with one atomic Redis &lt;code&gt;SET NX&lt;/code&gt;, writing a marker that says "in flight" and a token only that request knows. Then it runs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fois6e5e8xkkxs0qkvbvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fois6e5e8xkkxs0qkvbvz.png" alt="Two timelines of a first request and a retry that overlaps it. Before: both look in the cache, find it empty, and each creates a key, key A and key B. Now: the first request claims the key, the retry gets 409 request_in_progress, tries again after the response is stored, and gets key A replayed" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Before, both requests found the cache empty. Now the retry waits for the first answer.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What a request with an &lt;code&gt;Idempotency-Key&lt;/code&gt; meets now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nobody holds the key      claim it, run, store the response, answer
a finished request        replay its response (24 hours)
a request still running   409 idempotency.request_in_progress
a request that vanished   409 idempotency.outcome_unknown
a different request       422 idempotency.key_reused_with_different_request
Redis can't be reached    503 idempotency.unavailable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three details made this correct rather than just shorter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The response is stored before the answer goes out.&lt;/strong&gt; A retry that arrives the instant the first answer lands finds the stored response, not an empty key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A failure gives the key back.&lt;/strong&gt; A &lt;code&gt;4xx&lt;/code&gt;, a &lt;code&gt;5xx&lt;/code&gt; or an exception removes the marker, so the caller can fix the request and retry with the same key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only the holder can touch the key.&lt;/strong&gt; Storing and releasing are small Redis scripts that check the token first, so a slow request can never overwrite or delete what a newer one wrote.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The case we chose not to guess
&lt;/h2&gt;

&lt;p&gt;A request can die between claiming the key and recording what happened: a deploy, a crash, a lost connection to Redis. Did it create the key or not? Only that request knew.&lt;/p&gt;

&lt;p&gt;The easy option is to let the marker expire and hand the key to the next retry. That's the duplicate the key exists to prevent. So a marker older than five minutes, longer than any request runs, is treated as abandoned, and a retry gets &lt;code&gt;409 idempotency.outcome_unknown&lt;/code&gt;. The caller checks whether the key exists, then sends a new request with a new key.&lt;/p&gt;

&lt;p&gt;It's a worse experience for one rare case, and an honest one. We'd rather tell a caller "we don't know" than create a credential they never see.&lt;/p&gt;

&lt;p&gt;Redis outages get the same reasoning. A caller who sends the header is asking for a guarantee, so when we can't keep it the request is refused with &lt;code&gt;503&lt;/code&gt; and can be retried. A request without the header never touches Redis and runs as before.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sealing the replay
&lt;/h2&gt;

&lt;p&gt;For a create, the stored response holds the raw secret, so it's sealed with AES-256-GCM under its own key, which production refuses to start without. A copy of Redis alone doesn't give up a secret.&lt;/p&gt;

&lt;p&gt;One limit stays: if Redis itself loses the entry (a flush, or a restart without persistence), the key is free again. That's a property of where the state lives, and we say so in the code rather than pretend otherwise.&lt;/p&gt;

&lt;h2&gt;
  
  
  How we know
&lt;/h2&gt;

&lt;p&gt;An end-to-end test fires six identical create requests with one key at the same time. Exactly one API key exists afterwards, every answer that created something carries the same key, and the rest are &lt;code&gt;409 request_in_progress&lt;/code&gt;. Further tests check that a later retry gets the same secret back, that Redis holds no raw secret, that a different request with the key is refused, and that a request whose outcome was lost is never run again.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Storing the response after the request finishes protects late retries, not overlapping ones. Claim the key before you run.&lt;/li&gt;
&lt;li&gt;Store the response before you answer, and give the key back on failure.&lt;/li&gt;
&lt;li&gt;When you can't know whether a request did its work, refuse the retry. Don't guess.&lt;/li&gt;
&lt;li&gt;A stored response that carries a secret should be encrypted, and scoped to the caller that made it.&lt;/li&gt;
&lt;li&gt;If the store is down, refuse the keyed request rather than run it unprotected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every create that accepts &lt;code&gt;Idempotency-Key&lt;/code&gt; says so on its API reference page. How to create and manage keys is in &lt;a href="https://canopy-io.com/docs/api-keys" rel="noopener noreferrer"&gt;API Keys&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://canopy-io.com/docs/api-keys" class="crayons-btn crayons-btn--primary" rel="noopener noreferrer"&gt;Create an API key&lt;/a&gt;
&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>security</category>
    </item>
    <item>
      <title>An agent should never hold your access</title>
      <dc:creator>Canopy IO</dc:creator>
      <pubDate>Tue, 24 Nov 2026 16:02:01 +0000</pubDate>
      <link>https://dev.to/canopy/an-agent-should-never-hold-your-access-2865</link>
      <guid>https://dev.to/canopy/an-agent-should-never-hold-your-access-2865</guid>
      <description>&lt;p&gt;The easiest way to let an agent act for someone is to give it their token. It works on the first try, and that is the problem.&lt;/p&gt;

&lt;p&gt;A token doesn't say who is holding it. If an assistant in your product calls your API with Sarah's token, your API sees Sarah. It can do everything Sarah can, including the things she would never ask it to do, and your logs say she did them. A prompt injection in a document the agent reads now has Sarah's whole account to work with.&lt;/p&gt;

&lt;p&gt;Asking the agent to be careful doesn't fix this. The limit has to be somewhere the agent can't talk its way past.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two parties, one token
&lt;/h2&gt;

&lt;p&gt;In Canopy, an agent that acts for a person is a machine client: its own credentials and its own roles. When Sarah asks it to do something, it exchanges her access token for a new one, using OAuth token exchange (RFC 8693).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;exchangeToken&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@canopy-io/node"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;accessToken&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;exchangeToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;AGENT_CLIENT_ID&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;clientSecret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;AGENT_CLIENT_SECRET&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;subjectToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;sarahsAccessToken&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;audience&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://api.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The token that comes back names both of them. &lt;code&gt;sub&lt;/code&gt; is Sarah, &lt;code&gt;act.sub&lt;/code&gt; is the agent, and its type is &lt;code&gt;delegated&lt;/code&gt;, so Canopy's SDK refuses it on any route that hasn't opted in. It has no refresh token, and it lasts the machine token lifetime or the rest of Sarah's token, whichever is shorter. The agent has to ask again, and Canopy can say no when it does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Both must hold it, at that place
&lt;/h2&gt;

&lt;p&gt;Its permissions are only the keys both Sarah and the agent hold. That rule alone is common. What matters is where it applies.&lt;/p&gt;

&lt;p&gt;Most systems stop at a flat list of scopes: this token may read deals. But in a real organization, Sarah can read the deals in her region and not the ones in the next region over, and the agent should be no different. In Canopy, permissions are granted at a place in a hierarchy, so the question your API asks names the place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;claims&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;verifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;alsoAllow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"delegated"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;acting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;delegation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;claims&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;authorizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;evaluate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;identity_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;permission&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"deals.read"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;node_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dealNodeId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acting&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;acting_client_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;acting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actingClientId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The answer allows only when Sarah and the agent both hold &lt;code&gt;deals.read&lt;/code&gt; at that node. If the agent's role covers the whole company, it still can't reach a region Sarah can't. If Sarah can reach it but the agent's role stops short, the answer is still no. Neither of them can lend the other anything.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo4t4dpi4yfddnf9u7dl6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fo4t4dpi4yfddnf9u7dl6.png" alt="Three copies of one tree: Company, with Engineering and Sales under it, and Frontend under Engineering. Sarah holds deals.read at Engineering, which covers Frontend. The agent holds it at Frontend only. The delegated token reaches only Frontend, the one place both hold it" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sarah holds it at Engineering, the agent at Frontend. The token reaches only where both hold it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You also opt in per route. &lt;code&gt;alsoAllow: ["delegated"]&lt;/code&gt; goes only on the routes where you let an agent act for someone, and everywhere else the token is refused.&lt;/p&gt;

&lt;h2&gt;
  
  
  We got this wrong first
&lt;/h2&gt;

&lt;p&gt;Canopy runs an MCP server, so an AI assistant can work in your Account for you. When we first built it, a developer signed in and the assistant then borrowed their whole authority. We marked its changes in the audit log as coming through MCP, but the log could not say which assistant it was.&lt;/p&gt;

&lt;p&gt;That is the pattern this post argues against, in our own product.&lt;/p&gt;

&lt;p&gt;Now the developer decides at sign-in. The consent page asks which Application and Environment the assistant may act in, whether it may only read or also change things, and which of their capabilities it may use. Each call is answered from their roles narrowed to that approval. A read-only assistant is refused writes by the API, not just by having the tools hidden.&lt;/p&gt;

&lt;p&gt;The audit row changed with it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actor:    you@company.com
Channel:  mcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;After&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Actor:    you@company.com
Channel:  mcp
Through:  Claude Code
Approval: 7d1c…
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The change is still attributed to the developer, because it is their authority. But now the row says which assistant used it, and under which approval, so you can tell two assistants apart and revoke one of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the log can say
&lt;/h2&gt;

&lt;p&gt;An exchange is recorded whether it succeeds or not: &lt;code&gt;oauth.token_exchange.issued&lt;/code&gt; or &lt;code&gt;oauth.token_exchange.refused&lt;/code&gt;, under the agent. An issued exchange names the person. So Canopy's audit log can answer which agent acted for whom, and when.&lt;/p&gt;

&lt;p&gt;What the agent then does happens in your API, so that line is yours to write, and the token gives you everything you need for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;who&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acting&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="sb"&gt;`${acting.actingClientId}, for ${claims.sub}`&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;claims&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write that on every change, and "the assistant did it" stops being the end of the investigation. You can see which agent, for which person, under what they both held.&lt;/p&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Don't hand an agent a person's token. A token doesn't say who is holding it.&lt;/li&gt;
&lt;li&gt;Give the agent its own identity, and exchange the person's token for one that names both.&lt;/li&gt;
&lt;li&gt;Check both parties at the place being touched, not against a flat list.&lt;/li&gt;
&lt;li&gt;Record who acted, for whom, and through what.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The details are in &lt;a href="https://canopy-io.com/docs/machine-clients" rel="noopener noreferrer"&gt;Machine Clients&lt;/a&gt; and &lt;a href="https://canopy-io.com/docs/mcp" rel="noopener noreferrer"&gt;Connect an MCP Client&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://canopy-io.com/docs/machine-clients" class="crayons-btn crayons-btn--primary" rel="noopener noreferrer"&gt;Read the machine clients guide&lt;/a&gt;
&lt;/p&gt;

</description>
      <category>security</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Why we didn't add a token_delivery parameter</title>
      <dc:creator>Canopy IO</dc:creator>
      <pubDate>Tue, 17 Nov 2026 16:12:36 +0000</pubDate>
      <link>https://dev.to/canopy/why-we-didnt-add-a-tokendelivery-parameter-5dgl</link>
      <guid>https://dev.to/canopy/why-we-didnt-add-a-tokendelivery-parameter-5dgl</guid>
      <description>&lt;p&gt;Mobile apps broke our refresh tokens, and the obvious fix would have broken everything else.&lt;/p&gt;

&lt;p&gt;Canopy issues a refresh token when someone signs in. In a browser it goes into an httpOnly cookie, which is the right answer there for one specific reason: page JavaScript cannot read it. A script injected into a customer's site cannot steal what the browser refuses to hand over.&lt;/p&gt;

&lt;p&gt;An installed phone app has none of that. No cookie jar, nothing re-sending credentials in the background. So the token has to come back in the response body, and the app stores it in the Keychain or the Keystore.&lt;/p&gt;

&lt;p&gt;Which means the API needs to know which kind of client is asking. The obvious way to tell it is a parameter on the request: &lt;code&gt;token_delivery: "cookie" | "body"&lt;/code&gt;, defaulting to &lt;code&gt;cookie&lt;/code&gt;. That is what our own plan said to build.&lt;/p&gt;

&lt;p&gt;It took us longer than it should have to see why that is unshippable.&lt;/p&gt;

&lt;h2&gt;
  
  
  The design that looks right
&lt;/h2&gt;

&lt;p&gt;Be fair to the parameter first. It is flexible. It is backward-compatible: every existing caller sends nothing and keeps its cookie. It defaults to the secure option, so a developer who never reads the docs gets the safe behaviour. Reviewed on its own, it is the conservative choice, and it is what most engineers reach for, including us.&lt;/p&gt;

&lt;p&gt;Now break it, in one move.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A default protects nobody, because an attacker does not take the default.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Picture the attack the cookie exists to stop: a script injected into a customer's website. That script cannot read the httpOnly cookie. It does not need to. It calls &lt;code&gt;/refresh&lt;/code&gt;, the browser attaches the cookie automatically, and the script asks for the response to be shaped differently:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What the site sends&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"publishable_key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"pk_…"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What the injected script sends&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"publishable_key"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"pk_…"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"token_delivery"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"body"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server rotates the token and returns the new one in JSON, to the script, which exfiltrates it. The cookie did its job perfectly and it did not matter, because the protection was conditional on the caller choosing it.&lt;/p&gt;

&lt;p&gt;So the mode cannot live on the request. It has to live somewhere the caller cannot restate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Delivery follows the key
&lt;/h2&gt;

&lt;p&gt;In Canopy, an application signs users in with a publishable key: a &lt;code&gt;pk_…&lt;/code&gt; value that names an Environment and grants nothing on its own. When you create one you say what will use it, &lt;strong&gt;Web&lt;/strong&gt; or &lt;strong&gt;Native&lt;/strong&gt;. A browser app ships a web key and gets cookies. A phone app ships a native key and gets the token in the body. The developer decides once, in the Developer Console, and no request can decide differently.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A publishable key is not a secret. It ships in browser JavaScript and in app binaries, and anyone can read it out of either. It is safe to publish precisely because it does not grant access: it says which Environment is signing in, and now, how that Environment's sessions carry their refresh token.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqrg70xcs5gycnh8adebv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqrg70xcs5gycnh8adebv.png" alt="Two request shapes: on the left a request carrying token_delivery: body is handed the refresh token; on the right the mode is fixed on the publishable key, web or native, and the request has no field to change it" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Left: the mode on the request, which an injected script can set. Right: the mode on the key, which it cannot.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The attack above now has nowhere to go. The injected script can send the site's web key, and gets a cookie it cannot read. It cannot promote itself to body delivery, because body delivery is not a thing a request says.&lt;/p&gt;

&lt;p&gt;Body delivery itself was not new. A trusted backend holding the Environment's secret API key already received the token in the body at login, and still does. That path is safe for a different reason: the credential that unlocks it cannot be held by browser JavaScript. The native key is the first time a &lt;em&gt;public&lt;/em&gt; credential leads to body delivery, which is why where it lives matters so much.&lt;/p&gt;

&lt;h2&gt;
  
  
  The blocking question that wasn't
&lt;/h2&gt;

&lt;p&gt;We spent real effort on a question we believed the whole piece of work turned on: can a client that holds no secret be trusted with a refresh token at all? A phone app is a public client. Anything it ships, it ships to everyone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://datatracker.ietf.org/doc/rfc9700/" rel="noopener noreferrer"&gt;RFC 9700&lt;/a&gt; had already answered it. It is BCP 240, published in January 2025, and section 2.2.2 says that refresh tokens issued to public clients must be either sender-constrained or rotated on every use. The trust does not come from a secret. It comes from making every token single-use, so a stolen one is worth exactly one refresh, and a replay of it is evidence.&lt;/p&gt;

&lt;p&gt;Canopy rotates on every use, for every client. We do not implement DPoP, the sender-constraining branch, and rotation alone satisfies the requirement. The question was closed before we asked it; we just had not read far enough. RFC 9700 is recent enough that plenty of teams have not internalised it yet, which is the only reason this admission is useful rather than embarrassing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Structural beats conditional
&lt;/h2&gt;

&lt;p&gt;Having decided that delivery follows the key, the next instinct is a check: on refresh, look up the key, confirm its type matches the session, refuse if not. That is correct. It is also one refactor away from being wrong, because it is a branch, and branches get reordered, bypassed and forgotten.&lt;/p&gt;

&lt;p&gt;What we shipped instead is that &lt;code&gt;/refresh&lt;/code&gt; accepts no publishable key at all.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;POST&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;identity&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;refresh&lt;/span&gt;
&lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"refresh_token"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"…"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A session records how it delivers its refresh token when it is created, and keeps that for its whole life. Every rotation inherits it. On refresh there is nothing to compare, because there is nowhere to state the mode: a request that includes a &lt;code&gt;publishable_key&lt;/code&gt; is rejected by body validation before any logic runs. A browser caller sends no body at all; the token rides in the cookie.&lt;/p&gt;

&lt;p&gt;The two shapes are worth setting side by side, because they look similar and are not:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Conditional:&lt;/em&gt; "on refresh, check that the key's type matches the session". A defence you have to keep.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Structural:&lt;/em&gt; the parameter does not exist. A defence that cannot regress, because there is no code path for it to regress through.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a user needs to move from one mode to the other, they sign in again. That is the only door, and it is the one the key already guards.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rotation on networks that drop things
&lt;/h2&gt;

&lt;p&gt;Single-use tokens have a cost, and mobile networks collect it.&lt;/p&gt;

&lt;p&gt;Every refresh mints a successor and retires the token it was given. Presenting a token that has already been rotated is, by default, treated as theft: it ends every session the identity holds. That response is correct when someone really is replaying a stolen token. It is wrong when the response was lost after the server had already rotated, which on a phone moving between cells is routine. The app retries with the only token it has, and its user is signed out everywhere for a packet that never arrived.&lt;/p&gt;

&lt;p&gt;The common answer is a fixed overlap window: accept a replay if the rotation happened within the last N seconds. It is simple, and it treats theft and a dropped response as the same thing, because a timer cannot tell them apart.&lt;/p&gt;

&lt;p&gt;Canopy's rule looks at the chain instead. A replay is forgiven only when all three hold:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the presented token was rotated within the last 5 minutes;&lt;/li&gt;
&lt;li&gt;it comes from the same software, judged by user agent; and&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;its successor has never been used.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The third check is the one that carries the weight. If a retired token's successor was consumed, two independent parties hold tokens from the same chain. One of them is not the app that lost a response; that is theft, and it ends every session. If the successor was never used, the only party holding a live token is the one retrying, so Canopy retires the unused successor, issues a fresh one, and the chain keeps exactly one live head. The app does nothing special. It retries with the token it has.&lt;/p&gt;

&lt;p&gt;A timer bounds how long a race can last. The chain says whether it was a race at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the parameter would have cost
&lt;/h2&gt;

&lt;p&gt;Nothing in this design is clever. Each step is the smaller of two options: a property of the key instead of a field on the request, no parameter instead of a check, a question answered by a spec instead of by us, and a fact about the chain instead of a guess about the clock.&lt;/p&gt;

&lt;p&gt;The general lesson is the one we had to learn in our own plan. When a security property depends on the client choosing correctly, it is not a property. It is a suggestion, and the one caller you built it against will decline it. Move the decision to something the client cannot restate, and then remove the place where they would have restated it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://canopy-io.com/docs/authentication/native-apps" class="crayons-btn crayons-btn--primary" rel="noopener noreferrer"&gt;Read the native apps guide&lt;/a&gt;
&lt;/p&gt;

</description>
      <category>security</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Flat roles first, hierarchy when you need it</title>
      <dc:creator>Canopy IO</dc:creator>
      <pubDate>Wed, 11 Nov 2026 18:15:34 +0000</pubDate>
      <link>https://dev.to/canopy/flat-roles-first-hierarchy-when-you-need-it-cca</link>
      <guid>https://dev.to/canopy/flat-roles-first-hierarchy-when-you-need-it-cca</guid>
      <description>&lt;p&gt;Most B2B products ship with three roles: &lt;code&gt;Admin&lt;/code&gt;, &lt;code&gt;Manager&lt;/code&gt;, &lt;code&gt;Viewer&lt;/code&gt;. That is the right way to start, and it is how Canopy starts too. A new Environment begins flat: one root node, every assignment landing on it. Clone an Environment from one that already runs a hierarchy and the clone inherits it, but an Environment created from nothing is flat.&lt;/p&gt;

&lt;p&gt;This post is about the day that stops being enough, and why in Canopy that day costs you a configuration change rather than a rewrite.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Canopy answers
&lt;/h2&gt;

&lt;p&gt;Canopy is identity and access management for B2B SaaS. It signs your users in, with passwords, passwordless email codes and multi-factor, and it federates to your customers' identity providers over SAML and OIDC with SCIM directory sync. Then it answers one question for your application, on every request:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Can this identity do this action in this part of the organization?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The last five words are the part most teams end up building themselves. Canopy holds the organization's shape, so a role granted at a region applies to every store inside it, including the store opened next quarter.&lt;/p&gt;

&lt;p&gt;Your configuration sits in three layers. An &lt;strong&gt;Account&lt;/strong&gt; is your company. Inside it are &lt;strong&gt;Applications&lt;/strong&gt;, one per product whose access you manage. Inside each Application are &lt;strong&gt;Environments&lt;/strong&gt;: isolated copies of your permissions, roles and hierarchy. Signing up creates your first Application with a &lt;code&gt;Development&lt;/code&gt; and a &lt;code&gt;Production&lt;/code&gt; Environment, and everything below happens inside one of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Day one is flat
&lt;/h2&gt;

&lt;p&gt;A new Environment has a permission catalog to fill, no roles, and a single root node. The loop below is five calls, assuming the person you are granting to already exists as an identity with access to the Environment. Each call carries an Environment-scoped API key in &lt;code&gt;X-API-Key&lt;/code&gt;; single resources come back under &lt;code&gt;data&lt;/code&gt;, collections under &lt;code&gt;items&lt;/code&gt;, failures under &lt;code&gt;error&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, tell Canopy the actions your product has. Permissions are yours to name, lowercase and dot-separated, and you can register them in one request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -X POST https://auth.canopy-io.com/api/v1/permissions \
  -H &lt;span class="s2"&gt;"X-API-Key: $CANOPY_API_KEY"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{
        "permissions": [
          { "key": "orders.view",   "name": "View orders",   "category": "orders" },
          { "key": "orders.refund", "name": "Refund orders", "category": "orders" }
        ]
      }'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then bundle them into a role, and set what it grants:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -X POST https://auth.canopy-io.com/api/v1/roles \
  -H &lt;span class="s2"&gt;"X-API-Key: $CANOPY_API_KEY"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{ "name": "Manager" }'&lt;/span&gt;

curl -X PUT https://auth.canopy-io.com/api/v1/roles/&lt;span class="nv"&gt;$ROLE_ID&lt;/span&gt;/permissions \
  -H &lt;span class="s2"&gt;"X-API-Key: $CANOPY_API_KEY"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{ "permission_keys": ["orders.view", "orders.refund"] }'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now give it to someone. &lt;code&gt;POST /api/v1/assignments&lt;/code&gt; takes three ids: who, which role, and where:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"identity_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"7d3f9b2e-4c1a-4e8b-9f2d-1a6c5e0b8f34"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"role_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"b1c4a7e2-5d38-42f0-9c6b-3e8a1f7d0c95"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"node_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"e6b2d8f4-9a1c-4d7e-8b3f-2c5a9e1d6f48"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;code&gt;node_id&lt;/code&gt; is the Environment's root, and in a flat Environment it is the only node there is. Which is the point: the column that will later carry your customers' structure is already in the schema, holding a constant.&lt;/p&gt;

&lt;p&gt;Finally, ask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -X POST https://auth.canopy-io.com/api/v1/permissions/evaluate \
  -H &lt;span class="s2"&gt;"X-API-Key: $CANOPY_API_KEY"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{
        "identity_id": "7d3f9b2e-4c1a-4e8b-9f2d-1a6c5e0b8f34",
        "permission": "orders.refund",
        "scope": "app_wide"
      }'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;scope&lt;/code&gt; is required, and &lt;code&gt;app_wide&lt;/code&gt; is the flat question: does this identity hold this permission anywhere in the Environment. The answer says so, and says what it was not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"allowed"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"permission"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"orders.refund"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"scope_evaluated"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"app_wide"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"effective_node_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"granting_roles"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;effective_node_id&lt;/code&gt; is null and &lt;code&gt;granting_roles&lt;/code&gt; is empty because an &lt;code&gt;app_wide&lt;/code&gt; answer is not anchored to a node. Both fill in once you ask a narrower question, which is the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  The day flat stops working
&lt;/h2&gt;

&lt;p&gt;It arrives as a sales conversation, not a bug report. A customer with forty stores wants a regional manager who can refund in the twelve stores in their region and nowhere else.&lt;/p&gt;

&lt;p&gt;With flat roles there are two ways out, and both are bad. You bake the place into the role name, &lt;code&gt;manager-west&lt;/code&gt;, &lt;code&gt;manager-east&lt;/code&gt;, and now your role catalog grows with your customers' real estate, and every role means something different per tenant. Or you assign &lt;code&gt;Manager&lt;/code&gt; separately at each of the twelve stores, which works until the thirteenth store opens with nobody able to refund in it, and nobody notices for a week.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What the place-in-the-name approach stores&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"manager-west"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What Canopy stores&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"manager"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"region/west"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second shape is the one that survives a reorganization. The role says what someone does. The node says where. Moving a branch between regions changes one of those and not the other.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turning on the hierarchy
&lt;/h2&gt;

&lt;p&gt;Switching an Environment to hierarchical access control is a wizard in the Developer Console, under &lt;strong&gt;Access Control → Overview&lt;/strong&gt;, where the two models sit side by side as cards. &lt;strong&gt;Enable Hierarchy&lt;/strong&gt; asks you for your levels, top down, and nothing else: &lt;code&gt;Region&lt;/code&gt;, &lt;code&gt;District&lt;/code&gt;, &lt;code&gt;Store&lt;/code&gt;. It reviews them against a root, and then drops you on the Hierarchy page with a tree holding one node.&lt;/p&gt;

&lt;p&gt;Those levels become the Environment's &lt;code&gt;hierarchy_schema&lt;/code&gt;, which you can also write directly if you provision from code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"node_types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"application"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"region"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"district"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"store"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"allowed_children"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"application"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"region"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"district"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"store"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"region"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"district"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"store"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"district"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"store"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"store"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"max_depth"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"root_node_type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things to read off that. The root tier is prepended for you: your levels hang beneath the Environment's root node, which is typed &lt;code&gt;application&lt;/code&gt; and named after your Application. And the wizard is permissive by default, letting each tier contain any tier below it, so a store may sit directly under a region when a region has no districts in it. If your structure is stricter than that, write &lt;code&gt;allowed_children&lt;/code&gt; yourself and map &lt;code&gt;region&lt;/code&gt; to &lt;code&gt;["district"]&lt;/code&gt; alone.&lt;/p&gt;

&lt;p&gt;Either way the schema is enforced, not advisory. Canopy validates it on every node create and every node move, and &lt;code&gt;max_depth&lt;/code&gt;, which runs from 1 to 16 and counts the root tier, stops a tree growing past the shape you declared. A type mapped to an empty list is a leaf and takes no children.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The schema is a prerequisite, not an afterthought. Define it before your first node, or &lt;code&gt;POST /api/v1/nodes&lt;/code&gt; refuses with a 400 and tells you the hierarchy schema is not configured.&lt;/p&gt;

&lt;p&gt;This walkthrough assumes one tree per Environment, which is the shape to start with. If your tenants are whole companies rather than branches of one, Canopy has a second container, organizations, where each customer is the top of a tree of their own and the schema is rooted there instead. The ideas below are the same; the root tier is not.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The nodes themselves are ordinary API resources, or a drag-and-drop tree in the Console. The two are different routes, the public API and the Console's own, over the same operations underneath, so a tree built by hand and a tree built from a provisioning script are the same tree. What matters is what an assignment means once they exist.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzoo0gk4h3dg775sthrpy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzoo0gk4h3dg775sthrpy.png" alt="On the left, a flat Environment: three identities all assigned at one root node. On the right, a hierarchy: one assignment at North Region, inherited by every district and store beneath it, while South Region is untouched" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Flat is a tree with one node. Hierarchy is the same assignment row, pointed somewhere narrower.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A role assigned at a node applies at that node and every node beneath it. Assign &lt;code&gt;Manager&lt;/code&gt; to Alice at &lt;code&gt;North Region&lt;/code&gt; and she holds it in all twelve stores, and in the thirteenth on the day it is created, because nothing about it was enumerated. Canopy resolves this at evaluate time by walking from the node in question up to the root, collecting every assignment along the way and unioning the permissions of the roles that are active.&lt;/p&gt;

&lt;p&gt;Be precise about what the switch itself did, because it is easy to oversell. Enabling the hierarchy writes the schema and makes sure a root node exists. It does not touch a single assignment. Everyone who held a role at the root still holds it at the root, and a role at the root still reaches every node you go on to create, so nobody loses access and there is no migration step to run or to roll back.&lt;/p&gt;

&lt;p&gt;What it does not do is narrow anyone for you, and it cannot. Alice was granted &lt;code&gt;Manager&lt;/code&gt; across the whole Environment, and Canopy has no way of knowing that she should now hold it in the North region alone. Scoping her down is a deliberate act: assign &lt;code&gt;Manager&lt;/code&gt; at &lt;code&gt;North Region&lt;/code&gt;, then remove the assignment at the root. Do that for the people whose authority should become regional, and leave the ones whose authority really is env-wide exactly where they are.&lt;/p&gt;

&lt;p&gt;That is the honest shape of the move. The structure costs you a wizard; re-pointing the people who should be narrower costs you one assignment each, once.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changes in your application
&lt;/h2&gt;

&lt;p&gt;One line changes, and one is added.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flat&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"identity_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"7d3f9b2e…"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"permission"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"orders.refund"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"scope"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"app_wide"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Hierarchical&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"identity_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"7d3f9b2e…"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"permission"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"orders.refund"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"scope"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"node_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"9c2f71a4…"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;scope: "node"&lt;/code&gt; is the real question, and it is the one your guarded routes should ask: may this person refund &lt;em&gt;this&lt;/em&gt; order, which belongs to &lt;em&gt;that&lt;/em&gt; store. The answer now carries the node it was anchored to and the roles that produced it, which is what you want in a log when someone asks why a refund went through.&lt;/p&gt;

&lt;p&gt;Keep &lt;code&gt;app_wide&lt;/code&gt; for the loose question it is good at, hiding a menu item for someone who holds a permission nowhere at all. It is the wrong guard for a resource that belongs to a node, because it answers "somewhere" and the resource lives "here".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Caution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app_wide&lt;/code&gt; on a hierarchical Environment will happily tell you that a store manager in Ohio holds &lt;code&gt;orders.refund&lt;/code&gt;. They do, in Ohio. Guard the refund with &lt;code&gt;scope: "node"&lt;/code&gt; and the order's node id.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What the tree makes possible
&lt;/h2&gt;

&lt;p&gt;Some features only have a meaning once there is structure, and they are the reason the move is worth making beyond the one customer who asked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lists, not just checks.&lt;/strong&gt; The first real query in a hierarchical app is not "may they do this here", it is "show me everything they may see". &lt;code&gt;GET /api/v1/identities/{id}/accessible-nodes?permission=orders.view&lt;/code&gt; returns the grant roots, the nodes beneath them, and &lt;code&gt;env_wide&lt;/code&gt; when nothing needs filtering at all. Feed the nodes to your own &lt;code&gt;IN (...)&lt;/code&gt; and the list is scoped without a per-row check. Past &lt;code&gt;max_nodes&lt;/code&gt;, ten thousand by default, the set is not sent: &lt;code&gt;complete&lt;/code&gt; comes back false and &lt;code&gt;nodes&lt;/code&gt; null, and you filter on the roots against your own tree instead, minus the &lt;code&gt;excluded_roots&lt;/code&gt; a deny carves out. Worth handling on the first write rather than the first large tenant.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Denies.&lt;/strong&gt; A deny refuses a permission at a node and everything under it, whatever any role grants, and no assignment lower down undoes it. Denies are checked before grants, and the decision comes back with the &lt;code&gt;deny_id&lt;/code&gt; that caused it. This is how you suspend one branch during an investigation without unpicking anyone's roles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Delegation your customers can run themselves.&lt;/strong&gt; Once authority is attached to places, you can hand a customer's own admin the part of the tree that is theirs. Canopy holds them to a subset rule: an identity may grant a role at a node only if it already holds, at that node or above, every permission that role carries. Nothing to configure, so there is no setting to get wrong. The bound is on identities delegating to each other; your own Console users and API keys are not held to it, and neither is a customer's own top administrator inside their organization, who would otherwise be unable to staff it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An answer to "why".&lt;/strong&gt; The Console's Explain Access takes a person, a node and a permission and shows the verdict with the inheritance path behind it, and a link that reopens the same question. Hierarchical access is harder to reason about in your head than a flat list, so the ability to point at the exact assignment that allowed something stops being a nicety.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Time-bound grants.&lt;/strong&gt; Every assignment carries an optional &lt;code&gt;effective_from&lt;/code&gt; and &lt;code&gt;effective_to&lt;/code&gt;. A scheduled assignment grants nothing until its start, an expired one stops at its end and stays in the audit trail, and a sweep running every minute announces each boundary as an &lt;code&gt;assignment.activated&lt;/code&gt; or &lt;code&gt;assignment.expired&lt;/code&gt; event, so your cache can react without polling. The evaluator does not wait for it: a window that has closed stops granting whether or not the event has gone out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checks run in your process
&lt;/h2&gt;

&lt;p&gt;A guarded route calling an authorization service on every request is a latency budget nobody wants to spend. &lt;a href="https://canopy-io.com/docs/sdks-and-libraries" rel="noopener noreferrer"&gt;Canopy's SDKs&lt;/a&gt; don't, and the mechanism is worth understanding whether or not you use one, because it belongs to the API rather than to any particular client.&lt;/p&gt;

&lt;p&gt;The SDK fetches two things and keeps them: an identity's grant roots, meaning each permission it holds and the nodes it was granted at, and a node-to-parent map of your hierarchy. A check is then a walk up the tree in your own memory. One call per identity per 60 seconds replaces one call per request.&lt;/p&gt;

&lt;p&gt;What keeps it small is that a grant already means &lt;em&gt;this node and everything beneath it&lt;/em&gt;, so the grant roots are never expanded into descendants. The hierarchy costs about 90 bytes per node, one id and one map entry: a 50,000-node tenant is 4.6 MB, measured, which is small beside what an idle server process holds before it has done anything, and it is shared across every identity your server handles rather than held per user.&lt;/p&gt;

&lt;p&gt;The trade is honest and worth stating plainly: with the default window, an access change takes effect within 60 seconds, sometimes sooner and never later. After you revoke someone's access, they can act for up to a minute. If that is too long, don't shorten the window below the gap between a user's clicks, because then every request refetches and you have paid for a cache that never hits. Clear it instead, by handing verified webhook deliveries to the SDK or invalidating an identity from your own backend when you change their access.&lt;/p&gt;

&lt;p&gt;None of this is SDK-only. Both endpoints are ordinary HTTP, the responses carry a &lt;code&gt;model_version&lt;/code&gt; so a client refuses rows whose meaning has changed rather than guessing at them, and Canopy publishes the authorization conformance cases its own suite replays, so you can hold your implementation to the same ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Starting flat is not a shortcut
&lt;/h2&gt;

&lt;p&gt;The reason to begin flat is not that it is easier to set up. It is that on day one you do not know what shape your customers' organizations are, and inventing one before a customer has asked for it is how products end up with a tree that nobody's business actually matches.&lt;/p&gt;

&lt;p&gt;So begin with the three roles. Keep the thing that makes the move cheap later: a permission catalog that names actions rather than places, roles that describe jobs rather than regions, and a &lt;code&gt;node_id&lt;/code&gt; on every assignment that happens to point at the root today. The day a customer asks for regional managers, you define the levels, build the tree, and move the assignments that should move. Nothing is rewritten, because nothing was built around the absence of structure.&lt;/p&gt;

&lt;p&gt;And if you turn it on and get the shape wrong, an Environment reverts to flat too: every surviving assignment consolidates back to the root, and the response tells you exactly how many moved, merged and were dropped. The one thing that stops a revert is an administrator still scoped to a node inside it, since deleting that node would take their authority away without saying so; re-scope them and it goes through. Model the tree in &lt;code&gt;Development&lt;/code&gt; first, where the access model is set per Environment, and leave &lt;code&gt;Production&lt;/code&gt; flat until you are sure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://canopy-io.com/docs/hierarchy/quick-start" class="crayons-btn crayons-btn--primary" rel="noopener noreferrer"&gt;Read the hierarchy guide&lt;/a&gt;
&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>architecture</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
