You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit e105889
Browse filesBrowse the repository at this point in the historyBrowse files
fix: reject unsafe table and column names in msnodesqlv8 bulk loads
With the msnodesqlv8 driver, a bulk load finds its table through the
driver's table manager. The table manager writes the table name into
the text of a query of its own, then writes an INSERT from what it
found. Neither is quoted in a way that a name cannot end. So a table,
schema or database name could end the quoting it was written into, and
the rest of the name ran as SQL. The same was true of a column name, or
a table name as the server reported it, in the INSERT.
The name is now read once, without its brackets as before, and checked
before a connection is taken. The table manager is given the string
that was checked:
- The table and schema names must not hold a single `'`, because they
are written into quoted strings. A doubled `''` is still accepted.
- The database name must be made of identifier characters. Space around
it, or double quotes, are allowed. It must not hold a `'`.
- Every part of a temporary table name must be made of identifier
characters, because it is written unquoted into the INSERT.
- No part may hold a `$` followed by a backtick, a `'` or a `&`. The
table manager replaces each of these pairs with a part of its own
query, so such a name never named the table it seemed to.
When the table manager has found the table, the names it will write are
checked too. The name of a table in tempdb is written unquoted into the
INSERT, so each of its parts must pass the rule for the database name.
This applies also when the name does not say tempdb. A table or a
writable column whose name, as the server reports it, holds a `]` is
rejected, because the driver writes it in brackets without doubling the
`]`. A rejection at this point releases the connection.
A rejected name raises EINJECT. A name that passes the checks reaches
the table manager unchanged. The tedious driver is not changed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
// The table manager puts each part into its query with String.prototype.replace. That reads $`
43
+
// in the part as the text of the query before the placeholder, $' as the text after it, and $&
44
+
// as the placeholder. A name that holds one of these does not name the table it seems to. The
45
+
// text it copies brings the quotes of the query with it, so these are rejected in every part.
46
+
constCOPIES_QUERY=/\$[`'&]/
47
+
48
+
// Takes the name the table manager is to be given, and returns what was read, so the name it
49
+
// is given is the one that was checked.
50
+
constassertSafeBulkTable=(name)=>{
51
+
consttable=String(name)
52
+
constparts=table.split('.')
53
+
if(parts[parts.length-1].charAt(0)==='#'){
54
+
if(!parts.every(isBarePart)){
55
+
thrownewMSSQLError(`Invalid table name '${table}'. msnodesqlv8 writes the name of a temporary table unquoted, so it may only contain identifier characters.`,'EINJECT')
thrownewMSSQLError(`Invalid table name '${table}'. msnodesqlv8 writes the database name unquoted, so it may only contain identifier characters, or be in double quotes and contain no '.`,'EINJECT')
59
+
}elseif(parts.slice(-2).some(endsQuotedString)){
60
+
thrownewMSSQLError(`Invalid table name '${table}'. msnodesqlv8 writes the table and schema names into a quoted string, which a ' would end.`,'EINJECT')
61
+
}elseif(COPIES_QUERY.test(table)){
62
+
thrownewMSSQLError(`Invalid table name '${table}'. msnodesqlv8 replaces $\`, $' and $& in a table name with parts of its own query.`,'EINJECT')
63
+
}
64
+
returntable
65
+
}
66
+
67
+
// Checked once the table manager has found the table, against what it found, since that is
68
+
// what it builds its INSERT from. A table in tempdb is written as the name it was given, so
69
+
// each part of that name is held to the database rule above. The connection's own database
70
+
// can be tempdb, so the name need not say so. Any other table is written as
71
+
// `[database].[schema].[table]`, from the names the server reported, and each column it writes
72
+
// to as `[name]`. In both, any `]` in a name is left as it is. The columns it writes to are
73
+
// those it does not count as read only. A driver without `isReadOnly()` on its columns, such
thrownewMSSQLError(`Invalid table name '${table}'. msnodesqlv8 writes the name of a table in tempdb unquoted, so each part may only contain identifier characters, or be in double quotes and contain no '.`,'EINJECT')
thrownewMSSQLError(`Invalid table name '${columns[0].table_catalog}.${columns[0].table_schema}.${columns[0].table_name}'. msnodesqlv8 cannot bulk load into a table whose name contains ']'.`,'EINJECT')
0 commit comments