[BUIDL Asia 2023 Builder Stage] Building a secure in-browser cryptocurrency wallet

BUIDL Asia 2023 · 3 Jul 2023 · 25:40

Yan Zhu, Chief Security Officer at Brave, explains how to build a more secure in-browser cryptocurrency wallet. The talk covers Brave's privacy features and the Basic Attention Token, the browser sandboxing model and why Electron-based wallets such as Mist were risky, and the design of Brave Wallet. It then explains how window.ethereum is exposed to web pages, the EIP restricting injection to secure contexts and sandboxed iframes, prototype pollution risks, and an EIP for wallets to announce themselves when several are installed.

Speakers

Topics

From BUIDL Asia 2023, hosted by Kryptoplanet.

Transcript

thank you so uh none of this will be more complicated than the mirroring Mac OS setup process we just went through so my name's Jan I work at Brave and today I'm here to talk to you about building more secure cryptocurrency wallets so how many people have heard of Brave already hopefully okay okay most of you that's better than zero for those of you who don't know we're an open source and privacy focused web browser we launched in 2016 and I've been the security Chief at Brave since 2015.

and these days we also have a search engine and a VPN so you might ask why would someone use Brave over chrome or Firefox well we're one of the few browsers that has a default of privacy of blocking ads and trackers we're fully open source based on Chrome we also have support for Tor and VPN for better Network privacy uh and one of the coolest things we do is block fingerprinting which is a way that websites can track users without cookies so if there's no cookies then websites might try to infer details about a user's uh graphics card or CPU

or audio driver in order to build a fingerprint for them that they can use to track them across the web so as far as I know we're one of the only browsers that tries to do that kind of detection of fingerprinting and block it and at this point most people ask so how can we support the web without ads because that is how most websites make money and our answer to this is the bat which is the basic attention token so this is a erc20 token we did an Ico in 2017.

and the way this is supposed to work is that advertisers buy ads with brave and then a part of these ad these this ad revenue is used is given to users who earn the basic attention token and then when the users earn the token they can use it to reward creators and then the creators earn the token so we call this Brave rewards uh in other words Brave users get the tokens from looking at ads and to be clear the ads are completely optional so if you don't want to see ads that's up to you there will be no ads but if you do see ads our reasoning is

that you should get part of the money that the advertiser is paying Brave so you actually earn these tokens and then you can use them to pay creators to make up for the revenue that they're losing by us blocking ads by default so the the bat is actually a kept in a one-way wallet which means that once it's in Brave it can't just be cashed out in fact the only way that users can use the token is to pay creators and this is to reduce the risk of fraud by making it a little bit less fungible so users can spend that essentially by tipping creators or by Auto contribute and creators can sign up at creators.brave.com to verify their own a

website or a YouTube channel and then they can earn the token and then the ads appear as a system notification and what's Innovative about this is that the matching happens client side so there's no data that's leaked to Advertiser all the all the inference happens using local machine learning so if you are interested in hotels in Korea the browser knows that but there's no reason the advertiser has to know it so that that's in for locally you see the ad locally and then no data is sent to the advertiser until you click the ad so this is actually all um guaranteed using a zero knowledge proof protocol on top of the attention token so these are

actually like a form of virtual token that has better privacy than like on-chain erc20 tokens so essentially we can prove to Brave that the user has seen an advertisement without revealing what advertisements the user has seen so this is very cool because we can prove that the user should be earning money for the ads without getting any more information about what kinds of you know hotels or cars or anything that they're interested in but the focus of my talk today is actually Brave wallet which is a completely separate product and this was started in 2018 when I attended East Denver and we decided to build a wallet inside

Brave a built-in um and many people asked us why why did you build another ethereum wallet there were many ethereum wallets in 2018 but actually if you think back to them there weren't that many that were integrated into the browser because like dapps weren't as as big of a thing um and the wallet that I think was most used at the time was missed which was created by the ethereum foundation and we actually identified some like pretty big security issues with the design of nist so to explain what the security problems were I have to talk about these security architecture of Chrome so Mist was based on a platform called electron which was based on Chrome and

Chrome has a a principle of security sandboxing so essentially what this means is that there are processes that run at a high privilege level sandboxed from processes that run on a low privilege level and these processes have separate memory spaces but they can talk to each other each other through inter-process communication channels the things that run at the browser process level or the browser kernel are the like most security important parts of the browser if you think about you know if someone hacks your browser what is the worst thing they can get the worst would be cookies right like imagine if they got your cookies to coinbase they could log into your

coinbase account immediately if they got your passwords they could also do that uh things that deal with data stored on disk that's all in the high privilege sandbox process on the other hand the low privilege process that's called the renderer process that deals with untrusted data such as parsing HTML decoding images interpreting JavaScript because when you go to a web page um you know users visit all kinds of untrusted web page the browser has to parse the HTML it has to you know have entire like PNG SVG jpeg decoders all of those are very complex grammars and uh of it's very easy relatively easy to

find vulnerabilities in in those because they're so complicated and the idea is that if I have a vulnerability in the PNG decoder that leads to a buffer over overflow in the browser I should not be able to get your passwords in fact I should only be able to compromise the single tab that you're visiting so that that's the idea of sandboxing however the problem with electron which is what Mist was based on was that everything ran out the same privilege level there was no sandboxing so if you got a exploit in the PNG decoder you automatically could just get access to the browser passwords or the wallet private keys or anything at high privilege

so yeah so of course missed which was based on electron had to have access to the private keys right because it was an ethereum wallet it was furthermore it was based on a version of Chrome that was over a year out of date and chrome um you know they published known exploits after about 12 weeks right so at at a year there's definitely like known memory corruption vulnerabilities you could use and without sandboxing it's very easy to go from just an exploit in HTML or an image decoder to just full compromise of the wallet with private keys and they um I think around 2018 they actually issued a security advisory about this exact thing that I'm talking about

so but the point is we did not win the hackathon so we didn't get any prizes and and so uh people forgot about our project until in 2021 we actually decided to just build it in A Brave and launch it officially as a brave wallet so right now we have about 250 000 monthly octaves and over 2 million wallets created so the design is pretty simple uh it's built into Brave it's running at a brave wallet and um what what's important to note here is that we have this implementation of window.ethereum which is injected into web pages so what what is window dot ethereum um

it's a very interesting topic because this is one of the only Bridges between websites and ethereum so this came about because of metamask because they started injecting a global JavaScript API into websites which allows user which allows websites to read ethereum accounts read data from blockchains and suggest the user sign messages and transactions if you've ever used crypto kitties you will know that window.etherium is how crypto kitties is able to access your metamask account and request you you know transfer funds buy kitties sell kitties Etc so that's all done through this bridge so uh previously I worked on the w3c

which was a organization that dealt with like JavaScript apis web apis um so when I started looking at window data theorem in Brave and when we were implementing it I realized like there were a lot of ways we could make this more secure so myself and my co-worker Kyle who is here and a few of our other co-workers wrote a new EIP called restrict ethereum provider API injection um the idea behind this is that so the the web has a concept of powerful apis so these are things like geolocation camera microphones so apis that are so powerful that they should only be accessible in Secure contacts

so what is a secure context um a context you can think of as an HTML environment or an eye so that could be a top level page or an iframe so in this example we have a top level page of localhost and then we have uh two iframes inside that and then another iframe inside one of those so that is total for total context because you count the top and then you count all of the iframes each of those is its own HTML document so what's a secure context well that's very simple so secure contacts is just a context that is https or localhost pretty much you can check if you're in a secure context in JavaScript using a window is secure contacts which will

return true and so our contribution here is we specified in the standard that window.ethium is a powerful API because it allows access to wallet private keys and so therefore it has to be undefined unless you are in a secure context so I'm just going to skip this example so you might ask what what's the point of iframes uh why are they so useful so let's imagine we're building a website that is a cryptocurrency exchange we'll call it binance and you want to embed a video about how to use cryptocurrency right so one way you could do this is you could just embed a script directly saying script Source equals example.com video.javascript and the problem with

that is that if there's any vulnerability in this third-party video player suddenly your entire cryptocurrency exchange is compromised because that JavaScript executes in the same context as the top level page but instead what you could do is you could put an iframe from example.com on your web page instead and now you have this nice browser provided security separation because iframe runs in a separate JavaScript context that has no access to the parents page and also it actually runs on a completely separate process so security vulnerability in the iframe will not compromise the the parent and actually you know to be to be

specific we added some more restrictions on my frame so we we also said window.etheoreum cannot be by default injected into third-party iframes these are iframes from a different origin than your parent and also if the iframe is first party and it contains the sandbox attribute then don't don't inject window and other theorem unless the sandbox says also allow same origin so I won't explain the full details of this I'm just mentioning it because the reason why we had to add these restrictions these additional restrictions was because of a website called openc that you might have heard of uh so openc um if you've used it you know it's for buying uh nfts right nfts

can actually come in a lot of formats they can include SVG so SVG is a very interesting format because it runs JavaScript and it runs HTML nfts can be entire web apps so that you know they can handle clicks they can use any JavaScript API so well that's a problem right because if enough if if this arbitrary web app called an nft is running inside openc then if anyone you know makes a malicious nft can it just compromise the entire openc website well the answer is no and the reason why is because openc uses iframes for sandboxing so this was just a page on openc that I looked at while I was on the flight here and this

this nft which wouldn't load because I was on a plane and I had very bad Wi-Fi as an SVG and you can see they're using an iframe to sandbox it right so that the SVG can load but it doesn't have access to all of the security context and on openc.com so yeah this is uh so yeah there's sandboxing that's using iframes and they're using usually using third-party iframes and all of the examples that I could find but someone told us that there's actually cases where they're using first party iframes which is very strange but anyway um because they might use first party iframes we have to say like oh if it's first party but it's also sandboxed then

we assume it's like a different security context so we shouldn't put the window dot ethereum uh object in it so so what is the attack scenario right like let me let me just bring it back to like why we even care about this let's say I'm a evil person and I create a malicious nft which is an SVG file which means it can include JavaScript and I can say ethereum.request and then I could request a transaction and then I upload the nft to openc Marketplace which anyone can do because it's an Open Marketplace and then when you look at my nft and you have you know metamask or Brave wallet installed it will show you a prompt asking you to sign a transaction now you will think probably

that that transaction is coming from open C which is a very trustworthy website but it's actually from my completely random you know third-party nft which you don't trust so yeah so basically if we assume if we look at iframes that are sandbox we just assume they're not trustworthy so the the another very interesting thing is this exact exploit actually happened on etherscan and on a few other high-profile ethereum websites but not because of nfts uh because of advertisements so the way that websites um ensure that advertisements don't compromise the entire website is using iframes as well so in this example you go to etherscan and there's an iframe at

the bottom from BC Dot Game you know some it's just some random advertisement and that advertisement calls window.ethereum.request and that opens metamask and if you're not very careful it will you will think you know that request is coming from etherscan but it's actually coming from this ad so that's very tricky and that opens the wallet up to like all kinds of phishing attacks so the last problem I want to talk about is that the fact that definitions of window.e theorem can be overwritten currently uh to explain this problem um I have to explain the security architecture of browser extensions so this works the same in almost every

browser these days um essentially there are three levels of script permission there is the background script which is high privilege the content script which runs in the browser tab which is medium and then there's injected scripts which run in the page which are lowest privilege so just keep in mind there's three contacts the ones we'll focus on our content script and injected script so content scripts run in this interesting thing called an isolated JavaScript World which means that even though they are injected into a Pages tab they don't have access to um well actually the page doesn't have access to

variables defined in the content script by default so you can overwrite this by either directly putting a script tag in the web page or you can just use this like main World parameter to execute things in the page so to Allah Spirit what this means uh let's make a very simple HTML web page so this is just a page with a single div that says hello and then let's also build a browser extension and so this is an extension content script which is going to run in an isolated world so in the content script we're just going to define a bunch of variables we're going to Define a variable called password we're going to Define window.etherium

and then we're going to actually go look at the div of the website and then change the value inside the div and then we're going to change some attributes on that HTML so what what is visible to the page okay so if we actually just load the page and then we go into the console we can look at what is injected by the extension and what actually is visible to the the JavaScript of the in the main world so first you can see is that window.e theorem is not defined remember we defined it in the second line here so that's not visible to the page and and then password is also undefined right so that variable is not not visible however you can see that the text was changed to

goodbye and that's visible to the page so if essentially what I'm saying is that if you take if you inject an extension content script it can change things on the page and the page will see that but any other variables pretty much are just undefined so the lesson here is that window.etherium can only be called by websites if it's injected into the main World which is not the default for extensions the problem is now um JavaScript on any web page can redefine window data theorem to be anything it wants and this creates an interesting type of attack called prototype pollution it's uh the vulnerability where an attacker can inject their own property definitions into trusted objects where they're not

supposed to be able to do that to illustrate prototype pollution let's say that we have a website and we Define a user object with two properties we have name and we have ADD is admin so we expose an API that allows users to update their name but we don't want them to update is admin because if they can do that then they can you know change other things that we don't want them to have permission to do so this code looks pretty reasonable on the back end so it look it basically checks if object.keys includes is admin and if so it just returns early and if not it's emerges the user update with the original object right so it does look

like it has a security issue probably not because we're chat we're filtering the is admin key so there's a way to get around this using prototype pollution let's say that my user name is Alice and is admin as false so I call update with my name Bob right and then Bob it works because Bob is a is a normal key and is admin is still false okay now I try to update is admin so let's try to elevate my permission to admin that doesn't work because remember in the original code we're checking if the key includes is admin and if so we just return early but what about this what if I say update and then I use the object underscore

underscore Proto underscore underscore is admin true that's very interesting because that works and now I've eliminated elevated myself to admin privileges just using a special object that you know looks very magical and strange well why does this work this works because JavaScript is a prototype based language so when um when you create an object in JavaScript there's a hidden prototype linked to the object and if an attribute doesn't explicitly exist on the object JavaScript will look at the Prototype right so even if you can't directly update an object if you can update the

Prototype then all objects that descend from that prototype will be polluted and they will have the bad property the attacker has defined so let's check okay is metamask prototype pollutable so the way I test this is I Define I I redefine array.push so in JavaScript array.push just adds an element to an array but I can Define that to be something else so instead I say you know a radar push instead we're just going to log polluted and then we're going to increment a variable and so what happens when I load this in my browser well if I load it without metamask nothing happens because nothing is calling array.prototype dot Push by default

if I load it with metamask installed I see all kinds of prototype pollution right like it's all being logged into console because metamask is calling it's an internal code is calling a radar push but that a radar push has been redefined by my web page to be anything I want so where is it to find a metamask it's defined in this code so it's registering a JS script called in page.js and that runs in the main World which means anything can override it so how do we fix this well the good solution would be able being able to freeze window.othereum so in JavaScript we have these ways of freezing objects so that future callers cannot uh cannot update them we can do a

window.freeze we can set writeable false configurable false but this is a problem because in Brave we learned that many users want to use both Brave wallet and another wallet so they would use Brave and then we would call you know window object.freezewindow.etherium and then they would have they would install metamask and metamask wouldn't work because their metamask is also trying to Define window.seum so there needs to be like cooperation between wallets you know for us to build wallets that do not have prototype pollution on window Dot ethereum so oh also it's interesting if multiple extensions are installed and they're all trying to define window.ethereum the

last one wins right so if you install Brave you install um and then you install metamask and then you install Phantom because you installed Phantom last Phantom's definition of window data ethereum is the one that the page will get so uh the solution to this is another EIP which again you know Kyle is as an author on so much you know a credit to my co-worker Kyle who's here and and the solution is very easy right it's just if these providers should have to announce themselves right we should Define these apis that allow them to say hey this is Phantom this is metamask where this is brave and this is my implementation of window.etherium and then on on the page the page should be

able to tell the user like which one would you like to use would you like to use Brave Phantom or uh you know or or metamask and then the user should pick and that should be the winning winning solution so that's that's how we should resolve that conflict anyway that's that's my entire talk so I thank you for your time and if you want to contact me you can use any of these channels thanks [Applause]