-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add localInfile option to control LOAD DATA LOCAL INFILE
closes #2185
- Loading branch information
1 parent
1e2c350
commit 337e87a
Showing
12 changed files
with
146 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,21 @@ | ||
module.exports = LocalInfileRequestPacket; | ||
function LocalInfileRequestPacket(options) { | ||
options = options || {}; | ||
|
||
this.filename = options.filename; | ||
} | ||
|
||
LocalInfileRequestPacket.prototype.parse = function parse(parser) { | ||
if (parser.parseLengthCodedNumber() !== null) { | ||
var err = new TypeError('Received invalid field length'); | ||
err.code = 'PARSER_INVALID_FIELD_LENGTH'; | ||
throw err; | ||
} | ||
|
||
this.filename = parser.parsePacketTerminatedString(); | ||
}; | ||
|
||
LocalInfileRequestPacket.prototype.write = function write(writer) { | ||
writer.writeLengthCodedNumber(null); | ||
writer.writeString(this.filename); | ||
}; |
This file contains 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 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 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 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
38 changes: 38 additions & 0 deletions
38
test/integration/connection/test-load-data-infile-disable.js
This file contains 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,38 @@ | ||
var assert = require('assert'); | ||
var common = require('../../common'); | ||
|
||
var path = common.fixtures + '/data.csv'; | ||
var table = 'load_data_test'; | ||
var newline = common.detectNewline(path); | ||
|
||
common.getTestConnection({localInfile: false}, function (err, connection) { | ||
assert.ifError(err); | ||
|
||
common.useTestDb(connection); | ||
|
||
connection.query([ | ||
'CREATE TEMPORARY TABLE ?? (', | ||
'`id` int(11) unsigned NOT NULL AUTO_INCREMENT,', | ||
'`title` varchar(400),', | ||
'PRIMARY KEY (`id`)', | ||
') ENGINE=InnoDB DEFAULT CHARSET=utf8' | ||
].join('\n'), [table], assert.ifError); | ||
|
||
var sql = | ||
'LOAD DATA LOCAL INFILE ? INTO TABLE ?? CHARACTER SET utf8 ' + | ||
'FIELDS TERMINATED BY ? ' + | ||
'LINES TERMINATED BY ? ' + | ||
'(id, title)'; | ||
|
||
connection.query(sql, [path, table, ',', newline], function (err) { | ||
assert.ok(err); | ||
assert.equal(err.code, 'ER_NOT_ALLOWED_COMMAND'); | ||
}); | ||
|
||
connection.query('SELECT * FROM ??', [table], function (err, rows) { | ||
assert.ifError(err); | ||
assert.equal(rows.length, 0); | ||
}); | ||
|
||
connection.end(assert.ifError); | ||
}); |
This file contains 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 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,41 @@ | ||
var assert = require('assert'); | ||
var common = require('../../common'); | ||
var connection = common.createConnection({port: common.fakeServerPort, localInfile: false}); | ||
var server = common.createFakeServer(); | ||
|
||
server.listen(common.fakeServerPort, function (err) { | ||
assert.ifError(err); | ||
|
||
connection.query('LOAD DATA LOCAL INFILE ? INTO TABLE ??', ['data.csv', 'foo'], function (err, result) { | ||
assert.ok(err); | ||
assert.equal(err.code, 'LOCAL_FILES_DISABLED'); | ||
assert.ok(!err.fatal); | ||
assert.equal(result.affectedRows, 0); | ||
|
||
connection.destroy(); | ||
server.destroy(); | ||
}); | ||
}); | ||
|
||
server.on('connection', function(conn) { | ||
conn.on('clientAuthentication', function (packet) { | ||
if (packet.clientFlags & common.ClientConstants.LOCAL_FILES) { | ||
conn.deny(); | ||
} else { | ||
conn.ok(); | ||
} | ||
}); | ||
conn.on('query', function (packet) { | ||
if (packet.sql.indexOf('LOAD DATA LOCAL INFILE') === 0) { | ||
conn.once('EmptyPacket', function () { | ||
conn.ok(); | ||
}); | ||
this._sendPacket(new common.Packets.LocalInfileRequestPacket({ | ||
filename: common.fixtures + '/data.csv' | ||
})); | ||
} else { | ||
this._handleQueryPacket(packet); | ||
} | ||
}); | ||
conn.handshake(); | ||
}); |