Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape dollarsign in sequelize.fn related to issue 11533 #11606

Merged
merged 4 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,6 @@ class QueryGenerator {
}
}
}

return SqlString.escape(value, this.options.timezone, this.dialect);
}

Expand Down Expand Up @@ -2134,7 +2133,7 @@ class QueryGenerator {
if (_.isPlainObject(arg)) {
return this.whereItemsQuery(arg);
}
return this.escape(arg);
return this.escape(typeof arg === 'string' ? arg.replace('$', '$$$') : arg);
papb marked this conversation as resolved.
Show resolved Hide resolved
}).join(', ')})`;
}
if (smth instanceof Utils.Col) {
Expand Down
11 changes: 11 additions & 0 deletions test/integration/instance/save.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,17 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
});
});

it('updates with function that contains escaped dollar symbol', function() {
return this.User.create({}).then(user => {
user.username = this.sequelize.fn('upper', '$sequelize');
return user.save().then(() => {
return this.User.findByPk(user.id).then(userAfterUpdate => {
expect(userAfterUpdate.username).to.equal('$SEQUELIZE');
});
});
});
});

describe('without timestamps option', () => {
it("doesn't update the updatedAt column", function() {
const User2 = this.sequelize.define('User2', {
Expand Down
10 changes: 10 additions & 0 deletions test/integration/model/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,16 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});

it('should escape $ in sequelize functions arguments', function() {
return this.User.create({
secretValue: this.sequelize.fn('upper', '$sequelize')
}).then(user => {
return this.User.findByPk(user.id).then(user => {
expect(user.secretValue).to.equal('$SEQUELIZE');
});
});
});

it('should work with a non-id named uuid primary key columns', function() {
const Monkey = this.sequelize.define('Monkey', {
monkeyId: { type: DataTypes.UUID, primaryKey: true, defaultValue: DataTypes.UUIDV4, allowNull: false }
Expand Down
6 changes: 6 additions & 0 deletions test/unit/dialects/abstract/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ describe('QueryGenerator', () => {
QG.handleSequelizeMethod(this.sequelize.where(this.sequelize.col('foo'), Op.not, null))
.should.be.equal('foo IS NOT NULL');
});

it('should correctly escape $ in sequelize.fn arguments', function() {
const QG = getAbstractQueryGenerator(this.sequelize);
QG.handleSequelizeMethod(this.sequelize.fn('upper', '$user'))
.should.include('$$user');
papb marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('format', () => {
Expand Down