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

Commit 5ada84a

Browse files
authored
Merge pull request #400 from Mithgol/sri
some security measures (for people not ready to trust their CDNs)
2 parents 2ba256b + 15dfd63 commit 5ada84a

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
@@ -25,8 +25,8 @@ ipfs-api
2525
- [Install](#install)
2626
- [Running the daemon with the right port](#running-the-daemon-with-the-right-port)
2727
- [Importing the module and usage](#importing-the-module-and-usage)
28-
- [In the Browser through browserify](#in-the-browser-through-browserify)
29-
- [In the Browser through `<script>` tag](#in-the-browser-through-script-tag)
28+
- [In a web browser through Browserify](#in-a-web-browser-through-browserify)
29+
- [In a web browser from CDN](#in-a-web-browser-from-cdn)
3030
- [CORS](#cors)
3131
- [Usage](#usage)
3232
- [API](#api)
@@ -75,33 +75,51 @@ var ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/5001')
7575
var ipfs = ipfsAPI({host: 'localhost', port: '5001', procotol: 'http'})
7676
```
7777

78-
### In the Browser through browserify
78+
### In a web browser through Browserify
7979

8080
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.
8181

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

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

8688
```html
8789
<script src="https://unpkg.com/ipfs-api/dist/index.js"></script>
8890
```
8991

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

92110
```
93111
var ipfs = window.IpfsApi('localhost', '5001')
94112
```
95113

96-
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:
114+
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:
97115

98116
```
99117
var ipfs = window.IpfsApi()
100118
```
101119

102120
### CORS
103121

104-
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:
122+
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:
105123

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

0 commit comments

Comments
 (0)