<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Brock Smedley on Medium]]></title>
        <description><![CDATA[Stories by Brock Smedley on Medium]]></description>
        <link>https://medium.com/@brocksmedley?source=rss-1807d0f2f534------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*x2_Fn-8QhilW4a8om_iOgg.jpeg</url>
            <title>Stories by Brock Smedley on Medium</title>
            <link>https://medium.com/@brocksmedley?source=rss-1807d0f2f534------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 21:11:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brocksmedley/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[WTF Rust: attribute proc macros]]></title>
            <link>https://brocksmedley.medium.com/wtf-rust-attribute-proc-macros-df2c078e7263?source=rss-1807d0f2f534------2</link>
            <guid isPermaLink="false">https://medium.com/p/df2c078e7263</guid>
            <category><![CDATA[middleware]]></category>
            <category><![CDATA[attr-macr]]></category>
            <category><![CDATA[proc-macro]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[auth]]></category>
            <dc:creator><![CDATA[Brock Smedley]]></dc:creator>
            <pubDate>Fri, 21 Jan 2022 20:38:30 GMT</pubDate>
            <atom:updated>2022-01-21T20:38:30.169Z</atom:updated>
            <content:encoded><![CDATA[<p>Rust proc macros are confusing. Hopefully this makes them easier to understand.</p><p>If you’re reading this I’m assuming that you already know why you need proc (procedural) macros. The specific type of proc macro I’m talking about here is <a href="https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros">attribute macros</a>.</p><p>Proc macros are awesome for implementing things like middleware and default implementations (as in the case of derive macros), but the documentation is kinda shit, and it’s very hard to find practical examples. With this article, I hope to shed some light on this confusing topic.</p><p>Here’s the situation I was dealing with: I’m writing an API using actix and I need to define access permissions based on the user’s “user type.” The users in my system have four types: “admin,” “shop,” “customer,” and “manager.” What they mean isn’t really important — the point is that some endpoints are only for admins, and customers shouldn’t be able to access them at all.</p><p>To achieve this, I decided to implement a proc attribute macro which would allow me to declare the user types that an endpoint would allow access to.</p><p>Like this:</p><pre>#[auth_route([&quot;admin&quot;])]<br>pub async fn get_bill_of_lading(...) -&gt; Result&lt;...&gt; {...}</pre><p>This route should only allow users of the “admin” type to access it.</p><p>The signature of my attribute proc macro looks like this:</p><pre>pub fn auth_route(attr: TokenStream, item: TokenStream) -&gt; TokenStream {...}</pre><p>attr contains the attributes supplied to the macro invocation (e.g. ([&quot;admin&quot;])), and item contains the original function signature and body.</p><p>This macro function serves to inject some code that pulls the current user session and runs some checks to see if that user is allowed to access the endpoint. The code it generates is defined in a quote! block, which when compiled, is scoped to the function in which it’s implemented. This means that variables defined in the actual proc macro function (auth_route in my case) are not accessible to the generated code.</p><p>So at first, I was trying to do this (WRONG):</p><pre>// adapted for readability -- don&#39;t bother compiling<br>let auth_check_stmt = quote!({<br>  if get_auth_types(attr).contains(get_session_user().user_type) {<br>    // access granted<br>  } else {<br>    // access denied<br>  }<br>});</pre><p>attr isn’t defined in the scope that the generated code lives in, so how do we use the information from attr to define whether our user will get access?</p><p>Put simply, the answer is match.</p><p>Instead of trying to use the attr variable in your injected code, <strong>generate new code based on the contents of </strong><strong>attr</strong> that performs checks against static values.</p><p>We need a variable auth_types to store the auth types in the injected code, then we need to push the auth types from attr into it at compile-time. This variable will then be used to check against the user’s type at runtime.</p><p>Note: there are two different auth_types variables: the one declared in the proc macro function which stores values from attr at compile-time, and the one declared in the injected code, that is filled with the auth types from the first auth_types variable at runtime (but the code to do so is created at compile-time).</p><p>Here’s what it looks like in code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UUPndAeAmEAMPDd-vBhd0w.png" /></figure><p>The first auth_types instance you see is an array containing the auth types pulled from the attr variable. The rest of the auth_types instances you see (inside the build_stmt! calls) are referring to the instance that lives in the scope of the endpoint; where our genereated code will live.</p><p>build_stmt! is just a simple macro I wrote to create a Stmt out of arbitrary code.</p><p>Notice that instead of using the attr variable in the generated code (which would <em>try to</em> run checks at runtime (and would not compile)), we’re creating static rules at compile-time, which will then be injected into the code and used at runtime. We push these statements into an array (auth_statements), which is then injected into our generated code just before the code that checks the user’s user type against the array of allowed user types.</p><p>The lesson is this: <strong>because </strong><strong>attr can’t be used at runtime, we have to use </strong><strong>attr to create code at compile-time to achieve our ends.</strong></p><p>Here’s what the injection part of the proc macro definition looks like in the end:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zr2iarZtlDnwU2yDOiP0Eg.png" /></figure><p>We simply inject our authentication code, which will return a 401 if the user isn’t allowed, or do nothing if they are, at the beginning of the endpoint’s code body. The code is injected in reverse order because we’re using insert(0, ...).</p><p>In effect, this code does the following at runtime:</p><ol><li>Declare the auth_types variable, which will be used to check against the user’s user type.</li><li>Push statically-compiled auth type strings into the auth_types variable.</li><li>Check the user’s user type against the auth_types array, return 401 if no match.</li></ol><p>Simple, right? 😅</p><p>Hopefully this helps some people. I spent days trying to figure this out. I understand that my examples were very specific and missing lots of details, so if you’d like me to provide any clarification, please drop a comment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df2c078e7263" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Wallets Are Broken; My Largest Whitehat Rescue]]></title>
            <link>https://brocksmedley.medium.com/wallets-are-broken-my-largest-whitehat-rescue-1893a2edfce4?source=rss-1807d0f2f534------2</link>
            <guid isPermaLink="false">https://medium.com/p/1893a2edfce4</guid>
            <category><![CDATA[whitehat]]></category>
            <category><![CDATA[flashbots]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[wallet]]></category>
            <dc:creator><![CDATA[Brock Smedley]]></dc:creator>
            <pubDate>Wed, 29 Sep 2021 02:06:46 GMT</pubDate>
            <atom:updated>2021-10-06T03:17:07.517Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8-WDxZgADWTFbilqWcQxPw.jpeg" /><figcaption>credit <a href="https://www.publicdomainpictures.net/en/view-image.php?image=370955&amp;picture=spooky-dark-forest">publicdomainpictures.net</a></figcaption></figure><p>Today I saved over $40,000 from a compromised Ethereum account — the largest rescue I’ve ever done. And though I’m thrilled to have helped this person recover their assets, I’m not very happy that it had to happen at all.</p><p>I’ve never leaked a private key to anyone, ever. I’m a software developer. I keep my private keys on encrypted flash drives because I’m too much of a cheapskate to buy a hardware wallet. I understand how public key encryption works, and how it’s implemented. I’m a nerd — big time.</p><p>I also have a keen sense of the difference between a legitimate website and a scam, or a legitimate company representative and a fake. I’ve spent hours on the phone playing with scammers just to waste their time and get myself a laugh.</p><p><strong>Most people are not like this, nor should they have to be.</strong></p><p>Most folks who are into crypto only know a couple things about their wallets:</p><ol><li>It’s a good idea to use a hardware wallet to store your valuable crypto stash safely.</li><li>You plug in the wallet and enter the PIN when you wanna do stuff.</li></ol><p>And that’s all you <em>should</em> need to know. Blockchain/cryptocurrency was meant to put financial power back into the peoples’ hands. The problem is that people make mistakes. With great power comes great responsibility, and I think people are being forced to bear too much of it.</p><p>It’s only natural for one to eventually neglect or forget about the gravity of maintaining personal control over their crypto assets. Today, you have to be vigilant like a doomsday prepper, understanding attack vectors and safe practices. Where to keep your wallet, when it’s appropriate to share your private key (which is <em>never</em>, unless you already did and you now need help). You have to keep your wits about you — like having a shotgun by the nightstand in case someone tries to break into your safe while you sleep.</p><p>Again, most people are not like this. Well, some people are but I wouldn’t assume that to be the majority mindset.</p><p>From their beginnings, banks served to relieve people of the burden of safely holding their money. People put their trust into a bank to physically (or digitally) hold their money in a safe place, and the federal government (in the States) insures every bank account up to $250,000, guaranteeing the customer that their money will be totally safe. With crypto assets, there is no such guarantee.</p><p>Today, I recovered over $40,000 worth of assets from a compromised Ethereum account. Prior to this, the victim (let’s call him Andy) had sent his secret seed phrase to someone in a crypto platform’s discord channel, who claimed to be a developer for the platform. As it turns out, the moderators of the server were issuing the “developer” tag to practically anyone who requested it.</p><p>This was a simple, honest mistake by Andy, but it resulted in devastating losses, in the hundreds of thousands of dollars. Andy thought he would be safe using a hardware wallet, but he made the mistake of sharing his seed phrase, which gave the attacker complete and total control of his accounts. Andy didn’t deserve this.</p><p>The point is that the current paradigm is broken. Users shouldn’t have to worry all the time about where their private keys are stored.</p><p>Current wallet implementations are more appropriate for super-users than the everyday credit card holder. Existing implementations work well, but only if you know and follow the rules. And I have a strong feeling that not everyone reads the rule book, nor will they in the future.</p><p>For now, keep your private keys safe. In the future, let’s hope that wallets get better; safer. People make mistakes. Can’t we let them?</p><p>If you accidentally leak your private key and need help recovering your assets, find me on twitter (<a href="https://twitter.com/zeroXbrock">@zeroXbrock</a>) or discord in the <a href="https://discord.gg/CXcjpGMh">#flashbots</a> server (brock#1337).</p><p><em>Edit: I did some research into safer wallets and realized that multi-party transaction signing would probably be the safest bet. </em><a href="https://zengo.com/security/"><em>ZenGo</em></a><em> offers a service that does this.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1893a2edfce4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>