create account

DLUX dApp Security Deep Dive by disregardfiat

View this thread on: hive.blogpeakd.comecency.com
· @disregardfiat ·
$125.53
DLUX dApp Security Deep Dive
# Security Through Transparency
## dApps on IPFS


![dApp Card on dlux.io](https://files.peakd.com/file/peakd-hive/disregardfiat/23tRvMrX4xVmZiU9GsgYQ1eAYKjrwvbJEiQiFPUy1kNp7wJUtuzNnA2FrVpMy1uTFvzDM.png)


Anybody who watches [The Lock Picking Lawyer](https://www.youtube.com/c/lockpickinglawyer) knows that traditional security is provided mostly through obscurity, as he demonstrates with nearly every video that this kind of "security" is no security at all. 

I've just put the last checks together that I believe makes the dlux dApp distribution scheme as secure as any traditional website. Let's talk about how this works, pose a challenge, and invite some commentary.

## Arbitrary Code Execution

The real name of the game is executing arbitrary code in a certain context. Traditional websites have had this exploit thru all sorts of means and if you decide to build a dApp on DLUX you'll have the same expectations of knowledge as any normal website developer. Things that we can't control are obviously out of bounds, such as setting up a phishing website and getting a user to click a link in an email. However, the way dlux dApps are set up it was possible to to send somebody a link, get them to load a page inside of the same sandbox as usual... which would mean exposing that dApps data through cookies, localStorage, and even sessionStorage in some cases. Depending on the nature of the dApp this could mean leaking valuable information. 

## DLUX dApp Paradigm

In a few words DLUX dApps should be roughly identical to setting up your own static website, on your own server, with your own SSL certificates. There are numerous ways to accomplish the same thing on today's internet such as github pages. The major differences here are instead of free hosting, Hive has content rewards. Posting an app on github pages like [The SPK Network Monitor](https://hiveuprss.github.io/spkccmonitor/) won't earn you any cryptocurrency and has the possibility of being censored or deleted by Github. 

[DLUX | Language classes for Ukrainian refugees in VR](https://bezkresu.ipfs.dlux.io/?undefined&hash=QmNby3SMAAa9hBVHvdkKvvTqs7ssK4nYa2jBdZkxqmRc16&author=bezkresu&permlink=language-classes-for-ukrainian-refugees-vr) is a very simple dApp that just displays some 360 images. If I wanted to censor this dApp I could, but the goal of decentralization is to have multiple people run multiple frontends, or even have a local application that can deliver this content no matter the whims of certain individuals.

I hope this paradigm meets or exceeds any other UX for both the developer and the user in terms of speed, trust, security, and usability.

### Breaking Down DLUX Security

Using the above dApp; looking at the domain you will find bezkresu.ipfs.dlux.io. It's set up in such a way that only @bezkresu can post dApps that will run from this domain. Let's find out how this works. 

Clicking on this app from dlux.io will generate this link:

https://www.dlux.io/dlux/@bezkresu/language-classes-for-ukrainian-refugees-vr

#### Links to dlux.io
This link will likely benefit from a HEAD request to enable link previews. GET requests will have dlux.io will serve this static [file](https://github.com/dluxio/dlux-iov/blob/main/dlux/index.html) with the following interesting code.

```js
const author = window.location.pathname.split('/')[2].replace('@', '')
const permlink = window.location.pathname.split('/')[3]
fetch("https://api.hive.blog")
  .then((r) => r.json())
  .then(res => {
    stateObj = res.result
    metadata = stateObj.json_metadata
    hashy = JSON.parse(metadata).vrHash,
    vars = `?${location.href.split('?')[1]}` || `?`
    //...
    function match (s,t) {var a=[];for(var i=0;i<s.length;i++){j=s.indexOf(t,i);if(j>=0){a.push(j);i=j}else return a}}
     subauthor = match(author,'.').length ? match(author,'.').join('') + author.replace('.', '-') : author
     ipfsdomain = `https://${subauthor}.ipfs.dlux.io`;
     location.href = ipfsdomain + `${vars}&hash=${hashy}&author=${author}&permlink=${permlink}&user=${user}`
```

It forwards the request to an IPFS enabled subdomain. Probably the hardest thing to understand here is Hive Accounts can have a . in them which would make this subdomain 2 or more subdomains. If @your.app account created a dApp, @your-app could be made to post a dApp that could access @your.app's subdomain. Since hive accounts can't start with a number, this will be used as a place to index where .'s are replaced with -'s. your-app and 4your-app in this case. 

#### Managing an iFrame

Our IPFS server only has one file to serve. This file checks some signatures indirectly and puts the dApp in an iFrame. Let's see how this works.

```
dns01 = window.location.hostname.split('.')[0]
dots = match(author,'.')
authorizedDNS01 = dots.length ? dots.join('') + author.replaceAll('.', '-') : author
if(dns01 != authorizedDNS01){
  goAhead = false
  alert(`This dApp failed it's signature check.\nYou are likely following a malicious link:\nAuthor: ${dns01} 
    != subdomain: ${authorizedDNS01}\n Please report')
}
```
The match function is the same as above. It does it's own check to see if it's on an authorized subdomain before asking a Hive API for the post content. The user following a link to an unqualified domain will get a warning message and no iFrame will be set up.

![Warning](https://files.peakd.com/file/peakd-hive/disregardfiat/Eqqvm2VzyWEKjUMEnZ74DwqA1QGce6Z4LbmZy8UDEq67VsRnUuKQ3GvCKeCpLmBbo5f.png)

#### Caddy Configuration

Finally, to serve anything out of our IPFS's subdomain gateway we've configured [Caddy](https://caddyserver.com/) as follows. 
```
*.ipfs.dlux.io {
        root * /var/www/html/ipfs
        file_server
        @ipfs {
                header Referer https://{labels.3}.ipfs.dlux.io*
        }
        handle @ipfs {
                reverse_proxy /ipfs/* localhost:8080
        }
        tls {
                dns cloudflare {api-key}
        }
}
```
`*.ipfs.dlux.io` handles our wildcard subdomain.
`file_server` serves our one and only file that checks subdomains, and set's up the iFrame sandbox
`@ipfs` defines a rule where the referer matches the current subdomain
`handle @ipfs` forwards ipfs/CIDs to the IPFS instance to load out the dApp.
`tls` give Caddy the information it needs to keep our SSL certs up to date.

### Capture the Flag

I've put a 'secret' in my localStorage.

![image.png](https://files.peakd.com/file/peakd-hive/disregardfiat/23wfmxBxLZG7V7B5EUWySMZugC6w5xj2PvwtsDhjfZ2jnPpAdv1vB7sn3tap2fbDZteqm.png)

It's sha256 hash is as follows: `16f20aed9b0f3a773f890c54936ed60df1c9d225723e2b9b7c89f30cd1bb3146`

I'll click on any link posted below. If you can get my secret from my localStorage I'd love to know how. So much in fact that I'll offer a 50,000 DLUX bounty (or 500 Hive).

If you can think of improvements, I want to know. If you have questions, ask them. 

I hope that our sandbox is just as secure as any other website. That phishing out of a non-managed url is the best an attacker can do... and the sandbox only executes code that the author wrote. 

As always, a vote for my witness or approval of [proposal 234](https://peakd.com/proposals/234) helps me out.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 259 others
👎  , ,
properties (23)
authordisregardfiat
permlinkdlux-dapp-security-deep-dive
categoryhive
json_metadata"{"app":"peakd/2022.10.3","format":"markdown","description":"Wth a Capture the Flag challenge.","tags":["hive","dlux","ipfs","dev","security","dapp","defi"],"users":["bezkresu","your.app","your-app","ipfs"],"image":["https://files.peakd.com/file/peakd-hive/disregardfiat/23tRvMrX4xVmZiU9GsgYQ1eAYKjrwvbJEiQiFPUy1kNp7wJUtuzNnA2FrVpMy1uTFvzDM.png","https://files.peakd.com/file/peakd-hive/disregardfiat/Eqqvm2VzyWEKjUMEnZ74DwqA1QGce6Z4LbmZy8UDEq67VsRnUuKQ3GvCKeCpLmBbo5f.png","https://files.peakd.com/file/peakd-hive/disregardfiat/23wfmxBxLZG7V7B5EUWySMZugC6w5xj2PvwtsDhjfZ2jnPpAdv1vB7sn3tap2fbDZteqm.png"]}"
created2022-10-27 05:02:36
last_update2022-10-27 05:02:36
depth0
children7
last_payout2022-11-03 05:02:36
cashout_time1969-12-31 23:59:59
total_payout_value62.806 HBD
curator_payout_value62.727 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,197
author_reputation348,256,954,150,586
root_title"DLUX dApp Security Deep Dive"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id117,817,663
net_rshares202,902,558,593,701
author_curate_reward""
vote details (326)
@bezkresu ·
BTW, I have had a problem with loading photos for several months. I do not know why. I drag and drop and nothing happens (conditions are met, 2:1 max 5MB 4096x2048).
properties (22)
authorbezkresu
permlinkre-disregardfiat-20221028t1345664z
categoryhive
json_metadata{"tags":["hive","dlux","ipfs","dev","security","dapp","defi"],"app":"ecency/3.0.28-vision","format":"markdown+html"}
created2022-10-27 23:34:57
last_update2022-10-27 23:34:57
depth1
children2
last_payout2022-11-03 23:34:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length165
author_reputation46,757,822,662,314
root_title"DLUX dApp Security Deep Dive"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id117,839,744
net_rshares0
@disregardfiat ·
$0.02
the public services we were using decided to not continue, so we're switch over to SPK network for file storage... which means we gotta build all this out ourselves. Hoping to have uploads back online in a week or two. 
👍  ,
properties (23)
authordisregardfiat
permlinkre-bezkresu-rkfs08
categoryhive
json_metadata{"tags":["hive"],"app":"peakd/2022.10.3"}
created2022-10-28 00:00:09
last_update2022-10-28 00:00:09
depth2
children1
last_payout2022-11-04 00:00:09
cashout_time1969-12-31 23:59:59
total_payout_value0.012 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length219
author_reputation348,256,954,150,586
root_title"DLUX dApp Security Deep Dive"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id117,840,351
net_rshares41,764,415,771
author_curate_reward""
vote details (2)
@bezkresu ·
Good luck!
properties (22)
authorbezkresu
permlinkre-disregardfiat-20221029t2236770z
categoryhive
json_metadata{"tags":["hive"],"app":"ecency/3.0.34-mobile","format":"markdown+html"}
created2022-10-29 00:23:06
last_update2022-10-29 00:23:06
depth3
children0
last_payout2022-11-05 00:23:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation46,757,822,662,314
root_title"DLUX dApp Security Deep Dive"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id117,870,067
net_rshares0
@borislavzlatanov ·
Thanks for writing up on the workings of dlux. 

How is it determined which links become a part of the list of authorized subdomains? Each dapp would submit a request to the owner of dlux.io to be included in the authorized list?

Can dapps use this technology and still be served from their own domain? Or do they have to served as subdomains of dlux.io?
properties (22)
authorborislavzlatanov
permlinkre-disregardfiat-rll9sz
categoryhive
json_metadata{"tags":["hive"],"app":"peakd/2022.11.1"}
created2022-11-19 09:46:12
last_update2022-11-19 09:46:12
depth1
children0
last_payout2022-11-26 09:46:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length355
author_reputation23,569,043,849,875
root_title"DLUX dApp Security Deep Dive"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id118,451,731
net_rshares0
@hivebuzz ·
Congratulations @disregardfiat! You received a personal badge!

<table><tr><td>https://images.hive.blog/70x70/http://hivebuzz.me/badges/pud.png?202211011251</td><td>You powered-up at least 10 HIVE on Hive Power Up Day!<br>Wait until the end of Power Up Day to find out the size of your Power-Bee.<br>May the Hive Power be with you!
</td></tr></table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@disregardfiat) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>


**Check out the last post from @hivebuzz:**
<table><tr><td><a href="/hive-122221/@hivebuzz/pum-202211"><img src="https://images.hive.blog/64x128/https://i.imgur.com/M9RD8KS.png"></a></td><td><a href="/hive-122221/@hivebuzz/pum-202211">Be ready for the 11th edition of the Hive Power Up Month!</a></td></tr><tr><td><a href="/halloween/@hivebuzz/halloween-2022"><img src="https://images.hive.blog/64x128/https://i.imgur.com/jeLRU6l.png"></a></td><td><a href="/halloween/@hivebuzz/halloween-2022">Trick or Treat - Share your scariest story and win your Halloween badge</a></td></tr><tr><td><a href="/hive-122221/@hivebuzz/pud-202211"><img src="https://images.hive.blog/64x128/https://i.imgur.com/805FIIt.jpg"></a></td><td><a href="/hive-122221/@hivebuzz/pud-202211">Hive Power Up Day - November 1st 2022</a></td></tr></table>
properties (22)
authorhivebuzz
permlinknotify-disregardfiat-20221101t132319
categoryhive
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2022-11-01 13:23:18
last_update2022-11-01 13:23:18
depth1
children0
last_payout2022-11-08 13:23:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,344
author_reputation370,085,249,338,367
root_title"DLUX dApp Security Deep Dive"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id117,969,151
net_rshares0
@malos10 ·
Great!! keep improving the app for everyone!

 i give you my witness vote to help you !
properties (22)
authormalos10
permlinkre-disregardfiat-20221027t133615552z
categoryhive
json_metadata{"tags":["hive","dlux","ipfs","dev","security","dapp","defi"],"app":"ecency/3.0.28-vision","format":"markdown+html"}
created2022-10-27 17:36:18
last_update2022-10-27 17:36:18
depth1
children0
last_payout2022-11-03 17:36:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length87
author_reputation677,191,552,355,945
root_title"DLUX dApp Security Deep Dive"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id117,832,285
net_rshares0
@urun ·
![untitled.gif](https://media.tenor.com/5zR-Pz5VOeAAAAAC/jesse-cox-omfgcata.gif)
properties (22)
authorurun
permlinkre-disregardfiat-rki1xb
categoryhive
json_metadata{"tags":["hive"],"app":"peakd/2022.10.3"}
created2022-10-29 05:29:36
last_update2022-10-29 05:29:36
depth1
children0
last_payout2022-11-05 05:29:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length80
author_reputation94,129,224,984,132
root_title"DLUX dApp Security Deep Dive"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id117,874,395
net_rshares0