From fc2bb8343dd699e3acc6a68b35996c8448f65abd Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 14 Jun 2018 17:31:40 +0200 Subject: [PATCH] test: adds test vulnerable to encoding translation --- js/src/object/get.js | 36 ++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 37 insertions(+) diff --git a/js/src/object/get.js b/js/src/object/get.js index 8a960aaa9..4040d152b 100644 --- a/js/src/object/get.js +++ b/js/src/object/get.js @@ -7,6 +7,8 @@ const bs58 = require('bs58') const series = require('async/series') const hat = require('hat') const { getDescribe, getIt, expect } = require('../utils/mocha') +const UnixFs = require('ipfs-unixfs') +const crypto = require('crypto') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -285,5 +287,39 @@ module.exports = (createCommon, options) => { } ], done) }) + + it('supplies unadulterated data', () => { + // has to be big enough to span several DAGNodes + let required = 1024 * 3000 + + // can't just request `required` random bytes in the browser yet + // as it's more than 65536: + // https://github.com/crypto-browserify/randombytes/pull/15 + let data = Buffer.alloc(0) + const maxBytes = 65536 + let next = maxBytes + + while (data.length !== required) { + data = Buffer.concat([data, crypto.randomBytes(next)]) + next = maxBytes + + if (data.length + maxBytes > required) { + next = required - data.length + } + } + + return ipfs.files.add({ + path: '', + content: data + }) + .then((result) => { + return ipfs.object.get(result[0].hash) + }) + .then((node) => { + const meta = UnixFs.unmarshal(node.data) + + expect(meta.fileSize()).to.equal(data.length) + }) + }) }) } diff --git a/package.json b/package.json index 1243a94bb..1b3fba38b 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "dirty-chai": "^2.0.1", "hat": "0.0.3", "ipfs-block": "~0.7.1", + "ipfs-unixfs": "~0.1.15", "ipld-dag-cbor": "~0.12.1", "ipld-dag-pb": "~0.14.5", "is-ipfs": "~0.3.2",