Skip to content

Commit baa0418

Browse files
committed
Remove and replace all note id compression in LZString with base64url
Signed-off-by: Max Wu <jackymaxj@gmail.com>
1 parent 912cce2 commit baa0418

4 files changed

Lines changed: 34 additions & 8 deletions

File tree

lib/models/note.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var fs = require('fs')
44
var path = require('path')
55
var LZString = require('lz-string')
6+
var base64url = require('base64url')
67
var md = require('markdown-it')()
78
var metaMarked = require('meta-marked')
89
var cheerio = require('cheerio')
@@ -114,6 +115,22 @@ module.exports = function (sequelize, DataTypes) {
114115
return false
115116
}
116117
},
118+
encodeNoteId: function (id) {
119+
// remove dashes in UUID and encode in url-safe base64
120+
return base64url.encode(id.replace(/-/g, ''))
121+
},
122+
decodeNoteId: function (encodedId) {
123+
// decode from url-safe base64
124+
let id = base64url.decode(encodedId)
125+
// add dashes between the UUID string parts
126+
let idParts = []
127+
idParts.push(id.substr(0, 8))
128+
idParts.push(id.substr(8, 4))
129+
idParts.push(id.substr(12, 4))
130+
idParts.push(id.substr(16, 4))
131+
idParts.push(id.substr(20, 12))
132+
return idParts.join('-')
133+
},
117134
checkNoteIdValid: function (id) {
118135
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
119136
var result = id.match(uuidRegex)
@@ -190,6 +207,16 @@ module.exports = function (sequelize, DataTypes) {
190207
return _callback(err, null)
191208
})
192209
},
210+
parseNoteIdByBase64Url: function (_callback) {
211+
// try to parse note id by base64url
212+
try {
213+
var id = Note.decodeNoteId(noteId)
214+
if (id && Note.checkNoteIdValid(id)) { return callback(null, id) } else { return _callback(null, null) }
215+
} catch (err) {
216+
return _callback(err, null)
217+
}
218+
},
219+
// parse note id by LZString is deprecated, here for compability
193220
parseNoteIdByLZString: function (_callback) {
194221
// try to parse note id by LZString Base64
195222
try {

lib/realtime.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var cookie = require('cookie')
55
var cookieParser = require('cookie-parser')
66
var url = require('url')
77
var async = require('async')
8-
var LZString = require('lz-string')
98
var randomcolor = require('randomcolor')
109
var Chance = require('chance')
1110
var chance = new Chance()
@@ -703,7 +702,7 @@ function operationCallback (socket, operation) {
703702
}
704703

705704
function updateHistory (userId, note, time) {
706-
var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id)
705+
var noteId = note.alias ? note.alias : models.Note.encodeNoteId(note.id)
707706
if (note.server) history.updateHistory(userId, noteId, note.server.document, time)
708707
}
709708

lib/response.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// external modules
44
var fs = require('fs')
55
var markdownpdf = require('markdown-pdf')
6-
var LZString = require('lz-string')
76
var shortId = require('shortid')
87
var querystring = require('querystring')
98
var request = require('request')
@@ -124,7 +123,7 @@ function newNote (req, res, next) {
124123
alias: req.alias ? req.alias : null,
125124
content: req.body ? req.body : ''
126125
}).then(function (note) {
127-
return res.redirect(config.serverurl + '/' + LZString.compressToBase64(note.id))
126+
return res.redirect(config.serverurl + '/' + models.Note.encodeNoteId(note.id))
128127
}).catch(function (err) {
129128
logger.error(err)
130129
return response.errorInternalError(res)
@@ -179,7 +178,7 @@ function showNote (req, res, next) {
179178
findNote(req, res, function (note) {
180179
// force to use note id
181180
var noteId = req.params.noteId
182-
var id = LZString.compressToBase64(note.id)
181+
var id = models.Note.encodeNoteId(note.id)
183182
if ((note.alias && noteId !== note.alias) || (!note.alias && noteId !== id)) { return res.redirect(config.serverurl + '/' + (note.alias || id)) }
184183
return responseHackMD(res, note)
185184
})
@@ -321,7 +320,7 @@ function actionPDF (req, res, note) {
321320
function actionGist (req, res, note) {
322321
var data = {
323322
client_id: config.github.clientID,
324-
redirect_uri: config.serverurl + '/auth/github/callback/' + LZString.compressToBase64(note.id) + '/gist',
323+
redirect_uri: config.serverurl + '/auth/github/callback/' + models.Note.encodeNoteId(note.id) + '/gist',
325324
scope: 'gist',
326325
state: shortId.generate()
327326
}
@@ -418,7 +417,7 @@ function publishNoteActions (req, res, next) {
418417
var action = req.params.action
419418
switch (action) {
420419
case 'edit':
421-
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id)))
420+
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)))
422421
break
423422
default:
424423
res.redirect(config.serverurl + '/s/' + note.shortid)
@@ -432,7 +431,7 @@ function publishSlideActions (req, res, next) {
432431
var action = req.params.action
433432
switch (action) {
434433
case 'edit':
435-
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : LZString.compressToBase64(note.id)))
434+
res.redirect(config.serverurl + '/' + (note.alias ? note.alias : models.Note.encodeNoteId(note.id)))
436435
break
437436
default:
438437
res.redirect(config.serverurl + '/p/' + note.shortid)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"Idle.Js": "git+https://github.com/shawnmclean/Idle.js",
1919
"async": "^2.1.4",
2020
"aws-sdk": "^2.7.20",
21+
"base64url": "^2.0.0",
2122
"blueimp-md5": "^2.6.0",
2223
"body-parser": "^1.15.2",
2324
"bootstrap": "^3.3.7",

0 commit comments

Comments
 (0)