-
Notifications
You must be signed in to change notification settings - Fork 80
Fix SQL injection #18
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| require('./init'); | ||
|
|
||
| var should = require('should'); | ||
|
|
||
| var Post, db; | ||
|
|
||
| describe('mssql connector', function () { | ||
|
|
||
| before(function () { | ||
| db = getDataSource(); | ||
|
|
||
| Post = db.define('PostWithBoolean', { | ||
| title: { type: String, length: 255, index: true }, | ||
| content: { type: String }, | ||
| approved: Boolean | ||
| }); | ||
| }); | ||
|
|
||
| it('should run migration', function (done) { | ||
| db.automigrate('PostWithBoolean', function () { | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| var post; | ||
| it('should support boolean types with true value', function(done) { | ||
| Post.create({title: 'T1', content: 'C1', approved: true}, function(err, p) { | ||
| should.not.exists(err); | ||
| post = p; | ||
| Post.findById(p.id, function(err, p) { | ||
| should.not.exists(err); | ||
| p.should.have.property('approved', true); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should support updating boolean types with false value', function(done) { | ||
| Post.update({id: post.id}, {approved: false}, function(err) { | ||
| should.not.exists(err); | ||
| Post.findById(post.id, function(err, p) { | ||
| should.not.exists(err); | ||
| p.should.have.property('approved', false); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| it('should support boolean types with false value', function(done) { | ||
| Post.create({title: 'T2', content: 'C2', approved: false}, function(err, p) { | ||
| should.not.exists(err); | ||
| post = p; | ||
| Post.findById(p.id, function(err, p) { | ||
| should.not.exists(err); | ||
| p.should.have.property('approved', false); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should single quote escape', function(done) { | ||
| Post.create({title: "T2", content: "C,D", approved: false}, function(err, p) { | ||
| should.not.exists(err); | ||
| post = p; | ||
| Post.findById(p.id, function(err, p) { | ||
| should.not.exists(err); | ||
| p.should.have.property('content', "C,D"); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return the model instance for upsert', function(done) { | ||
| Post.upsert({id: post.id, title: 'T2_new', content: 'C2_new', | ||
| approved: true}, function(err, p) { | ||
| p.should.have.property('id', post.id); | ||
| p.should.have.property('title', 'T2_new'); | ||
| p.should.have.property('content', 'C2_new'); | ||
| p.should.have.property('approved', true); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should return the model instance for upsert when id is not present', | ||
| function(done) { | ||
| Post.upsert({title: 'T2_new', content: 'C2_new', approved: true}, | ||
| function(err, p) { | ||
| p.should.have.property('id'); | ||
| p.should.have.property('title', 'T2_new'); | ||
| p.should.have.property('content', 'C2_new'); | ||
| p.should.have.property('approved', true); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should escape number values to defect SQL injection in findById', | ||
| function(done) { | ||
| Post.findById('(SELECT 1+1)', function(err, p) { | ||
| should.exists(err); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should escape number values to defect SQL injection in find', | ||
| function(done) { | ||
| Post.find({where: {id: '(SELECT 1+1)'}}, function(err, p) { | ||
| should.exists(err); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should escape number values to defect SQL injection in find with gt', | ||
| function(done) { | ||
| Post.find({where: {id: {gt: '(SELECT 1+1)'}}}, function(err, p) { | ||
| should.exists(err); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should escape number values to defect SQL injection in find', | ||
| function(done) { | ||
| Post.find({limit: '(SELECT 1+1)'}, function(err, p) { | ||
| should.exists(err); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should escape number values to defect SQL injection in find with inq', | ||
| function(done) { | ||
| Post.find({where: {id: {inq: ['(SELECT 1+1)']}}}, function(err, p) { | ||
| should.exists(err); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Until we ditch out string concatenation in favour of parameterized commands/queries: it would be nice to move this escape function to loopback-connector/lib/sql.js so that there is only one instance of this code shared by all SQL connectors.