|
1 | 1 | 'use strict'
|
2 | 2 |
|
| 3 | +const Buffer = require('safe-buffer').Buffer |
| 4 | + |
| 5 | +const ssri = require('ssri') |
3 | 6 | const test = require('tap').test
|
| 7 | +const tnock = require('./util/tnock') |
| 8 | + |
| 9 | +const CACHE = require('./util/test-dir')(__filename) |
| 10 | +const CONTENT = Buffer.from('hello, world!', 'utf8') |
| 11 | +const INTEGRITY = ssri.fromData(CONTENT) |
| 12 | +const HOST = 'https://make-fetch-happen-safely.npm' |
| 13 | + |
| 14 | +const fetch = require('..').defaults({retry: false}) |
| 15 | + |
| 16 | +test('basic integrity verification', t => { |
| 17 | + const srv = tnock(t, HOST) |
| 18 | + srv.get('/wowsosafe').reply(200, CONTENT) |
| 19 | + srv.get('/wowsobad').reply(200, Buffer.from('pwnd')) |
| 20 | + const safetch = fetch.defaults({ |
| 21 | + integrity: INTEGRITY |
| 22 | + }) |
| 23 | + return safetch(`${HOST}/wowsosafe`).then(res => { |
| 24 | + return res.buffer() |
| 25 | + }).then(buf => { |
| 26 | + t.deepEqual(buf, CONTENT, 'good content passed scrutiny 👍🏼') |
| 27 | + return safetch(`${HOST}/wowsobad`).then(res => { |
| 28 | + return res.buffer() |
| 29 | + }).then(buf => { |
| 30 | + throw new Error(`bad data: ${buf.toString('utf8')}`) |
| 31 | + }).catch(err => { |
| 32 | + t.equal(err.code, 'EBADCHECKSUM', 'content failed checksum!') |
| 33 | + }) |
| 34 | + }) |
| 35 | +}) |
| 36 | + |
| 37 | +test('picks the "best" algorithm', t => { |
| 38 | + const integrity = ssri.fromData(CONTENT, { |
| 39 | + algorithms: ['md5', 'sha384', 'sha1', 'sha256'] |
| 40 | + }) |
| 41 | + integrity['md5'][0].digest = 'badc0ffee' |
| 42 | + integrity['sha1'][0].digest = 'badc0ffee' |
| 43 | + const safetch = fetch.defaults({integrity}) |
| 44 | + const srv = tnock(t, HOST) |
| 45 | + srv.get('/good').times(3).reply(200, CONTENT) |
| 46 | + srv.get('/bad').reply(200, 'pwnt') |
| 47 | + return safetch(`${HOST}/good`).then(res => res.buffer()).then(buf => { |
| 48 | + t.deepEqual(buf, CONTENT, 'data passed integrity check') |
| 49 | + return safetch(`${HOST}/bad`).then(res => { |
| 50 | + return res.buffer() |
| 51 | + }).then(buf => { |
| 52 | + throw new Error(`bad data: ${buf.toString('utf8')}`) |
| 53 | + }).catch(err => { |
| 54 | + t.equal(err.code, 'EBADCHECKSUM', 'content validated with either sha256 or sha384 (likely the latter)') |
| 55 | + }) |
| 56 | + }).then(() => { |
| 57 | + // invalidate sha384. sha256 is still valid, in theory |
| 58 | + integrity['sha384'][0].digest = 'pwnt' |
| 59 | + return safetch(`${HOST}/good`).then(res => { |
| 60 | + return res.buffer() |
| 61 | + }).then(buf => { |
| 62 | + throw new Error(`bad data: ${buf.toString('utf8')}`) |
| 63 | + }).catch(err => { |
| 64 | + t.equal(err.code, 'EBADCHECKSUM', 'strongest algorithm (sha384) treated as authoritative -- sha256 not used') |
| 65 | + }) |
| 66 | + }).then(() => { |
| 67 | + // remove bad sha384 altogether. sha256 remains valid |
| 68 | + delete integrity['sha384'] |
| 69 | + return safetch(`${HOST}/good`).then(res => res.buffer()) |
| 70 | + }).then(buf => { |
| 71 | + t.deepEqual(buf, CONTENT, 'data passed integrity check with sha256') |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +test('supports multiple hashes per algorithm', t => { |
| 76 | + const ALTCONTENT = Buffer.from('alt-content is like content but not really') |
| 77 | + const integrity = ssri.fromData(CONTENT, { |
| 78 | + algorithms: ['md5', 'sha384', 'sha1', 'sha256'] |
| 79 | + }).concat(ssri.fromData(ALTCONTENT, { |
| 80 | + algorithms: ['sha384'] |
| 81 | + })) |
| 82 | + const safetch = fetch.defaults({integrity}) |
| 83 | + const srv = tnock(t, HOST) |
| 84 | + srv.get('/main').reply(200, CONTENT) |
| 85 | + srv.get('/alt').reply(200, ALTCONTENT) |
| 86 | + srv.get('/bad').reply(200, 'nope') |
| 87 | + return safetch(`${HOST}/main`).then(res => res.buffer()).then(buf => { |
| 88 | + t.deepEqual(buf, CONTENT, 'main content validated against sha384') |
| 89 | + return safetch(`${HOST}/alt`).then(res => res.buffer()) |
| 90 | + }).then(buf => { |
| 91 | + t.deepEqual(buf, ALTCONTENT, 'alt content validated against sha384') |
| 92 | + return safetch(`${HOST}/bad`).then(res => res.buffer()).then(buf => { |
| 93 | + throw new Error(`bad data: ${buf.toString('utf8')}`) |
| 94 | + }).catch(err => { |
| 95 | + t.equal(err.code, 'EBADCHECKSUM', 'only the two valid contents pass') |
| 96 | + }) |
| 97 | + }) |
| 98 | +}) |
4 | 99 |
|
5 |
| -test('basic integrity verification') |
6 |
| -test('picks the "best" algorithm') |
7 |
| -test('fails with EBADCHECKSUM if integrity fails') |
8 |
| -test('checks integrity on cache fetch too') |
| 100 | +test('checks integrity on cache fetch too', t => { |
| 101 | + const srv = tnock(t, HOST) |
| 102 | + srv.get('/test').reply(200, CONTENT) |
| 103 | + const safetch = fetch.defaults({ |
| 104 | + cacheManager: CACHE, |
| 105 | + integrity: INTEGRITY, |
| 106 | + cache: 'must-revalidate' |
| 107 | + }) |
| 108 | + return safetch(`${HOST}/test`).then(res => res.buffer()).then(buf => { |
| 109 | + t.deepEqual(buf, CONTENT, 'good content passed scrutiny 👍🏼') |
| 110 | + srv.get('/test').reply(200, 'nope') |
| 111 | + return safetch(`${HOST}/test`).then(res => res.buffer()).then(buf => { |
| 112 | + throw new Error(`bad data: ${buf.toString('utf8')}`) |
| 113 | + }).catch(err => { |
| 114 | + t.equal(err.code, 'EBADCHECKSUM', 'cached content failed checksum!') |
| 115 | + }) |
| 116 | + }).then(() => { |
| 117 | + srv.get('/test').reply(200, 'nope') |
| 118 | + return safetch(`${HOST}/test`, { |
| 119 | + // try to use local cached version |
| 120 | + cache: 'force-cache', |
| 121 | + integrity: {algorithm: 'sha512', digest: 'doesnotmatch'} |
| 122 | + }).then(res => res.buffer()).then(buf => { |
| 123 | + throw new Error(`bad data: ${buf.toString('utf8')}`) |
| 124 | + }).catch(err => { |
| 125 | + t.equal(err.code, 'EBADCHECKSUM', 'cached content failed checksum!') |
| 126 | + }) |
| 127 | + }) |
| 128 | +}) |
0 commit comments