Skip to content

Commit

Permalink
fix: avoid problematic double-escaping of " in LIKE clauses (used i…
Browse files Browse the repository at this point in the history
…n multi-entry searches)

Also:
- fix: for at least one-extra-level-nested array keys, ensure proper encoding
    occurs (to ensure `get` requests work properly with them)
  • Loading branch information
brettz9 committed Sep 11, 2024
1 parent 63e6c76 commit 57b6c3b
Show file tree
Hide file tree
Showing 28 changed files with 216 additions and 51 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGES for indexeddbshim

## 15.2.0

- fix: avoid problematic double-escaping of `"` in LIKE clauses (used in
multi-entry searches)
- fix: for at least one-extra-level-nested array keys, ensure proper encoding
occurs (to ensure `get` requests work properly with them)

## 15.1.0

- fix: actually include TS fixes
Expand Down
2 changes: 1 addition & 1 deletion dist/IDBIndex.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/indexeddbshim-Key.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/indexeddbshim-Key.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/indexeddbshim-Key.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/indexeddbshim-Key.min.js.map

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions dist/indexeddbshim-UnicodeIdentifiers-node.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! indexeddbshim - v15.0.4 - 9/11/2024 */
/*! indexeddbshim - v15.2.0 - 9/11/2024 */

'use strict';

Expand Down Expand Up @@ -1313,12 +1313,13 @@ function escapeIndexNameForSQLKeyColumn(index) {
}

/**
* @todo Didn't need to escape `%`. Do we still need this escape?
* @param {string} str
* @returns {string}
*/
function sqlLIKEEscape(str) {
// https://www.sqlite.org/lang_expr.html#like
return sqlEscape(str).replaceAll('^', '^^');
return str.replaceAll('^', '^^');
}

/**
Expand Down Expand Up @@ -7151,8 +7152,11 @@ function executeFetchIndexData(count, unboundedDisallowed, index, hasKey, range,
*/
check => rowKey.includes(check)) ||
// More precise than our SQL
isMultiEntryMatch(/** @type {string} */
encodedKey, row[escapedIndexNameForKeyCol]))) {
isMultiEntryMatch(
// Added `JSON.stringify` as was having problems with
// `JSON.stringify` encoding added to nested
// array keys
JSON.stringify(encodedKey).slice(1, -1), row[escapedIndexNameForKeyCol]))) {
recordCount++;
record = row;
} else if (!hasKey && !multiChecks) {
Expand Down Expand Up @@ -7225,7 +7229,13 @@ function buildFetchIndexDataSQL(nullDisallowed, index, range, opType, multiCheck
sql.push(')');
} else if (index.multiEntry) {
sql.push('AND', escapeIndexNameForSQL(index.name), "LIKE ? ESCAPE '^'");
sqlValues.push('%' + sqlLIKEEscape(/** @type {string} */encode$1(range, index.multiEntry)) + '%');
if (Array.isArray(range)) {
// Todo: For nesting deeper than one level, we probably need to
// run `JSON.stringify` again
sqlValues.push('%' + sqlLIKEEscape(JSON.stringify(/** @type {string} */encode$1(range, index.multiEntry)).slice(1, -1)) + '%');
} else {
sqlValues.push('%' + sqlLIKEEscape(/** @type {string} */encode$1(range, index.multiEntry)) + '%');
}
} else {
const convertedRange = convertValueToKeyRange(range, nullDisallowed);
setSQLForKeyRange(convertedRange, escapeIndexNameForSQL(index.name), sql, sqlValues, true, false);
Expand Down
2 changes: 1 addition & 1 deletion dist/indexeddbshim-UnicodeIdentifiers-node.cjs.map

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions dist/indexeddbshim-UnicodeIdentifiers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/indexeddbshim-UnicodeIdentifiers.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/indexeddbshim-UnicodeIdentifiers.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/indexeddbshim-UnicodeIdentifiers.min.js.map

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions dist/indexeddbshim-node.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! indexeddbshim - v15.0.4 - 9/11/2024 */
/*! indexeddbshim - v15.2.0 - 9/11/2024 */

'use strict';

Expand Down Expand Up @@ -1313,12 +1313,13 @@ function escapeIndexNameForSQLKeyColumn(index) {
}

/**
* @todo Didn't need to escape `%`. Do we still need this escape?
* @param {string} str
* @returns {string}
*/
function sqlLIKEEscape(str) {
// https://www.sqlite.org/lang_expr.html#like
return sqlEscape(str).replaceAll('^', '^^');
return str.replaceAll('^', '^^');
}

/**
Expand Down Expand Up @@ -7151,8 +7152,11 @@ function executeFetchIndexData(count, unboundedDisallowed, index, hasKey, range,
*/
check => rowKey.includes(check)) ||
// More precise than our SQL
isMultiEntryMatch(/** @type {string} */
encodedKey, row[escapedIndexNameForKeyCol]))) {
isMultiEntryMatch(
// Added `JSON.stringify` as was having problems with
// `JSON.stringify` encoding added to nested
// array keys
JSON.stringify(encodedKey).slice(1, -1), row[escapedIndexNameForKeyCol]))) {
recordCount++;
record = row;
} else if (!hasKey && !multiChecks) {
Expand Down Expand Up @@ -7225,7 +7229,13 @@ function buildFetchIndexDataSQL(nullDisallowed, index, range, opType, multiCheck
sql.push(')');
} else if (index.multiEntry) {
sql.push('AND', escapeIndexNameForSQL(index.name), "LIKE ? ESCAPE '^'");
sqlValues.push('%' + sqlLIKEEscape(/** @type {string} */encode$1(range, index.multiEntry)) + '%');
if (Array.isArray(range)) {
// Todo: For nesting deeper than one level, we probably need to
// run `JSON.stringify` again
sqlValues.push('%' + sqlLIKEEscape(JSON.stringify(/** @type {string} */encode$1(range, index.multiEntry)).slice(1, -1)) + '%');
} else {
sqlValues.push('%' + sqlLIKEEscape(/** @type {string} */encode$1(range, index.multiEntry)) + '%');
}
} else {
const convertedRange = convertValueToKeyRange(range, nullDisallowed);
setSQLForKeyRange(convertedRange, escapeIndexNameForSQL(index.name), sql, sqlValues, true, false);
Expand Down
2 changes: 1 addition & 1 deletion dist/indexeddbshim-node.cjs.map

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions dist/indexeddbshim-noninvasive.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/indexeddbshim-noninvasive.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/indexeddbshim-noninvasive.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/indexeddbshim-noninvasive.min.js.map

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions dist/indexeddbshim.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/indexeddbshim.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/indexeddbshim.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/indexeddbshim.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function escapeIndexNameForSQL(index: string): string;
*/
export function escapeIndexNameForSQLKeyColumn(index: string): string;
/**
* @todo Didn't need to escape `%`. Do we still need this escape?
* @param {string} str
* @returns {string}
*/
Expand Down
Loading

0 comments on commit 57b6c3b

Please sign in to comment.