@coasys/ad4m 0.11.2-dev-sdna-only-prolog → 0.11.2-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -23,3 +23,35 @@ export function capSentence(cap) {
23
23
  can
24
24
  )} your ${domain} actions, with access to ${formatList(pointers)}`;
25
25
  }
26
+
27
+ /**
28
+ * Escapes a string value for safe use in SurrealQL queries.
29
+ *
30
+ * @description
31
+ * Prevents SQL injection by properly escaping special characters in string values
32
+ * that will be interpolated into SurrealQL queries. This handles the most common
33
+ * special characters that could break SQL queries or enable injection attacks.
34
+ *
35
+ * Single quotes, backslashes, and other special characters are escaped using
36
+ * backslash notation, which is the standard escaping mechanism for SurrealQL.
37
+ *
38
+ * @param value - The string value to escape
39
+ * @returns The escaped string safe for SurrealQL interpolation (without surrounding quotes)
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const userInput = "user's input with 'quotes'";
44
+ * const escaped = escapeSurrealString(userInput);
45
+ * const query = `SELECT * FROM link WHERE uri = '${escaped}'`;
46
+ * // Results in: SELECT * FROM link WHERE uri = 'user\'s input with \'quotes\''
47
+ * ```
48
+ */
49
+ export function escapeSurrealString(value: string): string {
50
+ return value
51
+ .replace(/\\/g, '\\\\') // Backslash -> \\
52
+ .replace(/'/g, "\\'") // Single quote -> \'
53
+ .replace(/"/g, '\\"') // Double quote -> \"
54
+ .replace(/\n/g, '\\n') // Newline -> \n
55
+ .replace(/\r/g, '\\r') // Carriage return -> \r
56
+ .replace(/\t/g, '\\t'); // Tab -> \t
57
+ }