From e7769538e656a018c6ad7eb8518765c0a4b3abc3 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Mon, 26 Feb 2018 11:25:40 +0800 Subject: [PATCH 1/3] Remove and replace all note id compression in LZString with base64url Signed-off-by: Max Wu --- lib/models/note.js | 14 ++++++++++++++ lib/realtime.js | 3 +-- lib/response.js | 11 +++++------ lib/utils.js | 19 +++++++++++++++++++ package.json | 1 + 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/models/note.js b/lib/models/note.js index 484f1a8c6f..409412a622 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -14,6 +14,8 @@ var DiffMatchPatch = require('diff-match-patch') var dmp = new DiffMatchPatch() var S = require('string') +const {encodeNoteId, decodeNoteId} = require('../utils') + // core var config = require('../config') var logger = require('../logger') @@ -114,6 +116,8 @@ module.exports = function (sequelize, DataTypes) { return false } }, + encodeNoteId: encodeNoteId, + decodeNoteId: decodeNoteId, checkNoteIdValid: function (id) { var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i var result = id.match(uuidRegex) @@ -190,6 +194,16 @@ module.exports = function (sequelize, DataTypes) { return _callback(err, null) }) }, + parseNoteIdByBase64Url: function (_callback) { + // try to parse note id by base64url + try { + var id = Note.decodeNoteId(noteId) + if (id && Note.checkNoteIdValid(id)) { return callback(null, id) } else { return _callback(null, null) } + } catch (err) { + return _callback(err, null) + } + }, + // parse note id by LZString is deprecated, here for compability parseNoteIdByLZString: function (_callback) { // try to parse note id by LZString Base64 try { diff --git a/lib/realtime.js b/lib/realtime.js index d6ba62b25b..5ee9f8fde5 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -5,7 +5,6 @@ var cookie = require('cookie') var cookieParser = require('cookie-parser') var url = require('url') var async = require('async') -var LZString = require('lz-string') var randomcolor = require('randomcolor') var Chance = require('chance') var chance = new Chance() @@ -703,7 +702,7 @@ function operationCallback (socket, operation) { } function updateHistory (userId, note, time) { - var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id) + var noteId = note.alias ? note.alias : models.Note.encodeNoteId(note.id) if (note.server) history.updateHistory(userId, noteId, note.server.document, time) } diff --git a/lib/response.js b/lib/response.js index 41e8c33647..25b9fafc42 100644 --- a/lib/response.js +++ b/lib/response.js @@ -3,7 +3,6 @@ // external modules var fs = require('fs') var markdownpdf = require('markdown-pdf') -var LZString = require('lz-string') var shortId = require('shortid') var querystring = require('querystring') var request = require('request') @@ -124,7 +123,7 @@ function newNote (req, res, next) { alias: req.alias ? req.alias : null, content: req.body ? req.body : '' }).then(function (note) { - return res.redirect(config.serverurl + '/' + LZString.compressToBase64(note.id)) + return res.redirect(config.serverurl + '/' + models.Note.encodeNoteId(note.id)) }).catch(function (err) { logger.error(err) return response.errorInternalError(res) @@ -179,7 +178,7 @@ function showNote (req, res, next) { findNote(req, res, function (note) { // force to use note id var noteId = req.params.noteId - var id = LZString.compressToBase64(note.id) + var id = models.Note.encodeNoteId(note.id) if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) { return res.redirect(config.serverurl + '/' + (note.alias || id)) } return responseHackMD(res, note) }) @@ -321,7 +320,7 @@ function actionPDF (req, res, note) { function actionGist (req, res, note) { var data = { client_id: config.github.clientID, - redirect_uri: config.serverurl + '/auth/github/callback/' + LZString.compressToBase64(note.id) + '/gist', + redirect_uri: config.serverurl + '/auth/github/callback/' + models.Note.encodeNoteId(note.id) + '/gist', scope: 'gist', state: shortId.generate() } @@ -418,7 +417,7 @@ function publishNoteActions (req, res, next) { var action = req.params.action switch (action) { case 'edit': - res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id))) + res.redirect(config.serverurl + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id))) break default: res.redirect(config.serverurl + '/s/' + note.shortid) @@ -432,7 +431,7 @@ function publishSlideActions (req, res, next) { var action = req.params.action switch (action) { case 'edit': - res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id))) + res.redirect(config.serverurl + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id))) break default: res.redirect(config.serverurl + '/p/' + note.shortid) diff --git a/lib/utils.js b/lib/utils.js index 247f85f25a..55b4fa8932 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,6 +1,7 @@ 'use strict' const fs = require('fs') const path = require('path') +const base64url = require('base64url') exports.isSQLite = function isSQLite (sequelize) { return sequelize.options.dialect === 'sqlite' @@ -32,3 +33,21 @@ exports.isRevealTheme = function isRevealTheme (theme) { } return undefined } + +exports.encodeNoteId = function (id) { + // remove dashes in UUID and encode in url-safe base64 + return base64url.encode(id.replace(/-/g, '')) +} + +exports.decodeNoteId = function (encodedId) { + // decode from url-safe base64 + let id = base64url.decode(encodedId) + // add dashes between the UUID string parts + let idParts = [] + idParts.push(id.substr(0, 8)) + idParts.push(id.substr(8, 4)) + idParts.push(id.substr(12, 4)) + idParts.push(id.substr(16, 4)) + idParts.push(id.substr(20, 12)) + return idParts.join('-') +} diff --git a/package.json b/package.json index ba8b05d757..beb01122b5 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "Idle.Js": "git+https://github.com/shawnmclean/Idle.js", "async": "^2.1.4", "aws-sdk": "^2.7.20", + "base64url": "^2.0.0", "blueimp-md5": "^2.6.0", "body-parser": "^1.15.2", "bootstrap": "^3.3.7", From b67300bf3667ca9c62c00e91b94af5070abda170 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Mon, 26 Feb 2018 11:26:48 +0800 Subject: [PATCH 2/3] Add migration for LZString compressed note id in history Signed-off-by: Max Wu --- lib/history.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/history.js b/lib/history.js index f46ff49f6d..f3d4440e3b 100644 --- a/lib/history.js +++ b/lib/history.js @@ -1,6 +1,7 @@ 'use strict' // history // external modules +var LZString = require('lz-string') // core var config = require('./config') @@ -27,7 +28,26 @@ function getHistory (userid, callback) { } var history = {} if (user.history) { - history = parseHistoryToObject(JSON.parse(user.history)) + history = JSON.parse(user.history) + // migrate LZString encoded note id to base64url encoded note id + for (let i = 0, l = history.length; i < l; i++) { + let item = history[i] + // try to parse in base64url + let id = models.Note.decodeNoteId(item.id) + if (!id || !models.Note.checkNoteIdValid(id)) { + // try to parse in LZString if it can't be parsed in base64url + try { + id = LZString.decompressFromBase64(item.id) + } catch (err) { + id = null + } + if (id && models.Note.checkNoteIdValid(id)) { + // replace the note id to base64url encoded note id + history[i].id = models.Note.encodeNoteId(id) + } + } + } + history = parseHistoryToObject(history) } if (config.debug) { logger.info('read history success: ' + user.id) From 3e3d01631b486f48c1d13bb788e9e06a908dd381 Mon Sep 17 00:00:00 2001 From: Max Wu Date: Mon, 26 Feb 2018 16:34:03 +0800 Subject: [PATCH 3/3] Update to move note id encode/decode function back to model Signed-off-by: Wu Cheng-Han --- lib/models/note.js | 21 +++++++++++++++++---- lib/utils.js | 19 ------------------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/lib/models/note.js b/lib/models/note.js index 409412a622..e199a3dbd1 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -3,6 +3,7 @@ var fs = require('fs') var path = require('path') var LZString = require('lz-string') +var base64url = require('base64url') var md = require('markdown-it')() var metaMarked = require('meta-marked') var cheerio = require('cheerio') @@ -14,8 +15,6 @@ var DiffMatchPatch = require('diff-match-patch') var dmp = new DiffMatchPatch() var S = require('string') -const {encodeNoteId, decodeNoteId} = require('../utils') - // core var config = require('../config') var logger = require('../logger') @@ -116,8 +115,22 @@ module.exports = function (sequelize, DataTypes) { return false } }, - encodeNoteId: encodeNoteId, - decodeNoteId: decodeNoteId, + encodeNoteId: function (id) { + // remove dashes in UUID and encode in url-safe base64 + return base64url.encode(id.replace(/-/g, '')) + }, + decodeNoteId: function (encodedId) { + // decode from url-safe base64 + let id = base64url.decode(encodedId) + // add dashes between the UUID string parts + let idParts = [] + idParts.push(id.substr(0, 8)) + idParts.push(id.substr(8, 4)) + idParts.push(id.substr(12, 4)) + idParts.push(id.substr(16, 4)) + idParts.push(id.substr(20, 12)) + return idParts.join('-') + }, checkNoteIdValid: function (id) { var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i var result = id.match(uuidRegex) diff --git a/lib/utils.js b/lib/utils.js index 55b4fa8932..247f85f25a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,7 +1,6 @@ 'use strict' const fs = require('fs') const path = require('path') -const base64url = require('base64url') exports.isSQLite = function isSQLite (sequelize) { return sequelize.options.dialect === 'sqlite' @@ -33,21 +32,3 @@ exports.isRevealTheme = function isRevealTheme (theme) { } return undefined } - -exports.encodeNoteId = function (id) { - // remove dashes in UUID and encode in url-safe base64 - return base64url.encode(id.replace(/-/g, '')) -} - -exports.decodeNoteId = function (encodedId) { - // decode from url-safe base64 - let id = base64url.decode(encodedId) - // add dashes between the UUID string parts - let idParts = [] - idParts.push(id.substr(0, 8)) - idParts.push(id.substr(8, 4)) - idParts.push(id.substr(12, 4)) - idParts.push(id.substr(16, 4)) - idParts.push(id.substr(20, 12)) - return idParts.join('-') -}