From d9e19b602968551fcda4ee806d767a04f6a11490 Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Mon, 2 Jan 2017 11:05:05 +0800 Subject: [PATCH] Update to remove null byte before saving to DB and remove null byte on changes --- lib/models/index.js | 7 +++++++ lib/models/note.js | 15 ++++++++++++--- lib/models/revision.js | 20 ++++++++++++++++---- public/js/index.js | 7 +++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/models/index.js b/lib/models/index.js index de6cd13..6d0fd3c 100644 --- a/lib/models/index.js +++ b/lib/models/index.js @@ -20,6 +20,13 @@ if (config.dburl) else sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig); +// [Postgres] Handling NULL bytes +// https://github.com/sequelize/sequelize/issues/6485 +function stripNullByte(value) { + return value ? value.replace(/\u0000/g, "") : value; +} +sequelize.stripNullByte = stripNullByte; + var db = {}; fs diff --git a/lib/models/note.js b/lib/models/note.js index 81de991..37d26ec 100644 --- a/lib/models/note.js +++ b/lib/models/note.js @@ -52,13 +52,22 @@ module.exports = function (sequelize, DataTypes) { defaultValue: 0 }, title: { - type: DataTypes.TEXT + type: DataTypes.TEXT, + set: function (value) { + this.setDataValue('title', sequelize.stripNullByte(value)); + } }, content: { - type: DataTypes.TEXT + type: DataTypes.TEXT, + set: function (value) { + this.setDataValue('content', sequelize.stripNullByte(value)); + } }, authorship: { - type: DataTypes.TEXT + type: DataTypes.TEXT, + set: function (value) { + this.setDataValue('authorship', JSON.stringify(value)); + } }, lastchangeAt: { type: DataTypes.DATE diff --git a/lib/models/revision.js b/lib/models/revision.js index 6f44cf1..adc651f 100644 --- a/lib/models/revision.js +++ b/lib/models/revision.js @@ -59,19 +59,31 @@ module.exports = function (sequelize, DataTypes) { defaultValue: Sequelize.UUIDV4 }, patch: { - type: DataTypes.TEXT + type: DataTypes.TEXT, + set: function (value) { + this.setDataValue('patch', sequelize.stripNullByte(value)); + } }, lastContent: { - type: DataTypes.TEXT + type: DataTypes.TEXT, + set: function (value) { + this.setDataValue('lastContent', sequelize.stripNullByte(value)); + } }, content: { - type: DataTypes.TEXT + type: DataTypes.TEXT, + set: function (value) { + this.setDataValue('content', sequelize.stripNullByte(value)); + } }, length: { type: DataTypes.INTEGER }, authorship: { - type: DataTypes.TEXT + type: DataTypes.TEXT, + set: function (value) { + this.setDataValue('authorship', value ? JSON.stringify(value) : value); + } } }, { classMethods: { diff --git a/public/js/index.js b/public/js/index.js index 328b67f..e62d1dc 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -3207,6 +3207,12 @@ function buildCursor(user) { } //editor actions +function removeNullByte(cm, change) { + var str = change.text.join("\n"); + if (/\u0000/g.test(str) && change.update) { + change.update(change.from, change.to, str.replace(/\u0000/g, "").split("\n")); + } +} function enforceMaxLength(cm, change) { var maxLength = cm.getOption("maxLength"); if (maxLength && change.update) { @@ -3228,6 +3234,7 @@ var ignoreEmitEvents = ['setValue', 'ignoreHistory']; editor.on('beforeChange', function (cm, change) { if (debug) console.debug(change); + removeNullByte(cm, change); if (enforceMaxLength(cm, change)) { $('.limit-modal').modal('show'); }