From 43d046406f736a2f2d16b10dbde7da139ee89e02 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 27 Apr 2018 11:54:23 +0200 Subject: [PATCH] Store the channel id as pad attribute --- www/common/common-hash.js | 3 +-- www/common/cryptpad-common.js | 27 ++++++++------------------- www/common/outer/async-store.js | 27 ++++++++++----------------- www/common/outer/store-rpc.js | 6 ------ www/common/outer/upload.js | 9 ++++++++- www/common/outer/userObject.js | 27 +++++++++++++++++++++++---- www/common/sframe-common-outer.js | 14 +++++++++----- 7 files changed, 59 insertions(+), 54 deletions(-) diff --git a/www/common/common-hash.js b/www/common/common-hash.js index 3a58a24b6..2f7171fbf 100644 --- a/www/common/common-hash.js +++ b/www/common/common-hash.js @@ -213,8 +213,7 @@ Version 1 if (ret.hashData.type !== 'pad') { return url + '#' + ret.hash; } if (ret.hashData.version === 0) { return url + '#' + ret.hash; } var hash; - if (options.readOnly === true || - (typeof (options.readOnly === "undefined") && ret.hashData.mode === "view")) { + if (typeof (options.readOnly === "undefined") && ret.hashData.mode === "view") { hash = getViewHashFromKeys({ version: ret.hashData.version, type: ret.hashData.app, diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index cbc469610..d6bae315a 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -462,22 +462,18 @@ define([ }; // When opening a new pad or renaming it, store the new title - common.setPadTitle = function (title, padHref, path, cb) { - var href = padHref || window.location.href; + common.setPadTitle = function (data, cb) { + if (!data || typeof (data) !== "object") { return cb ('Data is not an object'); } - // Password not needed here since we don't access hashData + var href = data.href || window.location.href; var parsed = Hash.parsePadUrl(href); - if (!parsed.hash) { return; } - href = parsed.getUrl({present: parsed.present}); + if (!parsed.hash) { return cb ('Invalid hash'); } + data.href = parsed.getUrl({present: parsed.present}); - if (title === null) { return; } - if (title.trim() === "") { title = Hash.getDefaultName(parsed); } + if (typeof (data.title) !== "string") { return cb('Missing title'); } + if (data.title.trim() === "") { data.title = Hash.getDefaultName(parsed); } - postMessage("SET_PAD_TITLE", { - href: href, - title: title, - path: path - }, function (obj) { + postMessage("SET_PAD_TITLE", data, function (obj) { if (obj && obj.error) { console.log("unable to set pad title"); return void cb(obj.error); @@ -498,13 +494,6 @@ define([ cb(void 0, data); }); }; - // Set initial path when creating a pad from pad creation screen - common.setInitialPath = function (path) { - postMessage("SET_INITIAL_PATH", path); - }; - common.setNewPadPassword = function (password) { - postMessage("SET_NEW_PAD_PASSWORD", password); - }; // Messaging (manage friends from the userlist) common.inviteFromUserlist = function (netfluxId, cb) { diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index e6cb10955..776843812 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -685,10 +685,11 @@ define([ Store.setPadTitle = function (data, cb) { var title = data.title; var href = data.href; - var padData = store.userObject.getFileData(store.userObject.getIdFromHref(href)); - var p = Hash.parsePadUrl(href, padData && padData.password); + var channel = data.channel; + var p = Hash.parsePadUrl(href); var h = p.hashData; + console.log(channel, data); if (AppConfig.disableAnonymousStore && !store.loggedIn) { return void cb(); } var owners; @@ -713,19 +714,19 @@ define([ var pad = allPads[id]; if (!pad.href) { continue; } - var p2 = Hash.parsePadUrl(pad.href, pad.password); + var p2 = Hash.parsePadUrl(pad.href); var h2 = p2.hashData; // Different types, proceed to the next one // No hash data: corrupted pad? if (p.type !== p2.type || !h2) { continue; } + // Different channel: continue + if (pad.channel !== channel) { continue; } var shouldUpdate = p.hash.replace(/\/$/, '') === p2.hash.replace(/\/$/, ''); // If the hash is different but represents the same channel, check if weaker or stronger - if (!shouldUpdate && - h.version === h2.version && - h.channel === h2.channel) { + if (!shouldUpdate && h.version !== 0) { // We had view & now we have edit, update if (h2.mode === 'view' && h.mode === 'edit') { shouldUpdate = true; } // Same mode and we had present URL, update @@ -762,13 +763,13 @@ define([ if (!contains) { Store.addPad({ href: href, + channel: channel, title: title, owners: owners, expire: expire, - password: store.data && store.data.newPadPassword, - path: data.path || (store.data && store.data.initialPath) + password: data.password, + path: data.path }, cb); - delete store.data.newPadPassword; return; } onSync(cb); @@ -807,14 +808,6 @@ define([ Store.getPadData = function (id, cb) { cb(store.userObject.getFileData(id)); }; - Store.setInitialPath = function (path) { - if (!store.data) { return; } - store.data.initialPath = path; - }; - Store.setNewPadPassword = function (password) { - if (!store.data) { return; } - store.data.newPadPassword = password; - }; // Messaging (manage friends from the userlist) diff --git a/www/common/outer/store-rpc.js b/www/common/outer/store-rpc.js index e95f6e227..b134435b2 100644 --- a/www/common/outer/store-rpc.js +++ b/www/common/outer/store-rpc.js @@ -120,12 +120,6 @@ define([ case 'GET_PAD_DATA': { Store.getPadData(data, cb); break; } - case 'SET_INITIAL_PATH': { - Store.setInitialPath(data); break; - } - case 'SET_NEW_PAD_PASSWORD': { - Store.setNewPadPassword(data); break; - } case 'GET_STRONGER_HASH': { Store.getStrongerHash(data, cb); break; } diff --git a/www/common/outer/upload.js b/www/common/outer/upload.js index ee9f4d77c..7f3874511 100644 --- a/www/common/outer/upload.js +++ b/www/common/outer/upload.js @@ -65,7 +65,14 @@ define([ if (noStore) { return void onComplete(href); } - common.setPadTitle(title || "", href, path, function (err) { + // PASSWORD_FILES + var data = { + title: title || "", + href: href, + path: path, + channel: id + }; + common.setPadTitle(data, function (err) { if (err) { return void console.error(err); } onComplete(href); common.setPadAttribute('fileType', metadata.type, null, href); diff --git a/www/common/outer/userObject.js b/www/common/outer/userObject.js index 0fc4099ec..06b62d696 100644 --- a/www/common/outer/userObject.js +++ b/www/common/outer/userObject.js @@ -552,33 +552,52 @@ define([ for (var id in fd) { id = Number(id); var el = fd[id]; + + // Clean corrupted data if (!el || typeof(el) !== "object") { debug("An element in filesData was not an object.", el); toClean.push(id); continue; } + // Clean missing href if (!el.href) { debug("Removing an element in filesData with a missing href.", el); toClean.push(id); continue; } - if (/^https*:\/\//.test(el.href)) { el.href = Hash.getRelativeHref(el.href); } - if (!el.ctime) { el.ctime = el.atime; } - // Password not needed here since we only need the type and hash var parsed = Hash.parsePadUrl(el.href); - if (!el.title) { el.title = Hash.getDefaultName(parsed); } + // Clean invalid hash if (!parsed.hash) { debug("Removing an element in filesData with a invalid href.", el); toClean.push(id); continue; } + // Clean invalid type if (!parsed.type) { debug("Removing an element in filesData with a invalid type.", el); toClean.push(id); continue; } + // Fix href + if (/^https*:\/\//.test(el.href)) { el.href = Hash.getRelativeHref(el.href); } + // Fix creation time + if (!el.ctime) { el.ctime = el.atime; } + // Fix title + if (!el.title) { el.title = Hash.getDefaultName(parsed); } + // Fix channel + if (!el.channel) { + if (parsed.hashData && parsed.hashData.type === "file") { + // PASSWORD_FILES + el.channel = Util.base64ToHex(parsed.hashData.channel); + } else { + var secret = Hash.getSecrets(parsed.type, parsed.hash, el.password); + el.channel = secret.channel; + } + console.log('Adding missing channel in filesData ', el.channel); + } + if ((loggedIn || config.testMode) && rootFiles.indexOf(id) === -1) { debug("An element in filesData was not in ROOT, TEMPLATE or TRASH.", id, el); var newName = Hash.createChannelId(); diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index 6ab878a27..f1cc789fb 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -25,6 +25,7 @@ define([ var AppConfig; var Test; var password; + var initialPathInDrive; nThen(function (waitFor) { // Load #2, the loading screen is up so grab whatever you need... @@ -295,8 +296,13 @@ define([ sframeChan.on('Q_SET_PAD_TITLE_IN_DRIVE', function (newTitle, cb) { currentTitle = newTitle; setDocumentTitle(); - Cryptpad.setNewPadPassword(password); - Cryptpad.setPadTitle(newTitle, undefined, undefined, function (err) { + var data = { + password: password, + title: newTitle, + channel: secret.channel, + path: initialPathInDrive // Where to store the pad if we don't have it in our drive + }; + Cryptpad.setPadTitle(data, function (err) { cb(err); }); }); @@ -718,8 +724,6 @@ define([ var newHash = Utils.Hash.createRandomHash(parsed.type, password); secret = Utils.Hash.getSecrets(parsed.type, newHash, password); - Cryptpad.setNewPadPassword(password); - // Update the hash in the address bar var ohc = window.onhashchange; window.onhashchange = function () {}; @@ -744,7 +748,7 @@ define([ nThen(function(waitFor) { if (data.templateId) { if (data.templateId === -1) { - Cryptpad.setInitialPath(['template']); + initialPathInDrive = ['template']; return; } Cryptpad.getPadData(data.templateId, waitFor(function (err, d) {