Skip to content

Commit 664cc07

Browse files
committed
Add size constraint check for attribute and element values of DbOject class (Issue #1630)
1 parent 1ef9380 commit 664cc07

File tree

11 files changed

+408
-78
lines changed

11 files changed

+408
-78
lines changed

doc/src/release_notes.rst

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ node-oracledb `v6.4.0 <https://github.com/oracle/node-oracledb/compare/v6.3.0...
1313
Common Changes
1414
++++++++++++++
1515

16+
#) Attribute and element values of :ref:`DbObject Class
17+
<dbobjectclass>` objects that contain strings or bytes now have their
18+
maximum size constraints checked. Errors ``NJS-142`` and ``NJS-143`` are
19+
now raised when the size constraints are violated.
20+
`Issue #1630 <https://github.com/oracle/node-oracledb/issues/1630>`__.
21+
1622
Thin Mode Changes
1723
++++++++++++++++++
1824

@@ -533,13 +539,13 @@ node-oracledb `v5.5.0 <https://github.com/oracle/node-oracledb/compare/v5.4.0...
533539

534540
- Deprecated ``pool.setAccessToken()``.
535541

536-
#) ResultSets now implement the ``asyncIterator()`` symbol to support asynchonous
537-
iteration.
542+
#) ResultSets now implement the ``asyncIterator()`` symbol to support
543+
asynchronous iteration.
538544

539545
#) Added support for Oracle Advanced Queuing (AQ) :ref:`aqrecipientlists`.
540546

541547
#) Fixed a regression that could cause a pool alias to be recorded in the
542-
internal list of aliases even if pool creation failed.
548+
internal list of aliases even if pool creation had failed.
543549

544550

545551
node-oracledb `v5.4.0 <https://github.com/oracle/node-oracledb/compare/v5.3.0...v5.4.0>`__ (9 Jun 2022)
@@ -717,8 +723,8 @@ node-oracledb `v5.1.0 <https://github.com/oracle/node-oracledb/compare/v5.0.0...
717723
format. A new type ``oracledb.DB_TYPE_JSON`` was added.
718724

719725
#) Numeric suffixes are now added to duplicate SELECT column names when using
720-
``oracledb.OUT_FORMAT_OBJECT`` mode, allowing all columns to be represented in
721-
the JavaScript object.
726+
``oracledb.OUT_FORMAT_OBJECT`` mode, allowing all columns to be represented
727+
in the JavaScript object.
722728

723729
#) The value of ``prefetchRows`` set when getting a REF CURSOR as a BIND_OUT
724730
parameter is now used in the subsequent data retrieval from that cursor.
@@ -759,8 +765,8 @@ node-oracledb `v5.0.0 <https://github.com/oracle/node-oracledb/compare/v4.2.0...
759765
to the Node.js image `Issue #1201 <https://github.com/oracle/
760766
node-oracledb/issues/1201>`__.
761767

762-
- Removed use of git in `package/buildpackage.js` making offline builds cleaner
763-
for self-hosting node-oracledb.
768+
- Removed use of git in `package/buildpackage.js` making offline builds
769+
cleaner for self-hosting node-oracledb.
764770

765771
#) Connection Pool changes:
766772

@@ -770,8 +776,8 @@ node-oracledb `v5.0.0 <https://github.com/oracle/node-oracledb/compare/v4.2.0...
770776
`Issue #514 <https://github.com/oracle/node-oracledb/issues/514>`__.
771777

772778
- Made an internal change to use an Oracle Client 20 Session Pool feature
773-
allowing node-oracledb connection pools to shrink to ``poolMin`` even when
774-
there is no pool activity.
779+
allowing node-oracledb connection pools to shrink to ``poolMin`` even
780+
when there is no pool activity.
775781

776782
#) Added :attr:`oracledb.prefetchRows` and equivalent ``execute()`` option
777783
attribute :ref:`prefetchRows <propexecprefetchrows>` for query row fetch

lib/connection.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ class Connection extends EventEmitter {
105105
const cls = this._getDbObjectClass(objType.elementTypeClass);
106106
objType.elementTypeClass = cls;
107107
}
108-
nodbUtil.addTypeProperties(objType, "elementType");
108+
if (objType.isCollection) {
109+
nodbUtil.addTypeProperties(objType, "elementType");
110+
objType.elementTypeInfo.type = objType.elementType;
111+
}
109112
if (objType.attributes) {
110113
const props = {};
111114
for (const attr of objType.attributes) {

lib/dbObject.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,48 @@ const errors = require('./errors.js');
3232
const types = require('./types.js');
3333
const util = require('util');
3434

35+
//---------------------------------------------------------------------------
36+
// validatePropertyValue
37+
//
38+
// Validate the value based on metadata.
39+
// For object type, metaData corresponds to the attribute which is set.
40+
// For collection type, metaData corresponds to element in the collection.
41+
//---------------------------------------------------------------------------
42+
function validatePropertyValue(objType, metaData, value, index) {
43+
let valueLen, lengthErr = false;
44+
45+
if (value) {
46+
switch (metaData.type) {
47+
case types.DB_TYPE_VARCHAR:
48+
case types.DB_TYPE_NVARCHAR:
49+
case types.DB_TYPE_NCHAR:
50+
case types.DB_TYPE_CHAR:
51+
valueLen = Buffer.byteLength(value);
52+
if (valueLen > metaData.maxSize) {
53+
lengthErr = true;
54+
}
55+
break;
56+
case types.DB_TYPE_RAW:
57+
valueLen = value.length;
58+
if (valueLen > metaData.maxSize) {
59+
lengthErr = true;
60+
}
61+
break;
62+
default:
63+
break;
64+
}
65+
if (lengthErr) {
66+
if (index !== undefined) {
67+
errors.throwErr(errors.ERR_WRONG_LENGTH_FOR_DBOBJECT_ELEM,
68+
index, objType.fqn, valueLen, metaData.maxSize);
69+
} else {
70+
errors.throwErr(errors.ERR_WRONG_LENGTH_FOR_DBOBJECT_ATTR,
71+
metaData.name, objType.fqn, valueLen, metaData.maxSize);
72+
}
73+
}
74+
}
75+
}
76+
3577
// define base database object class; instances of this class are never
3678
// instantiated; instead, classes subclassed from this one will be
3779
// instantiated; a cache of these classes are maintained on each connection
@@ -61,6 +103,7 @@ class BaseDbObject {
61103
};
62104
const options = {allowArray: false};
63105
value = transformer.transformValueIn(info, value, options);
106+
validatePropertyValue(this._objType, attr, value);
64107
this._impl.setAttrValue(attr, value);
65108
}
66109

@@ -124,6 +167,13 @@ class BaseDbObject {
124167
};
125168
const options = {allowArray: false};
126169
value = transformer.transformValueIn(info, value, options);
170+
let index = this._impl.getLastIndex();
171+
if (index) {
172+
index = index + 1; // element will be appended at index + 1.
173+
} else {
174+
index = 0; // undefined for initial append, so set it to 0
175+
}
176+
validatePropertyValue(this._objType, this._objType.elementTypeInfo, value, index);
127177
this._impl.append(value);
128178
}
129179

@@ -344,6 +394,7 @@ class BaseDbObject {
344394
};
345395
const options = {allowArray: false};
346396
value = transformer.transformValueIn(info, value, options);
397+
validatePropertyValue(this._objType, this._objType.elementTypeInfo, value, index);
347398
this._impl.setElement(index, value);
348399
}
349400

lib/errors.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ const ERR_SERVER_VERSION_NOT_SUPPORTED = 138;
139139
const ERR_UNEXPECTED_XML_TYPE = 139;
140140
const ERR_WRONG_USER_FORMAT_EXTAUTH_PROXY = 140;
141141
const ERR_TOO_MANY_BATCH_ERRORS = 141;
142+
const ERR_WRONG_LENGTH_FOR_DBOBJECT_ATTR = 142;
143+
const ERR_WRONG_LENGTH_FOR_DBOBJECT_ELEM = 143;
142144

143145
// Oracle Net layer errors start from 500
144146
const ERR_CONNECTION_CLOSED = 500;
@@ -406,6 +408,10 @@ messages.set(ERR_WRONG_USER_FORMAT_EXTAUTH_PROXY, // NJS-140
406408
'user name must be enclosed in [] when using external authentication with a proxy user');
407409
messages.set(ERR_TOO_MANY_BATCH_ERRORS, // NJS-141
408410
'the number of batch errors from executemany() exceeds 65535');
411+
messages.set(ERR_WRONG_LENGTH_FOR_DBOBJECT_ATTR, // NJS-142
412+
'value too large for attribute %s of object %s (actual: %d, maximum: %d)');
413+
messages.set(ERR_WRONG_LENGTH_FOR_DBOBJECT_ELEM, // NJS-143
414+
'value too large for element %d of object %s (actual: %d, maximum: %d)');
409415

410416
// Oracle Net layer errors
411417

@@ -793,6 +799,8 @@ module.exports = {
793799
ERR_UNEXPECTED_XML_TYPE,
794800
ERR_WRONG_USER_FORMAT_EXTAUTH_PROXY,
795801
ERR_TOO_MANY_BATCH_ERRORS,
802+
ERR_WRONG_LENGTH_FOR_DBOBJECT_ATTR,
803+
ERR_WRONG_LENGTH_FOR_DBOBJECT_ELEM,
796804
ERR_CONNECTION_CLOSED_CODE: `${ERR_PREFIX}-${ERR_CONNECTION_CLOSED}`,
797805
WRN_COMPILATION_CREATE,
798806
assert,

0 commit comments

Comments
 (0)