|
| 1 | +# Secrets |
| 2 | + |
| 3 | +When MagicMirror² was designed, no one thought about secrets. |
| 4 | + |
| 5 | +However, there are plenty of parameters worth protecting, such as: |
| 6 | + |
| 7 | +- API keys (e.g. weather) |
| 8 | +- Tokens in URLs |
| 9 | +- Private calendar URLs |
| 10 | +- Position information (latitude and longitude) |
| 11 | +- ... |
| 12 | + |
| 13 | +::: warning NOTE |
| 14 | + |
| 15 | +Due to its design, this data is also displayed in the web browser, for example, |
| 16 | +in the `/config` subpath. |
| 17 | + |
| 18 | +Therefore, be careful when sharing your MagicMirror² website with others. Anyone |
| 19 | +with access to the site can also access the secrets. |
| 20 | + |
| 21 | +::: |
| 22 | + |
| 23 | +Beginning with MagicMirror² `v2.35.0` we offer beta support for secrets. |
| 24 | + |
| 25 | +This is based on |
| 26 | +[environment variables inside the configuration file](/configuration/introduction.html#environment-variables-inside-the-configuration-file). |
| 27 | + |
| 28 | +::: warning NOTE |
| 29 | + |
| 30 | +"Beta" means we offer no guarantee that the methods described below for |
| 31 | +protecting sensitive information will actually work. Use at your own risk. |
| 32 | + |
| 33 | +::: |
| 34 | + |
| 35 | +## Different module types |
| 36 | + |
| 37 | +There are two types of modules in MagicMirror²: |
| 38 | + |
| 39 | +- browser-only |
| 40 | +- server-side and browser-side |
| 41 | + |
| 42 | +Why is this mentioned? Because secrets can only be protected server-side. |
| 43 | + |
| 44 | +::: warning NOTE |
| 45 | + |
| 46 | +In any case, you should carefully examine the modules you are using. |
| 47 | +Theoretically, a malicious module could intercept the entire configuration, |
| 48 | +including all secrets, and send it somewhere else. |
| 49 | + |
| 50 | +::: |
| 51 | + |
| 52 | +## Using secrets in server-side modules |
| 53 | + |
| 54 | +You have to add a new parameter in `config.js`: |
| 55 | + |
| 56 | +```js |
| 57 | +let config = { |
| 58 | + ... |
| 59 | + hideConfigSecrets: true, |
| 60 | + ... |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +After a restart MagicMirror² will not send environment variables beginning with |
| 65 | +`SECRET_` to the clients (browsers). |
| 66 | + |
| 67 | +### Example |
| 68 | + |
| 69 | +The MMM-Strava module displays activities and needs 2 parameters `client_id` and |
| 70 | +`client_secret` to get the data. |
| 71 | + |
| 72 | +In this example the 2 parameters are set with 2 normal environment variables |
| 73 | +`STRAVA_CLIENT_ID` and `STRAVA_API_KEY`: |
| 74 | + |
| 75 | +```js |
| 76 | +let config = { |
| 77 | + ... |
| 78 | + hideConfigSecrets: true, |
| 79 | + ... |
| 80 | + modules: [ |
| 81 | + { |
| 82 | + module: "MMM-Strava", |
| 83 | + header: "Strava", |
| 84 | + position: "bottom_left", |
| 85 | + config: { |
| 86 | + client_id: "${STRAVA_CLIENT_ID}", |
| 87 | + client_secret: "${STRAVA_API_KEY}", |
| 88 | + activities: ["ride"], |
| 89 | + period: "recent", |
| 90 | + stats: ["count", "distance", "elevation", "achievements"], |
| 91 | + auto_rotate: true, |
| 92 | + updateInterval: 20000, |
| 93 | + reloadInterval: 3600000, |
| 94 | + showPrivateStats: true, |
| 95 | + limitPrivateStats: 1200, |
| 96 | + digits: 0 |
| 97 | + } |
| 98 | + }, |
| 99 | + ... |
| 100 | + ] |
| 101 | +}; |
| 102 | +``` |
| 103 | + |
| 104 | +This setup is unsafe, you can see the contents of the 2 environment variables in |
| 105 | +the browser. |
| 106 | + |
| 107 | +To be safe, you have to use environment variables called |
| 108 | +`SECRET_STRAVA_CLIENT_ID` and `SECRET_STRAVA_API_KEY`. The browser has no access |
| 109 | +to the content of variables prefixed with `SECRET_`, the content of e.g. |
| 110 | +`SECRET_STRAVA_CLIENT_ID` is displayed as `**SECRET_STRAVA_CLIENT_ID**`. |
| 111 | + |
| 112 | +## Using secrets on the browser-side |
| 113 | + |
| 114 | +Yes, we wrote above that this isn't possible. However, for some applications it |
| 115 | +might work with a workaround. |
| 116 | + |
| 117 | +There are modules that use a map (e.g. MMM-RAIN-MAP, MMM-Flights) and the data |
| 118 | +required for this map is of course retrieved in the browser. |
| 119 | + |
| 120 | +Some map services, such as MapBox, offer free maps, but often require an access |
| 121 | +token in the URL. |
| 122 | + |
| 123 | +Example: |
| 124 | + |
| 125 | +```js |
| 126 | +let config = { |
| 127 | + ... |
| 128 | + hideConfigSecrets: true, |
| 129 | + ... |
| 130 | + modules: [ |
| 131 | + { |
| 132 | + module: "MMM-Flights", |
| 133 | + position: "top_left", |
| 134 | + config: { |
| 135 | + mapUrl: "https://api.mapbox.com/styles/v1/username/mapId/tiles/{z}/{x}/{y}?access_token=abc" |
| 136 | + }, |
| 137 | + }, |
| 138 | + ... |
| 139 | + ] |
| 140 | +}; |
| 141 | +``` |
| 142 | + |
| 143 | +To protect sensitive information in the above url you can change it to |
| 144 | + |
| 145 | +`mapUrl: "https://api.mapbox.com/styles/v1/${SECRET_MAPBOX_ID}/tiles/{z}/{x}/{y}?access_token=${SECRET_MAPBOX_TOKEN}"` |
| 146 | + |
| 147 | +This will protect the secrets but the modules won't work. The workaround is to |
| 148 | +use the internal cors proxy. This means that the traffic does not go directly |
| 149 | +from the browser to the outside, but first to the MagicMirror² server, which |
| 150 | +acts as a proxy here: |
| 151 | + |
| 152 | +`mapUrl: "/cors?url=https://api.mapbox.com/styles/v1/${SECRET_MAPBOX_ID}/tiles/{z}/{x}/{y}?access_token=${SECRET_MAPBOX_TOKEN}"` |
| 153 | + |
| 154 | +Behind the scenes the server substitutes the variables before fetching the data. |
0 commit comments