diff --git a/frontend/src/download.js b/frontend/src/download.js index b3457c95..1048647c 100644 --- a/frontend/src/download.js +++ b/frontend/src/download.js @@ -1,4 +1,5 @@ const FileReceiver = require('./fileReceiver'); +const $ = require('jquery'); $(document).ready(function() { $('#send-file').click(() => { @@ -12,11 +13,6 @@ $(document).ready(function() { let progress = document.createElement('p'); let btn = $('#download-btn'); - // li.appendChild(name); - // li.appendChild(progress); - - //document.getElementById('downloaded_files').appendChild(li); - fileReceiver.on('progress', percentComplete => { progress.innerText = `Progress: ${percentComplete}%`; @@ -24,16 +20,6 @@ $(document).ready(function() { fileReceiver.removeAllListeners('progress'); btn.text('Download complete!'); btn.attr('disabled', 'true'); - // let finished = document.createElement('p'); - // finished.innerText = 'Your download has finished.'; - // li.appendChild(finished); - - // let close = document.createElement('button'); - // close.innerText = 'Ok'; - // close.addEventListener('click', () => { - // document.getElementById('downloaded_files').removeChild(li); - // }); - // li.appendChild(close); } }); @@ -46,7 +32,6 @@ $(document).ready(function() { $('#download-btn').hide(); $('#expired-img').show(); console.log('The file has expired, or has already been deleted.'); - // document.getElementById('downloaded_files').removeChild(li); return; }) .then(([decrypted, fname]) => { @@ -57,6 +42,11 @@ $(document).ready(function() { let a = document.createElement('a'); a.href = downloadUrl; + if (window.navigator.msSaveBlob) { + // if we are in microsoft edge or IE + window.navigator.msSaveBlob(blob, fname); + return; + } a.download = fname; document.body.appendChild(a); a.click(); diff --git a/frontend/src/upload.js b/frontend/src/upload.js index 0ca34460..72c02f36 100644 --- a/frontend/src/upload.js +++ b/frontend/src/upload.js @@ -1,4 +1,5 @@ const FileSender = require('./fileSender'); +const $ = require('jquery'); $(document).ready(function() { // reset copy button diff --git a/package-lock.json b/package-lock.json index d511dbb5..540a8fed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,14 +27,12 @@ "ansi-regex": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", - "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", - "dev": true + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" }, "ansi-styles": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", - "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", - "dev": true + "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=" }, "anymatch": { "version": "1.3.0", @@ -343,8 +341,7 @@ "chalk": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", - "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", - "dev": true + "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=" }, "chokidar": { "version": "1.7.0", @@ -558,6 +555,11 @@ "integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=", "dev": true }, + "dbug": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/dbug/-/dbug-0.4.2.tgz", + "integrity": "sha1-MrSzEF6IYQQ6b5rHVdgOVC02WzE=" + }, "debug": { "version": "2.6.7", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.7.tgz", @@ -685,8 +687,7 @@ "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", - "dev": true + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, "esutils": { "version": "2.0.2", @@ -1557,8 +1558,7 @@ "has-ansi": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", - "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", - "dev": true + "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=" }, "hash-base": { "version": "2.0.2", @@ -1633,6 +1633,11 @@ "integrity": "sha1-wDv04BywhtW15azorQr+eInWOMM=", "dev": true }, + "intel": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/intel/-/intel-1.2.0.tgz", + "integrity": "sha1-EdEUfraz9Fgr31M3s31UFYTp5B4=" + }, "ipaddr.js": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.3.0.tgz", @@ -1750,6 +1755,11 @@ "resolved": "https://registry.npmjs.org/jmespath/-/jmespath-0.15.0.tgz", "integrity": "sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc=" }, + "jquery": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.2.1.tgz", + "integrity": "sha1-XE2d5lKvbNCncBVKYxu6ErAVx4c=" + }, "js-tokens": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.1.tgz", @@ -1841,6 +1851,11 @@ "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, + "merge": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/merge/-/merge-1.2.0.tgz", + "integrity": "sha1-dTHjnUlJwoGma4xabgJl6LBYlNo=" + }, "merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -1927,6 +1942,11 @@ } } }, + "mozlog": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/mozlog/-/mozlog-2.1.1.tgz", + "integrity": "sha1-JxCOChxQvOMuo3Q4kRiDrjdO77s=" + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -2356,6 +2376,11 @@ "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.6.tgz", "integrity": "sha1-dc449SvwczxafwwRjYEzSiu19BI=" }, + "stack-trace": { + "version": "0.0.10", + "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", + "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=" + }, "statuses": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.3.1.tgz", @@ -2470,6 +2495,11 @@ "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz", "integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=" }, + "strftime": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/strftime/-/strftime-0.10.0.tgz", + "integrity": "sha1-s/D6QZKVICpaKJ9ta+n0kJphcZM=" + }, "string_decoder": { "version": "0.10.31", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", @@ -2478,8 +2508,7 @@ "strip-ansi": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", - "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", - "dev": true + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=" }, "subarg": { "version": "1.0.0", @@ -2490,8 +2519,12 @@ "supports-color": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", - "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", - "dev": true + "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=" + }, + "symbol": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/symbol/-/symbol-0.3.1.tgz", + "integrity": "sha1-tvmpANSWpX8CQI8iGYwQndoGMEE=" }, "syntax-error": { "version": "1.3.0", @@ -2602,6 +2635,11 @@ } } }, + "utcstring": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/utcstring/-/utcstring-0.1.0.tgz", + "integrity": "sha1-Qw/VEKt/yVtdWRDJAteYgMIIQ2s=" + }, "util": { "version": "0.10.3", "resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz", diff --git a/package.json b/package.json index 41934f48..bc9262a3 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,8 @@ "express": "^4.15.3", "express-handlebars": "^3.0.0", "fs-extra": "^3.0.1", + "jquery": "^3.2.1", + "mozlog": "^2.1.1", "node-fetch": "^1.7.1", "path": "^0.12.7", "redis": "^2.7.1" diff --git a/public/webcrypto-shim.js b/public/webcrypto-shim.js new file mode 100644 index 00000000..78b68c57 --- /dev/null +++ b/public/webcrypto-shim.js @@ -0,0 +1,597 @@ +/** + * @file Web Cryptography API shim + * @author Artem S Vybornov + * @license MIT + */ +!function ( global ) { + 'use strict'; + + if ( typeof Promise !== 'function' ) + throw "Promise support required"; + + var _crypto = global.crypto || global.msCrypto; + if ( !_crypto ) return; + + var _subtle = _crypto.subtle || _crypto.webkitSubtle; + if ( !_subtle ) return; + + var _Crypto = global.Crypto || _crypto.constructor || Object, + _SubtleCrypto = global.SubtleCrypto || _subtle.constructor || Object, + _CryptoKey = global.CryptoKey || global.Key || Object; + + var isIE = !!global.msCrypto, + isWebkit = !!_crypto.webkitSubtle; + if ( !isIE && !isWebkit ) return; + + function s2a ( s ) { + return btoa(s).replace(/\=+$/, '').replace(/\+/g, '-').replace(/\//g, '_'); + } + + function a2s ( s ) { + s += '===', s = s.slice( 0, -s.length % 4 ); + return atob( s.replace(/-/g, '+').replace(/_/g, '/') ); + } + + function s2b ( s ) { + var b = new Uint8Array(s.length); + for ( var i = 0; i < s.length; i++ ) b[i] = s.charCodeAt(i); + return b; + } + + function b2s ( b ) { + if ( b instanceof ArrayBuffer ) b = new Uint8Array(b); + return String.fromCharCode.apply( String, b ); + } + + function alg ( a ) { + var r = { 'name': (a.name || a || '').toUpperCase().replace('V','v') }; + switch ( r.name ) { + case 'SHA-1': + case 'SHA-256': + case 'SHA-384': + case 'SHA-512': + break; + case 'AES-CBC': + case 'AES-GCM': + case 'AES-KW': + if ( a.length ) r['length'] = a.length; + break; + case 'HMAC': + if ( a.hash ) r['hash'] = alg(a.hash); + if ( a.length ) r['length'] = a.length; + break; + case 'RSAES-PKCS1-v1_5': + if ( a.publicExponent ) r['publicExponent'] = new Uint8Array(a.publicExponent); + if ( a.modulusLength ) r['modulusLength'] = a.modulusLength; + break; + case 'RSASSA-PKCS1-v1_5': + case 'RSA-OAEP': + if ( a.hash ) r['hash'] = alg(a.hash); + if ( a.publicExponent ) r['publicExponent'] = new Uint8Array(a.publicExponent); + if ( a.modulusLength ) r['modulusLength'] = a.modulusLength; + break; + default: + throw new SyntaxError("Bad algorithm name"); + } + return r; + }; + + function jwkAlg ( a ) { + return { + 'HMAC': { + 'SHA-1': 'HS1', + 'SHA-256': 'HS256', + 'SHA-384': 'HS384', + 'SHA-512': 'HS512', + }, + 'RSASSA-PKCS1-v1_5': { + 'SHA-1': 'RS1', + 'SHA-256': 'RS256', + 'SHA-384': 'RS384', + 'SHA-512': 'RS512', + }, + 'RSAES-PKCS1-v1_5': { + '': 'RSA1_5', + }, + 'RSA-OAEP': { + 'SHA-1': 'RSA-OAEP', + 'SHA-256': 'RSA-OAEP-256', + }, + 'AES-KW': { + '128': 'A128KW', + '192': 'A192KW', + '256': 'A256KW', + }, + 'AES-GCM': { + '128': 'A128GCM', + '192': 'A192GCM', + '256': 'A256GCM', + }, + 'AES-CBC': { + '128': 'A128CBC', + '192': 'A192CBC', + '256': 'A256CBC', + }, + }[a.name][ ( a.hash || {} ).name || a.length || '' ]; + } + + function b2jwk ( k ) { + if ( k instanceof ArrayBuffer || k instanceof Uint8Array ) k = JSON.parse( decodeURIComponent( escape( b2s(k) ) ) ); + var jwk = { 'kty': k.kty, 'alg': k.alg, 'ext': k.ext || k.extractable }; + switch ( jwk.kty ) { + case 'oct': + jwk.k = k.k; + case 'RSA': + [ 'n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi', 'oth' ].forEach( function ( x ) { if ( x in k ) jwk[x] = k[x] } ); + break; + default: + throw new TypeError("Unsupported key type"); + } + return jwk; + } + + function jwk2b ( k ) { + var jwk = b2jwk(k); + if ( isIE ) jwk['extractable'] = jwk.ext, delete jwk.ext; + return s2b( unescape( encodeURIComponent( JSON.stringify(jwk) ) ) ).buffer; + } + + function pkcs2jwk ( k ) { + var info = b2der(k), prv = false; + if ( info.length > 2 ) prv = true, info.shift(); // remove version from PKCS#8 PrivateKeyInfo structure + var jwk = { 'ext': true }; + switch ( info[0][0] ) { + case '1.2.840.113549.1.1.1': + var rsaComp = [ 'n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi' ], + rsaKey = b2der( info[1] ); + if ( prv ) rsaKey.shift(); // remove version from PKCS#1 RSAPrivateKey structure + for ( var i = 0; i < rsaKey.length; i++ ) { + if ( !rsaKey[i][0] ) rsaKey[i] = rsaKey[i].subarray(1); + jwk[ rsaComp[i] ] = s2a( b2s( rsaKey[i] ) ); + } + jwk['kty'] = 'RSA'; + break; + default: + throw new TypeError("Unsupported key type"); + } + return jwk; + } + + function jwk2pkcs ( k ) { + var key, info = [ [ '', null ] ], prv = false; + switch ( k.kty ) { + case 'RSA': + var rsaComp = [ 'n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi' ], + rsaKey = []; + for ( var i = 0; i < rsaComp.length; i++ ) { + if ( !( rsaComp[i] in k ) ) break; + var b = rsaKey[i] = s2b( a2s( k[ rsaComp[i] ] ) ); + if ( b[0] & 0x80 ) rsaKey[i] = new Uint8Array(b.length + 1), rsaKey[i].set( b, 1 ); + } + if ( rsaKey.length > 2 ) prv = true, rsaKey.unshift( new Uint8Array([0]) ); // add version to PKCS#1 RSAPrivateKey structure + info[0][0] = '1.2.840.113549.1.1.1'; + key = rsaKey; + break; + default: + throw new TypeError("Unsupported key type"); + } + info.push( new Uint8Array( der2b(key) ).buffer ); + if ( !prv ) info[1] = { 'tag': 0x03, 'value': info[1] }; + else info.unshift( new Uint8Array([0]) ); // add version to PKCS#8 PrivateKeyInfo structure + return new Uint8Array( der2b(info) ).buffer; + } + + var oid2str = { 'KoZIhvcNAQEB': '1.2.840.113549.1.1.1' }, + str2oid = { '1.2.840.113549.1.1.1': 'KoZIhvcNAQEB' }; + + function b2der ( buf, ctx ) { + if ( buf instanceof ArrayBuffer ) buf = new Uint8Array(buf); + if ( !ctx ) ctx = { pos: 0, end: buf.length }; + + if ( ctx.end - ctx.pos < 2 || ctx.end > buf.length ) throw new RangeError("Malformed DER"); + + var tag = buf[ctx.pos++], + len = buf[ctx.pos++]; + + if ( len >= 0x80 ) { + len &= 0x7f; + if ( ctx.end - ctx.pos < len ) throw new RangeError("Malformed DER"); + for ( var xlen = 0; len--; ) xlen <<= 8, xlen |= buf[ctx.pos++]; + len = xlen; + } + + if ( ctx.end - ctx.pos < len ) throw new RangeError("Malformed DER"); + + var rv; + + switch ( tag ) { + case 0x02: // Universal Primitive INTEGER + rv = buf.subarray( ctx.pos, ctx.pos += len ); + break; + case 0x03: // Universal Primitive BIT STRING + if ( buf[ctx.pos++] ) throw new Error( "Unsupported bit string" ); + len--; + case 0x04: // Universal Primitive OCTET STRING + rv = new Uint8Array( buf.subarray( ctx.pos, ctx.pos += len ) ).buffer; + break; + case 0x05: // Universal Primitive NULL + rv = null; + break; + case 0x06: // Universal Primitive OBJECT IDENTIFIER + var oid = btoa( b2s( buf.subarray( ctx.pos, ctx.pos += len ) ) ); + if ( !( oid in oid2str ) ) throw new Error( "Unsupported OBJECT ID " + oid ); + rv = oid2str[oid]; + break; + case 0x30: // Universal Constructed SEQUENCE + rv = []; + for ( var end = ctx.pos + len; ctx.pos < end; ) rv.push( b2der( buf, ctx ) ); + break; + default: + throw new Error( "Unsupported DER tag 0x" + tag.toString(16) ); + } + + return rv; + } + + function der2b ( val, buf ) { + if ( !buf ) buf = []; + + var tag = 0, len = 0, + pos = buf.length + 2; + + buf.push( 0, 0 ); // placeholder + + if ( val instanceof Uint8Array ) { // Universal Primitive INTEGER + tag = 0x02, len = val.length; + for ( var i = 0; i < len; i++ ) buf.push( val[i] ); + } + else if ( val instanceof ArrayBuffer ) { // Universal Primitive OCTET STRING + tag = 0x04, len = val.byteLength, val = new Uint8Array(val); + for ( var i = 0; i < len; i++ ) buf.push( val[i] ); + } + else if ( val === null ) { // Universal Primitive NULL + tag = 0x05, len = 0; + } + else if ( typeof val === 'string' && val in str2oid ) { // Universal Primitive OBJECT IDENTIFIER + var oid = s2b( atob( str2oid[val] ) ); + tag = 0x06, len = oid.length; + for ( var i = 0; i < len; i++ ) buf.push( oid[i] ); + } + else if ( val instanceof Array ) { // Universal Constructed SEQUENCE + for ( var i = 0; i < val.length; i++ ) der2b( val[i], buf ); + tag = 0x30, len = buf.length - pos; + } + else if ( typeof val === 'object' && val.tag === 0x03 && val.value instanceof ArrayBuffer ) { // Tag hint + val = new Uint8Array(val.value), tag = 0x03, len = val.byteLength; + buf.push(0); for ( var i = 0; i < len; i++ ) buf.push( val[i] ); + len++; + } + else { + throw new Error( "Unsupported DER value " + val ); + } + + if ( len >= 0x80 ) { + var xlen = len, len = 4; + buf.splice( pos, 0, (xlen >> 24) & 0xff, (xlen >> 16) & 0xff, (xlen >> 8) & 0xff, xlen & 0xff ); + while ( len > 1 && !(xlen >> 24) ) xlen <<= 8, len--; + if ( len < 4 ) buf.splice( pos, 4 - len ); + len |= 0x80; + } + + buf.splice( pos - 2, 2, tag, len ); + + return buf; + } + + function CryptoKey ( key, alg, ext, use ) { + Object.defineProperties( this, { + _key: { + value: key + }, + type: { + value: key.type, + enumerable: true, + }, + extractable: { + value: (ext === undefined) ? key.extractable : ext, + enumerable: true, + }, + algorithm: { + value: (alg === undefined) ? key.algorithm : alg, + enumerable: true, + }, + usages: { + value: (use === undefined) ? key.usages : use, + enumerable: true, + }, + }); + } + + function isPubKeyUse ( u ) { + return u === 'verify' || u === 'encrypt' || u === 'wrapKey'; + } + + function isPrvKeyUse ( u ) { + return u === 'sign' || u === 'decrypt' || u === 'unwrapKey'; + } + + [ 'generateKey', 'importKey', 'unwrapKey' ] + .forEach( function ( m ) { + var _fn = _subtle[m]; + + _subtle[m] = function ( a, b, c ) { + var args = [].slice.call(arguments), + ka, kx, ku; + + switch ( m ) { + case 'generateKey': + ka = alg(a), kx = b, ku = c; + break; + case 'importKey': + ka = alg(c), kx = args[3], ku = args[4]; + if ( a === 'jwk' ) { + b = b2jwk(b); + if ( !b.alg ) b.alg = jwkAlg(ka); + if ( !b.key_ops ) b.key_ops = ( b.kty !== 'oct' ) ? ( 'd' in b ) ? ku.filter(isPrvKeyUse) : ku.filter(isPubKeyUse) : ku.slice(); + args[1] = jwk2b(b); + } + break; + case 'unwrapKey': + ka = args[4], kx = args[5], ku = args[6]; + args[2] = c._key; + break; + } + + if ( m === 'generateKey' && ka.name === 'HMAC' && ka.hash ) { + ka.length = ka.length || { 'SHA-1': 512, 'SHA-256': 512, 'SHA-384': 1024, 'SHA-512': 1024 }[ka.hash.name]; + return _subtle.importKey( 'raw', _crypto.getRandomValues( new Uint8Array( (ka.length+7)>>3 ) ), ka, kx, ku ); + } + + if ( isWebkit && m === 'generateKey' && ka.name === 'RSASSA-PKCS1-v1_5' && ( !ka.modulusLength || ka.modulusLength >= 2048 ) ) { + a = alg(a), a.name = 'RSAES-PKCS1-v1_5', delete a.hash; + return _subtle.generateKey( a, true, [ 'encrypt', 'decrypt' ] ) + .then( function ( k ) { + return Promise.all([ + _subtle.exportKey( 'jwk', k.publicKey ), + _subtle.exportKey( 'jwk', k.privateKey ), + ]); + }) + .then( function ( keys ) { + keys[0].alg = keys[1].alg = jwkAlg(ka); + keys[0].key_ops = ku.filter(isPubKeyUse), keys[1].key_ops = ku.filter(isPrvKeyUse); + return Promise.all([ + _subtle.importKey( 'jwk', keys[0], ka, kx, keys[0].key_ops ), + _subtle.importKey( 'jwk', keys[1], ka, kx, keys[1].key_ops ), + ]); + }) + .then( function ( keys ) { + return { + publicKey: keys[0], + privateKey: keys[1], + }; + }); + } + + if ( ( isWebkit || ( isIE && ( ka.hash || {} ).name === 'SHA-1' ) ) + && m === 'importKey' && a === 'jwk' && ka.name === 'HMAC' && b.kty === 'oct' ) { + return _subtle.importKey( 'raw', s2b( a2s(b.k) ), c, args[3], args[4] ); + } + + if ( isWebkit && m === 'importKey' && ( a === 'spki' || a === 'pkcs8' ) ) { + return _subtle.importKey( 'jwk', pkcs2jwk(b), c, args[3], args[4] ); + } + + if ( isIE && m === 'unwrapKey' ) { + return _subtle.decrypt( args[3], c, b ) + .then( function ( k ) { + return _subtle.importKey( a, k, args[4], args[5], args[6] ); + }); + } + + var op; + try { + op = _fn.apply( _subtle, args ); + } + catch ( e ) { + return Promise.reject(e); + } + + if ( isIE ) { + op = new Promise( function ( res, rej ) { + op.onabort = + op.onerror = function ( e ) { rej(e) }; + op.oncomplete = function ( r ) { res(r.target.result) }; + }); + } + + op = op.then( function ( k ) { + if ( ka.name === 'HMAC' ) { + if ( !ka.length ) ka.length = 8 * k.algorithm.length; + } + if ( ka.name.search('RSA') == 0 ) { + if ( !ka.modulusLength ) ka.modulusLength = (k.publicKey || k).algorithm.modulusLength; + if ( !ka.publicExponent ) ka.publicExponent = (k.publicKey || k).algorithm.publicExponent; + } + if ( k.publicKey && k.privateKey ) { + k = { + publicKey: new CryptoKey( k.publicKey, ka, kx, ku.filter(isPubKeyUse) ), + privateKey: new CryptoKey( k.privateKey, ka, kx, ku.filter(isPrvKeyUse) ), + }; + } + else { + k = new CryptoKey( k, ka, kx, ku ); + } + return k; + }); + + return op; + } + }); + + [ 'exportKey', 'wrapKey' ] + .forEach( function ( m ) { + var _fn = _subtle[m]; + + _subtle[m] = function ( a, b, c ) { + var args = [].slice.call(arguments); + + switch ( m ) { + case 'exportKey': + args[1] = b._key; + break; + case 'wrapKey': + args[1] = b._key, args[2] = c._key; + break; + } + + if ( ( isWebkit || ( isIE && ( b.algorithm.hash || {} ).name === 'SHA-1' ) ) + && m === 'exportKey' && a === 'jwk' && b.algorithm.name === 'HMAC' ) { + args[0] = 'raw'; + } + + if ( isWebkit && m === 'exportKey' && ( a === 'spki' || a === 'pkcs8' ) ) { + args[0] = 'jwk'; + } + + if ( isIE && m === 'wrapKey' ) { + return _subtle.exportKey( a, b ) + .then( function ( k ) { + if ( a === 'jwk' ) k = s2b( unescape( encodeURIComponent( JSON.stringify( b2jwk(k) ) ) ) ); + return _subtle.encrypt( args[3], c, k ); + }); + } + + var op; + try { + op = _fn.apply( _subtle, args ); + } + catch ( e ) { + return Promise.reject(e); + } + + if ( isIE ) { + op = new Promise( function ( res, rej ) { + op.onabort = + op.onerror = function ( e ) { rej(e) }; + op.oncomplete = function ( r ) { res(r.target.result) }; + }); + } + + if ( m === 'exportKey' && a === 'jwk' ) { + op = op.then( function ( k ) { + if ( ( isWebkit || ( isIE && ( b.algorithm.hash || {} ).name === 'SHA-1' ) ) + && b.algorithm.name === 'HMAC') { + return { 'kty': 'oct', 'alg': jwkAlg(b.algorithm), 'key_ops': b.usages.slice(), 'ext': true, 'k': s2a( b2s(k) ) }; + } + k = b2jwk(k); + if ( !k.alg ) k['alg'] = jwkAlg(b.algorithm); + if ( !k.key_ops ) k['key_ops'] = ( b.type === 'public' ) ? b.usages.filter(isPubKeyUse) : ( b.type === 'private' ) ? b.usages.filter(isPrvKeyUse) : b.usages.slice(); + return k; + }); + } + + if ( isWebkit && m === 'exportKey' && ( a === 'spki' || a === 'pkcs8' ) ) { + op = op.then( function ( k ) { + k = jwk2pkcs( b2jwk(k) ); + return k; + }); + } + + return op; + } + }); + + [ 'encrypt', 'decrypt', 'sign', 'verify' ] + .forEach( function ( m ) { + var _fn = _subtle[m]; + + _subtle[m] = function ( a, b, c, d ) { + if ( isIE && ( !c.byteLength || ( d && !d.byteLength ) ) ) + throw new Error("Empy input is not allowed"); + + var args = [].slice.call(arguments), + ka = alg(a); + + if ( isIE && m === 'decrypt' && ka.name === 'AES-GCM' ) { + var tl = a.tagLength >> 3; + args[2] = (c.buffer || c).slice( 0, c.byteLength - tl ), + a.tag = (c.buffer || c).slice( c.byteLength - tl ); + } + + args[1] = b._key; + + var op; + try { + op = _fn.apply( _subtle, args ); + } + catch ( e ) { + return Promise.reject(e); + } + + if ( isIE ) { + op = new Promise( function ( res, rej ) { + op.onabort = + op.onerror = function ( e ) { + rej(e); + }; + + op.oncomplete = function ( r ) { + var r = r.target.result; + + if ( m === 'encrypt' && r instanceof AesGcmEncryptResult ) { + var c = r.ciphertext, t = r.tag; + r = new Uint8Array( c.byteLength + t.byteLength ); + r.set( new Uint8Array(c), 0 ); + r.set( new Uint8Array(t), c.byteLength ); + r = r.buffer; + } + + res(r); + }; + }); + } + + return op; + } + }); + + if ( isIE ) { + var _digest = _subtle.digest; + + _subtle['digest'] = function ( a, b ) { + if ( !b.byteLength ) + throw new Error("Empy input is not allowed"); + + var op; + try { + op = _digest.call( _subtle, a, b ); + } + catch ( e ) { + return Promise.reject(e); + } + + op = new Promise( function ( res, rej ) { + op.onabort = + op.onerror = function ( e ) { rej(e) }; + op.oncomplete = function ( r ) { res(r.target.result) }; + }); + + return op; + }; + + global.crypto = Object.create( _crypto, { + getRandomValues: { value: function ( a ) { return _crypto.getRandomValues(a) } }, + subtle: { value: _subtle }, + }); + + global.CryptoKey = CryptoKey; + } + + if ( isWebkit ) { + _crypto.subtle = _subtle; + + global.Crypto = _Crypto; + global.SubtleCrypto = _SubtleCrypto; + global.CryptoKey = CryptoKey; + } +}(this); diff --git a/server/config.js b/server/config.js index 1e1119a4..be87cd85 100644 --- a/server/config.js +++ b/server/config.js @@ -32,4 +32,10 @@ let conf = convict({ // Perform validation conf.validate({ allowed: 'strict' }); -module.exports = conf.getProperties(); +let props = conf.getProperties(); +module.exports = props; + +module.exports.notLocalHost = + props.env === 'production' && + props.s3_bucket !== 'localhost' && + props.bitly_key !== 'localhost'; diff --git a/server/log.js b/server/log.js new file mode 100644 index 00000000..e851239d --- /dev/null +++ b/server/log.js @@ -0,0 +1,12 @@ +const conf = require('./config.js'); + +let notLocalHost = conf.notLocalHost; + +const mozlog = require('mozlog') ({ + app: 'FirefoxFileshare', + level: notLocalHost ? 'INFO' : 'verbose', + fmt: notLocalHost ? 'heka' : 'pretty', + debug: !notLocalHost +}) + +module.exports = mozlog; \ No newline at end of file diff --git a/server/portal_server.js b/server/portal_server.js index 62e234aa..5f8810ba 100644 --- a/server/portal_server.js +++ b/server/portal_server.js @@ -11,10 +11,11 @@ const bytes = require('bytes'); const conf = require('./config.js'); const storage = require('./storage.js'); -let notLocalHost = - conf.env === 'production' && - conf.s3_bucket !== 'localhost' && - conf.bitly_key !== 'localhost'; +let notLocalHost = conf.notLocalHost; + +const mozlog = require('./log.js'); + +let log = mozlog('portal.server'); const app = express(); @@ -30,6 +31,13 @@ app.get('/', (req, res) => { res.render('index'); }); +app.get('/exists/:id', (req, res) => { + let id = req.params.id; + storage.exists(id).then(doesExist => { + res.sendStatus(doesExist ? 200 : 404); + }); +}); + app.get('/download/:id', (req, res) => { let id = req.params.id; storage.filename(id).then(filename => { @@ -68,7 +76,7 @@ app.get('/assets/download/:id', (req, res) => { file_stream.on(notLocalHost ? 'finish' : 'close', () => { storage.forceDelete(id).then(err => { if (!err) { - console.log('Deleted.'); + log.info('Deleted:', id); } }); }); @@ -99,7 +107,7 @@ app.post('/delete/:id', (req, res) => { .delete(id, delete_token) .then(err => { if (!err) { - console.log('Deleted.'); + log.info('Deleted:', id); res.sendStatus(200); } }) @@ -114,8 +122,9 @@ app.post('/upload/:id', (req, res, next) => { req.pipe(req.busboy); req.busboy.on('file', (fieldname, file, filename) => { - console.log('Uploading: ' + filename); - const protocol = conf.env === 'development' ? req.protocol : 'https'; + log.info('Uploading:', req.params.id); + + const protocol = notLocalHost ? 'https' : req.protocol; let url = `${protocol}://${req.get('host')}/download/${req.params.id}/`; storage.set(req.params.id, file, filename, url).then(linkAndID => { @@ -125,7 +134,7 @@ app.post('/upload/:id', (req, res, next) => { }); let server = app.listen(conf.listen_port, () => { - console.log(`Portal app listening on port ${conf.listen_port}!`); + log.info('startServer:', `Portal app listening on port ${conf.listen_port}!`); }); let validateID = route_id => { diff --git a/server/storage.js b/server/storage.js index 28b1c8ba..4405482f 100644 --- a/server/storage.js +++ b/server/storage.js @@ -7,23 +7,25 @@ const path = require('path'); const fetch = require('node-fetch'); const crypto = require('crypto'); +let notLocalHost = conf.notLocalHost; + +const mozlog = require('./log.js'); + +let log = mozlog('portal.storage'); + const redis = require('redis'); const redis_client = redis.createClient({ host: conf.redis_host }); redis_client.on('error', err => { - console.log(err); + log.info('Redis: ', err); }); -let notLocalhost = - conf.env === 'production' && - conf.s3_bucket !== 'localhost' && - conf.bitly_key !== 'localhost'; - -if (notLocalhost) { +if (notLocalHost) { module.exports = { filename: filename, + exists: exists, length: awsLength, get: awsGet, set: awsSet, @@ -33,6 +35,7 @@ if (notLocalhost) { } else { module.exports = { filename: filename, + exists: exists, length: localLength, get: localGet, set: localSet, @@ -53,6 +56,14 @@ function filename(id) { }); } +function exists(id) { + return new Promise((resolve, reject) => { + redis_client.exists(id, (rediserr, reply) => { + resolve(reply === 1); + }); + }); +} + function localLength(id) { return new Promise((resolve, reject) => { try { @@ -76,7 +87,7 @@ function localSet(id, file, filename, url) { redis_client.hmset([id, 'filename', filename, 'delete', uuid]); redis_client.expire(id, 86400000); - console.log('Upload Finished of ' + filename); + log.info('localSet:', 'Upload Finished of ' + id); resolve({ uuid: uuid, url: url @@ -142,7 +153,7 @@ function awsSet(id, file, filename, url) { return new Promise((resolve, reject) => { s3.upload(params, function(err, data) { if (err) { - console.log(err, err.stack); // an error occurred + log.info('awsUploadError:', err.stack); // an error occurred reject(); } else { let uuid = crypto.randomBytes(10).toString('hex'); @@ -150,7 +161,7 @@ function awsSet(id, file, filename, url) { redis_client.hmset([id, 'filename', filename, 'delete', uuid]); redis_client.expire(id, 86400000); - console.log('Upload Finished of ' + filename); + log.info('awsUploadFinish', 'Upload Finished of ' + filename); if (conf.bitly_key) { fetch( 'https://api-ssl.bitly.com/v3/shorten?access_token=' + diff --git a/views/download.handlebars b/views/download.handlebars index 5aee5344..e1fa05d4 100644 --- a/views/download.handlebars +++ b/views/download.handlebars @@ -2,7 +2,7 @@ Download your file - + diff --git a/views/index.handlebars b/views/index.handlebars index 6c2a8478..2cf237e5 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -2,10 +2,10 @@ Firefox Fileshare - - + + - +