Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 15dfd63

Browse files
authored
some security measures (for people not ready to trust their CDNs)
1 parent 1d9647e commit 15dfd63

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

README.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ ipfs-api
1919
- [Install](#install)
2020
- [Running the daemon with the right port](#running-the-daemon-with-the-right-port)
2121
- [Importing the module and usage](#importing-the-module-and-usage)
22-
- [In the Browser through browserify](#in-the-browser-through-browserify)
23-
- [In the Browser through `<script>` tag](#in-the-browser-through-script-tag)
22+
- [In a web browser through Browserify](#in-a-web-browser-through-browserify)
23+
- [In a web browser from CDN](#in-a-web-browser-from-cdn)
2424
- [CORS](#cors)
2525
- [Usage](#usage)
2626
- [API](#api)
@@ -69,33 +69,51 @@ var ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/5001')
6969
var ipfs = ipfsAPI({host: 'localhost', port: '5001', procotol: 'http'})
7070
```
7171

72-
### In the Browser through browserify
72+
### In a web browser through Browserify
7373

7474
Same as in Node.js, you just have to [browserify](http://browserify.org) the code before serving it. See the browserify repo for how to do that.
7575

76-
### In the Browser through `<script>` tag
76+
### In a web browser from CDN
7777

78-
You can use [unpkg](https://unpkg.com/) to get the latest built version, like this
78+
Instead of a local installation (and browserification) you may request a remote copy of IPFS API from [unpkg CDN](https://unpkg.com/).
79+
80+
To always request the latest version, use the following:
7981

8082
```html
8183
<script src="https://unpkg.com/ipfs-api/dist/index.js"></script>
8284
```
8385

84-
This will export the `IpfsApi` constructor on the `window` object, such that:
86+
For maximum security you may also decide to:
87+
88+
* reference a specific version of IPFS API (to prevent unexpected breaking changes when a newer latest version is published)
89+
90+
* [generate a SRI hash](https://www.srihash.org/) of that version and use it to ensure integrity
91+
92+
* set the [CORS settings attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes) to make anonymous requests to CDN
93+
94+
Example:
95+
96+
```html
97+
<script src="https://unpkg.com/ipfs-api@9.0.0/dist/index.js"
98+
integrity="sha384-5bXRcW9kyxxnSMbOoHzraqa7Z0PQWIao+cgeg327zit1hz5LZCEbIMx/LWKPReuB"
99+
crossorigin="anonymous"></script>
100+
```
101+
102+
CDN-based IPFS API provides the `IpfsApi` constructor as a method of the global `window` object. Example:
85103

86104
```
87105
var ipfs = window.IpfsApi('localhost', '5001')
88106
```
89107

90-
If you omit the host and port, the api will parse `window.host`, and use this information. This also works, and can be useful if you want to write apps that can be run from multiple different gateways:
108+
If you omit the host and port, the API will parse `window.host`, and use this information. This also works, and can be useful if you want to write apps that can be run from multiple different gateways:
91109

92110
```
93111
var ipfs = window.IpfsApi()
94112
```
95113

96114
### CORS
97115

98-
If are using this module in a browser with something like browserify, then you will get an error saying that the origin is not allowed. This would be a CORS ("Cross Origin Resource Sharing") failure. The ipfs server rejects requests from unknown domains by default. You can whitelist the domain that you are calling from by changing your ipfs config like this:
116+
In a web browser IPFS API (either browserified or CDN-based) might encounter an error saying that the origin is not allowed. This would be a CORS ("Cross Origin Resource Sharing") failure: IPFS servers are designed to reject requests from unknown domains by default. You can whitelist the domain that you are calling from by changing your ipfs config like this:
99117

100118
```bash
101119
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://example.com\"]"

0 commit comments

Comments
 (0)