execsql2 2.17.3__py3-none-any.whl → 2.18.0__py3-none-any.whl

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.
Files changed (52) hide show
  1. execsql/cli/__init__.py +13 -1
  2. execsql/cli/lint.py +16 -565
  3. execsql/cli/run.py +29 -2
  4. execsql/config.py +20 -0
  5. execsql/db/access.py +6 -0
  6. execsql/db/base.py +57 -1
  7. execsql/db/dsn.py +19 -9
  8. execsql/db/firebird.py +6 -0
  9. execsql/db/mysql.py +81 -0
  10. execsql/db/oracle.py +6 -0
  11. execsql/db/sqlite.py +37 -18
  12. execsql/db/sqlserver.py +31 -6
  13. execsql/exporters/base.py +1 -1
  14. execsql/exporters/duckdb.py +8 -4
  15. execsql/exporters/ods.py +11 -0
  16. execsql/exporters/sqlite.py +10 -3
  17. execsql/exporters/templates.py +10 -0
  18. execsql/exporters/xls.py +4 -0
  19. execsql/exporters/xlsx.py +9 -0
  20. execsql/importers/json.py +49 -32
  21. execsql/metacommands/conditions.py +7 -2
  22. execsql/metacommands/io_export.py +21 -26
  23. execsql/metacommands/io_fileops.py +21 -3
  24. execsql/metacommands/io_import.py +23 -3
  25. execsql/script/ast.py +8 -0
  26. execsql/script/engine.py +32 -0
  27. execsql/script/executor.py +12 -0
  28. execsql/script/variables.py +41 -15
  29. execsql/utils/auth.py +49 -1
  30. execsql/utils/fileio.py +120 -0
  31. execsql/utils/gui.py +11 -1
  32. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/md_compare.sql +12 -12
  33. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/md_glossary.sql +5 -5
  34. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/md_upsert.sql +13 -13
  35. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/pg_compare.sql +24 -24
  36. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/pg_glossary.sql +5 -5
  37. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/pg_upsert.sql +29 -29
  38. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/script_template.sql +2 -2
  39. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/ss_compare.sql +24 -24
  40. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/ss_glossary.sql +6 -6
  41. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/ss_upsert.sql +2917 -2917
  42. {execsql2-2.17.3.dist-info → execsql2-2.18.0.dist-info}/METADATA +6 -1
  43. {execsql2-2.17.3.dist-info → execsql2-2.18.0.dist-info}/RECORD +52 -52
  44. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/README.md +0 -0
  45. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/config_settings.sqlite +0 -0
  46. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/example_config_prompt.sql +0 -0
  47. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/execsql.conf +0 -0
  48. {execsql2-2.17.3.data → execsql2-2.18.0.data}/data/execsql2_extras/make_config_db.sql +0 -0
  49. {execsql2-2.17.3.dist-info → execsql2-2.18.0.dist-info}/WHEEL +0 -0
  50. {execsql2-2.17.3.dist-info → execsql2-2.18.0.dist-info}/entry_points.txt +0 -0
  51. {execsql2-2.17.3.dist-info → execsql2-2.18.0.dist-info}/licenses/LICENSE.txt +0 -0
  52. {execsql2-2.17.3.dist-info → execsql2-2.18.0.dist-info}/licenses/NOTICE +0 -0
execsql/importers/json.py CHANGED
@@ -4,10 +4,11 @@ from __future__ import annotations
4
4
  JSON import for execsql.
5
5
 
6
6
  Provides :func:`import_json`, used by ``IMPORT … FORMAT json``.
7
- Supports JSON arrays of objects (``[{…}, …]``) and newline-delimited
8
- JSON (NDJSON, one object per line). Nested objects are flattened with
9
- dot-separated keys; nested arrays and non-object values are serialized
10
- as JSON strings so every column maps to a scalar database value.
7
+ Supports JSON arrays of objects (``[{…}, …]``) and `JSON Lines
8
+ <https://jsonlines.org/>`_ (JSONL, one object per line). Nested
9
+ objects are flattened with dot-separated keys; nested arrays and
10
+ non-object values are serialized as JSON strings so every column
11
+ maps to a scalar database value.
11
12
  """
12
13
 
13
14
  import json
@@ -45,42 +46,58 @@ def _flatten(obj: Any, prefix: str = "", sep: str = ".") -> dict[str, Any]:
45
46
  def _parse_json_file(filename: str, encoding: str) -> list[dict[str, Any]]:
46
47
  """Read a JSON file and return a list of flat dicts.
47
48
 
48
- Accepts either a JSON array of objects or newline-delimited JSON
49
- (NDJSON).
49
+ Accepts either a JSON array of objects or `JSON Lines
50
+ <https://jsonlines.org/>`_ (JSONL). The JSONL path streams the
51
+ file line-by-line so the raw text isn't buffered alongside the
52
+ parsed records (B19/F043). The array path still buffers the
53
+ whole file — switching to a streaming parser would require
54
+ ``ijson`` as a dependency.
50
55
  """
51
- text = Path(filename).read_text(encoding=encoding)
52
- stripped = text.strip()
53
-
54
- if stripped.startswith("["):
55
- # Standard JSON array.
56
- raw = json.loads(stripped)
56
+ # Peek at the first non-whitespace character to decide which
57
+ # parsing strategy to use, without slurping the entire file.
58
+ with open(filename, encoding=encoding) as fh:
59
+ first_char: str | None = None
60
+ while True:
61
+ ch = fh.read(1)
62
+ if not ch:
63
+ break
64
+ if not ch.isspace():
65
+ first_char = ch
66
+ break
67
+
68
+ if first_char == "[":
69
+ # Standard JSON array — must buffer the whole file (json.loads
70
+ # has no streaming mode; ijson would be needed for that).
71
+ text = Path(filename).read_text(encoding=encoding)
72
+ raw = json.loads(text)
57
73
  if not isinstance(raw, list):
58
74
  raise ErrInfo(type="error", other_msg="JSON file root is not an array of objects.")
59
75
  records = raw
60
- elif stripped.startswith("{"):
61
- # Try NDJSON (one object per line) or a single object.
76
+ elif first_char == "{":
77
+ # JSONL (JSON Lines) stream the file line-by-line.
62
78
  records = []
63
- for lineno, line in enumerate(stripped.splitlines(), 1):
64
- line = line.strip()
65
- if not line:
66
- continue
67
- try:
68
- obj = json.loads(line)
69
- except json.JSONDecodeError as exc:
70
- raise ErrInfo(
71
- type="error",
72
- other_msg=f"Invalid JSON on line {lineno}: {exc}",
73
- ) from exc
74
- if not isinstance(obj, dict):
75
- raise ErrInfo(
76
- type="error",
77
- other_msg=f"Line {lineno} is not a JSON object.",
78
- )
79
- records.append(obj)
79
+ with open(filename, encoding=encoding) as fh:
80
+ for lineno, line in enumerate(fh, 1):
81
+ line = line.strip()
82
+ if not line:
83
+ continue
84
+ try:
85
+ obj = json.loads(line)
86
+ except json.JSONDecodeError as exc:
87
+ raise ErrInfo(
88
+ type="error",
89
+ other_msg=f"Invalid JSON on line {lineno}: {exc}",
90
+ ) from exc
91
+ if not isinstance(obj, dict):
92
+ raise ErrInfo(
93
+ type="error",
94
+ other_msg=f"Line {lineno} is not a JSON object.",
95
+ )
96
+ records.append(obj)
80
97
  else:
81
98
  raise ErrInfo(
82
99
  type="error",
83
- other_msg="JSON import expects a file starting with '[' (array) or '{' (object/NDJSON).",
100
+ other_msg="JSON import expects a file starting with '[' (array) or '{' (object/JSONL).",
84
101
  )
85
102
 
86
103
  if not records:
@@ -75,7 +75,7 @@ def xf_startswith(**kwargs: Any) -> bool:
75
75
  if kwargs["ignorecase"] and kwargs["ignorecase"].lower() == "i":
76
76
  s1 = s1.lower()
77
77
  s2 = s2.lower()
78
- return s1[: len(s2)] == s2
78
+ return s1.startswith(s2)
79
79
 
80
80
 
81
81
  def xf_endswith(**kwargs: Any) -> bool:
@@ -84,7 +84,7 @@ def xf_endswith(**kwargs: Any) -> bool:
84
84
  if kwargs["ignorecase"] and kwargs["ignorecase"].lower() == "i":
85
85
  s1 = s1.lower()
86
86
  s2 = s2.lower()
87
- return s1[-len(s2) :] == s2
87
+ return s1.endswith(s2)
88
88
 
89
89
 
90
90
  def xf_hasrows(**kwargs: Any) -> bool:
@@ -374,6 +374,10 @@ def xf_istrue(**kwargs: Any) -> bool:
374
374
  return unquoted(kwargs["value"].strip()).lower() in ("yes", "y", "true", "t", "1")
375
375
 
376
376
 
377
+ def xf_isfalse(**kwargs: Any) -> bool:
378
+ return unquoted(kwargs["value"].strip()).lower() in ("no", "n", "false", "f", "0")
379
+
380
+
377
381
  def xf_dbms(**kwargs: Any) -> bool:
378
382
  dbms = kwargs["dbms"]
379
383
  return _state.dbs.current().type.dbms_id.lower() == dbms.strip().lower()
@@ -797,6 +801,7 @@ def build_conditional_table() -> Any:
797
801
  category="condition",
798
802
  )
799
803
  mcl.add(r"^\s*IS_TRUE\(\s*(?P<value>[^)]*)\s*\)", xf_istrue, description="IS_TRUE", category="condition")
804
+ mcl.add(r"^\s*IS_FALSE\(\s*(?P<value>[^)]*)\s*\)", xf_isfalse, description="IS_FALSE", category="condition")
800
805
 
801
806
  # Boolean literals
802
807
  mcl.add(
@@ -18,7 +18,6 @@ appropriate writer in :mod:`execsql.exporters`.
18
18
 
19
19
  from __future__ import annotations
20
20
 
21
- from pathlib import Path
22
21
  from typing import Any
23
22
 
24
23
  import execsql.state as _state
@@ -44,28 +43,24 @@ from execsql.exporters.yaml import write_query_to_yaml
44
43
  from execsql.importers.base import import_data_table
45
44
  from execsql.script import current_script_line
46
45
  from execsql.utils.errors import exception_desc
47
- from execsql.utils.fileio import check_dir
46
+ from execsql.utils.fileio import check_dir, safe_output_path
48
47
 
49
48
 
50
49
  def _apply_output_dir(path: str) -> str:
51
- """Prepend the configured --output-dir to *path* if it is a relative path.
50
+ """Resolve *path* against the configured ``--output-dir`` root.
52
51
 
53
- If ``conf.export_output_dir`` is set and *path* is not absolute (and not
54
- ``stdout``), the base directory is joined to *path* so that all EXPORT
55
- output lands in the same directory without requiring scripts to hard-code
56
- absolute paths.
52
+ When ``conf.export_output_dir`` is set, ``--output-dir`` is treated as a
53
+ containment boundary: relative paths are joined to it, absolute paths
54
+ must already fall inside it, and ``..`` segments that escape the root
55
+ are rejected. ``stdout`` is passed through untouched.
56
+
57
+ When ``conf.export_output_dir`` is unset, *path* is returned unchanged
58
+ (no behavior change for users not opting in to ``--output-dir``).
57
59
  """
58
60
  output_dir = getattr(_state.conf, "export_output_dir", None)
59
- if not output_dir:
60
- return path
61
- if path.lower() == "stdout":
62
- return path
63
- if Path(path).is_absolute():
64
- return path
65
- # Windows drive-letter paths are also absolute
66
- if len(path) > 1 and path[1] == ":":
61
+ if not output_dir or path.lower() == "stdout":
67
62
  return path
68
- return str(Path(output_dir) / path)
63
+ return safe_output_path(path, output_dir)
69
64
 
70
65
 
71
66
  # ---------------------------------------------------------------------------
@@ -212,12 +207,12 @@ def x_export(**kwargs: Any) -> None:
212
207
 
213
208
  def x_export_query(**kwargs: Any) -> None:
214
209
  select_stmt = kwargs["query"]
215
- outfile = kwargs["filename"]
210
+ outfile = _apply_output_dir(kwargs["filename"])
216
211
  description = kwargs["description"]
217
212
  tee = bool(kwargs["tee"])
218
213
  append = bool(kwargs["append"])
219
214
  filefmt = kwargs["format"].lower()
220
- zipfilename = kwargs["zipfilename"]
215
+ zipfilename = _apply_output_dir(kwargs["zipfilename"]) if kwargs["zipfilename"] else None
221
216
  notype = bool(kwargs.get("notype"))
222
217
  _check_zip_compat(outfile, filefmt, zipfilename)
223
218
  check_dir(outfile)
@@ -242,13 +237,13 @@ def x_export_query(**kwargs: Any) -> None:
242
237
 
243
238
  def x_export_query_with_template(**kwargs: Any) -> None:
244
239
  select_stmt = kwargs["query"]
245
- outfile = kwargs["filename"]
240
+ outfile = _apply_output_dir(kwargs["filename"])
246
241
  template_file = kwargs["template"]
247
242
  tee = kwargs["tee"]
248
243
  tee = bool(tee)
249
244
  append = kwargs["append"]
250
245
  append = bool(append)
251
- zipfilename = kwargs["zipfilename"]
246
+ zipfilename = _apply_output_dir(kwargs["zipfilename"]) if kwargs["zipfilename"] else None
252
247
  check_dir(outfile)
253
248
  if tee and outfile.lower() != "stdout":
254
249
  prettyprint_query(select_stmt, _state.dbs.current(), "stdout", False)
@@ -262,13 +257,13 @@ def x_export_with_template(**kwargs: Any) -> None:
262
257
  table = kwargs["table"]
263
258
  queryname = _state.dbs.current().schema_qualified_table_name(schema, table)
264
259
  select_stmt = f"select * from {queryname};"
265
- outfile = kwargs["filename"]
260
+ outfile = _apply_output_dir(kwargs["filename"])
266
261
  template_file = kwargs["template"]
267
262
  tee = kwargs["tee"]
268
263
  tee = bool(tee)
269
264
  append = kwargs["append"]
270
265
  append = bool(append)
271
- zipfilename = kwargs["zipfilename"]
266
+ zipfilename = _apply_output_dir(kwargs["zipfilename"]) if kwargs["zipfilename"] else None
272
267
  check_dir(outfile)
273
268
  if tee and outfile.lower() != "stdout":
274
269
  prettyprint_query(select_stmt, _state.dbs.current(), "stdout", False)
@@ -279,7 +274,7 @@ def x_export_with_template(**kwargs: Any) -> None:
279
274
 
280
275
  def x_export_ods_multiple(**kwargs: Any) -> None:
281
276
  table_list = kwargs["tables"]
282
- outfile = kwargs["filename"]
277
+ outfile = _apply_output_dir(kwargs["filename"])
283
278
  description = kwargs["description"]
284
279
  tee = kwargs["tee"]
285
280
  tee = bool(tee)
@@ -292,7 +287,7 @@ def x_export_ods_multiple(**kwargs: Any) -> None:
292
287
  def x_export_xlsx_multiple(**kwargs: Any) -> None:
293
288
  """Export multiple tables to separate worksheets in a single XLSX workbook."""
294
289
  table_list = kwargs["tables"]
295
- outfile = kwargs["filename"]
290
+ outfile = _apply_output_dir(kwargs["filename"])
296
291
  description = kwargs["description"]
297
292
  tee = kwargs["tee"]
298
293
  tee = bool(tee)
@@ -303,10 +298,10 @@ def x_export_xlsx_multiple(**kwargs: Any) -> None:
303
298
 
304
299
 
305
300
  def x_export_metadata(**kwargs: Any) -> None:
306
- outfile = kwargs["filename"]
301
+ outfile = _apply_output_dir(kwargs["filename"])
307
302
  append = kwargs["append"] is not None
308
303
  xall = kwargs["all"] is not None
309
- zipfilename = kwargs["zipfilename"]
304
+ zipfilename = _apply_output_dir(kwargs["zipfilename"]) if kwargs["zipfilename"] else None
310
305
  filefmt = kwargs["format"].lower()
311
306
  if xall:
312
307
  hdrs, rows = _state.export_metadata.get_all()
@@ -17,7 +17,6 @@ import execsql.state as _state
17
17
  from execsql.exceptions import ErrInfo
18
18
  from execsql.models import DataTable
19
19
  from execsql.script import current_script_line
20
- from execsql.types import dbt_firebird
21
20
  from execsql.utils.errors import exception_desc
22
21
  from execsql.utils.fileio import filewriter_close
23
22
  from execsql.utils.strings import unquoted
@@ -95,7 +94,7 @@ def x_copy(**kwargs: Any) -> None:
95
94
  except Exception:
96
95
  _state.exec_log.log_status_info(f"Could not drop existing table ({tbl2}) for COPY metacommand")
97
96
  db2.execute(create_tbl)
98
- if db2.type == dbt_firebird:
97
+ if db2.needs_explicit_commit_after_ddl():
99
98
  db2.execute("COMMIT;")
100
99
  try:
101
100
  hdrs, rows = db1.select_rowsource(select_stmt)
@@ -169,7 +168,7 @@ def x_copy_query(**kwargs: Any) -> None:
169
168
  except Exception:
170
169
  _state.exec_log.log_status_info(f"Could not drop existing table ({tbl2}) for COPY metacommand")
171
170
  db2.execute(create_tbl)
172
- if db2.type == dbt_firebird:
171
+ if db2.needs_explicit_commit_after_ddl():
173
172
  db2.execute("COMMIT;")
174
173
  try:
175
174
  hdrs, rows = db1.select_rowsource(select_stmt)
@@ -209,6 +208,13 @@ def x_zip_buffer_mb(**kwargs: Any) -> None:
209
208
  def x_rm_file(**kwargs: Any) -> None:
210
209
  import glob as _glob
211
210
 
211
+ if not getattr(_state.conf, "allow_rm_file", True):
212
+ raise ErrInfo(
213
+ type="cmd",
214
+ command_text=kwargs.get("metacommandline", "RM_FILE"),
215
+ other_msg="The RM_FILE metacommand is disabled (--no-rm-file).",
216
+ )
217
+
212
218
  fn = kwargs["filename"].strip(' "')
213
219
  fnlist = _glob.glob(fn)
214
220
  for f in fnlist:
@@ -249,7 +255,19 @@ def x_hdf5_text_len(**kwargs: Any) -> None:
249
255
 
250
256
 
251
257
  def x_serve(**kwargs: Any) -> None:
258
+ from execsql.utils.fileio import safe_output_path
259
+
260
+ if not getattr(_state.conf, "allow_serve", True):
261
+ raise ErrInfo(
262
+ type="cmd",
263
+ command_text=kwargs.get("metacommandline", "SERVE"),
264
+ other_msg="The SERVE metacommand is disabled (--no-serve).",
265
+ )
266
+
252
267
  infname = kwargs["filename"]
268
+ serve_root = getattr(_state.conf, "serve_root", None)
269
+ if serve_root:
270
+ infname = safe_output_path(infname, serve_root)
253
271
  fmt = kwargs["format"].lower()
254
272
  if not Path(infname).is_file():
255
273
  raise ErrInfo(
@@ -8,7 +8,7 @@
8
8
  - ``x_import_xls`` / ``x_import_xls_pattern`` — same for XLS/XLSX.
9
9
  - ``x_import_parquet`` — IMPORT … FROM PARQUET (via polars).
10
10
  - ``x_import_feather`` — IMPORT … FROM FEATHER (via polars).
11
- - ``x_import_json`` — IMPORT … FROM JSON (array of objects or NDJSON).
11
+ - ``x_import_json`` — IMPORT … FROM JSON (array of objects or JSON Lines).
12
12
  - ``x_import_row_buffer`` — CONFIG IMPORT_ROW_BUFFER.
13
13
  - ``x_show_progress`` — CONFIG SHOW_PROGRESS (toggle the import progress bar).
14
14
  """
@@ -163,7 +163,18 @@ def x_import_ods_pattern(**kwargs: Any) -> None:
163
163
  is_new = 0
164
164
  schemaname = kwargs["schema"]
165
165
  filename = kwargs["filename"]
166
- rx = re.compile(kwargs["patn"], re.I)
166
+ # B18/F012: surface a friendly error for malformed regex patterns
167
+ # rather than letting an uncaught re.error bubble up. (ReDoS via
168
+ # catastrophic backtracking remains a documented risk — re2 is
169
+ # not in stdlib so we can't enforce a complexity cap here.)
170
+ try:
171
+ rx = re.compile(kwargs["patn"], re.I)
172
+ except re.error as exc:
173
+ raise ErrInfo(
174
+ type="cmd",
175
+ command_text=kwargs.get("metacommandline", "IMPORT ODS PATTERN"),
176
+ other_msg=f"Invalid regular expression {kwargs['patn']!r}: {exc}",
177
+ ) from exc
167
178
  hdr_rows = kwargs["skip"]
168
179
  if not hdr_rows:
169
180
  hdr_rows = 0
@@ -260,7 +271,16 @@ def x_import_xls_pattern(**kwargs: Any) -> None:
260
271
  is_new = 0
261
272
  schemaname = kwargs["schema"]
262
273
  filename = kwargs["filename"]
263
- rx = re.compile(kwargs["patn"], re.I)
274
+ # B18/F012: surface a friendly error for malformed regex patterns
275
+ # (see x_import_ods_pattern for the full rationale).
276
+ try:
277
+ rx = re.compile(kwargs["patn"], re.I)
278
+ except re.error as exc:
279
+ raise ErrInfo(
280
+ type="cmd",
281
+ command_text=kwargs.get("metacommandline", "IMPORT XLS PATTERN"),
282
+ other_msg=f"Invalid regular expression {kwargs['patn']!r}: {exc}",
283
+ ) from exc
264
284
  hdr_rows = kwargs["skip"]
265
285
  encoding = kwargs["encoding"]
266
286
  if not hdr_rows:
execsql/script/ast.py CHANGED
@@ -483,6 +483,14 @@ def _format_nodes(nodes: list[Node], lines: list[str], prefix: str) -> None:
483
483
  _format_if_block(node, lines, child_prefix)
484
484
  elif isinstance(node, (LoopBlock, BatchBlock, ScriptBlock, SqlBlock)):
485
485
  _format_nodes(node.body, lines, child_prefix)
486
+ elif isinstance(node, (SqlStatement, MetaCommandStatement, Comment, IncludeDirective)):
487
+ # Leaf nodes — no children to render.
488
+ pass
489
+ else:
490
+ # Guard against silently skipping bodies of future block-type nodes.
491
+ raise NotImplementedError(
492
+ f"_format_nodes does not handle {type(node).__name__}",
493
+ )
486
494
 
487
495
 
488
496
  def _format_if_block(node: IfBlock, lines: list[str], prefix: str) -> None:
execsql/script/engine.py CHANGED
@@ -442,6 +442,14 @@ def set_system_vars(ctx: Any = None) -> None:
442
442
 
443
443
 
444
444
  _MAX_SUBSTITUTION_DEPTH = 100
445
+ # B17/F013: output-byte ceiling for substitute_vars. The depth cap
446
+ # above stops cyclic references (a → !!b!!, b → !!a!!) but does not
447
+ # stop exponential expansion: a single variable referencing the same
448
+ # other variable N times grows ``2^N`` per iteration without ever
449
+ # looping. Track the rendered length and abort when it crosses
450
+ # _MAX_SUBSTITUTION_BYTES (default 10 MB — well above any legitimate
451
+ # SQL statement, comfortably below a memory-pressure failure mode).
452
+ _MAX_SUBSTITUTION_BYTES = 10 * 1024 * 1024
445
453
 
446
454
 
447
455
  def substitute_vars(command_str: str, localvars: SubVarSet | None = None, ctx: Any = None) -> str:
@@ -452,12 +460,23 @@ def substitute_vars(command_str: str, localvars: SubVarSet | None = None, ctx: A
452
460
  localvars: Optional local variable overlay to merge with globals.
453
461
  ctx: Optional :class:`RuntimeContext`. When ``None``, falls through
454
462
  to the global ``_state`` module (legacy behavior).
463
+
464
+ Raises:
465
+ ErrInfo: when the iteration count exceeds
466
+ :data:`_MAX_SUBSTITUTION_DEPTH` (cyclic reference) OR the
467
+ expanded output exceeds :data:`_MAX_SUBSTITUTION_BYTES`
468
+ (exponential expansion bomb).
455
469
  """
456
470
  _s = ctx if ctx is not None else _state
457
471
  if localvars is not None:
458
472
  subs = _s.subvars.merge(localvars)
459
473
  else:
460
474
  subs = _s.subvars
475
+ # Allow runtime override of the byte cap via conf. None / missing
476
+ # → use the engine default (back-compat with users who legitimately
477
+ # render multi-MB SQL through substitution).
478
+ conf_max = getattr(_s.conf, "max_substitution_bytes", None)
479
+ max_bytes = conf_max if conf_max is not None else _MAX_SUBSTITUTION_BYTES
461
480
  cmdstr = command_str
462
481
  subs_made = True
463
482
  iterations = 0
@@ -467,6 +486,19 @@ def substitute_vars(command_str: str, localvars: SubVarSet | None = None, ctx: A
467
486
  cmdstr, any_subbed = _s.counters.substitute_all(cmdstr)
468
487
  subs_made = subs_made or any_subbed
469
488
  iterations += 1
489
+ # Only enforce the byte cap when expansion ACTUALLY happened
490
+ # this iteration. A user passing a large pre-existing literal
491
+ # with no !!var!! tokens shouldn't be rejected — the cap
492
+ # targets expansion-bomb growth, not literal input size.
493
+ if subs_made and len(cmdstr) > max_bytes:
494
+ raise ErrInfo(
495
+ type="error",
496
+ other_msg=(
497
+ f"Substitution variable expansion exceeded {max_bytes} bytes "
498
+ f"(possible exponential expansion bomb) while expanding: "
499
+ f"{command_str[:200]}"
500
+ ),
501
+ )
470
502
  if iterations >= _MAX_SUBSTITUTION_DEPTH:
471
503
  raise ErrInfo(
472
504
  type="error",
@@ -783,6 +783,18 @@ def _execute_include_native(
783
783
  if len(target) > 1 and target[0] == "~" and target[1] == os.sep:
784
784
  target = str(Path.home() / target[2:])
785
785
 
786
+ # Optional containment: when conf.include_root is set, the resolved
787
+ # INCLUDE / EXECUTE SCRIPT target must live under that root.
788
+ include_root = getattr(ctx.conf, "include_root", None) if hasattr(ctx, "conf") else None
789
+ if include_root is None:
790
+ import execsql.state as _state
791
+
792
+ include_root = getattr(_state.conf, "include_root", None)
793
+ if include_root:
794
+ from execsql.utils.fileio import safe_output_path
795
+
796
+ target = safe_output_path(target, include_root)
797
+
786
798
  target_path = Path(target)
787
799
 
788
800
  # IF EXISTS handling
@@ -9,7 +9,6 @@ Classes:
9
9
  - :class:`ScriptArgSubVarSet` — per-script ``#``-prefixed argument overlay.
10
10
  """
11
11
 
12
- import os
13
12
  import re
14
13
  from typing import Any
15
14
 
@@ -244,17 +243,31 @@ class SubVarSet:
244
243
  if sub is None:
245
244
  sub = ""
246
245
  sub = str(sub)
247
- if os.name != "posix":
248
- sub = sub.replace("\\", "\\\\")
246
+ # B07a/F002: reject embedded NUL bytes regardless of quoter;
247
+ # most DBMS protocols truncate at NUL and PostgreSQL rejects.
248
+ if "\x00" in sub:
249
+ raise ValueError(
250
+ f"Substitution variable {varname!r} contains a NUL byte; refusing to interpolate.",
251
+ )
249
252
  quote = m.group("q")
250
253
  if quote == "'":
251
- # Wrap value in single quotes, doubling any embedded
252
- # apostrophe produces a SQL string literal.
253
- sub = "'" + sub.replace("'", "''") + "'"
254
+ # B07a/F002: wrap in single quotes, doubling embedded
255
+ # apostrophes AND escaping backslashes so MySQL default
256
+ # mode and PostgreSQL E-string literals can't end the
257
+ # quoted region via ``\'``. The previous Windows-only
258
+ # branch (``os.name != 'posix'``) applied the escape on
259
+ # the wrong axis — host OS vs target DBMS.
260
+ sub = "'" + sub.replace("\\", "\\\\").replace("'", "''") + "'"
254
261
  elif quote == '"':
255
- # Wrap value in double quotes produces a SQL quoted
256
- # identifier or quoted metacommand argument.
257
- sub = '"' + sub + '"'
262
+ # B07a/F001: wrap in double quotes, doubling embedded
263
+ # ``"`` so a value containing ``"; DROP TABLE x; --``
264
+ # produces a valid quoted identifier rather than a
265
+ # closing quote followed by a second statement.
266
+ sub = '"' + sub.replace('"', '""') + '"'
267
+ else:
268
+ # Bare !!var!! token — preserve the raw value verbatim
269
+ # but still defend against NUL bytes (handled above).
270
+ pass
258
271
  return command_str[: m.start()] + sub + command_str[m.end() :], True
259
272
  # Token found but variable not defined — skip it and keep searching.
260
273
  m = self._TOKEN_RX.search(command_str, m.end())
@@ -270,32 +283,45 @@ class SubVarSet:
270
283
  compilation to avoid O(N) ``re.compile`` calls on every invocation.
271
284
  """
272
285
  cmd_lower = command_str.lower()
286
+
287
+ def _check_nul(name: str, value: str) -> None:
288
+ """B07a/F002: raise when *value* (about to be interpolated)
289
+ contains a NUL byte. Called only after a token match so an
290
+ unrelated variable's NUL value never blocks substitution of
291
+ a different variable."""
292
+ if "\x00" in value:
293
+ raise ValueError(
294
+ f"Substitution variable {name!r} contains a NUL byte; refusing to interpolate.",
295
+ )
296
+
273
297
  for varname, sub in self._subs_dict.items():
274
298
  if sub is None:
275
299
  sub = ""
276
300
  sub = str(sub)
277
- if os.name != "posix":
278
- sub = sub.replace("\\", "\\\\")
279
301
  # Standard token: !!varname!!
280
302
  token = f"!!{varname}!!"
281
303
  idx = cmd_lower.find(token)
282
304
  if idx != -1:
305
+ _check_nul(varname, sub)
283
306
  return command_str[:idx] + sub + command_str[idx + len(token) :], True
284
- # Single-quote-wrapped token: !'!varname!'!
307
+ # Single-quote-wrapped token: !'!varname!'! — escape ``\`` and
308
+ # double embedded ``'`` (see substitute_one for rationale).
285
309
  tokenq = f"!'!{varname}!'!"
286
310
  idxq = cmd_lower.find(tokenq)
287
311
  if idxq != -1:
288
- wrapped = "'" + sub.replace("'", "''") + "'"
312
+ _check_nul(varname, sub)
313
+ wrapped = "'" + sub.replace("\\", "\\\\").replace("'", "''") + "'"
289
314
  return (
290
315
  command_str[:idxq] + wrapped + command_str[idxq + len(tokenq) :],
291
316
  True,
292
317
  )
293
- # Double-quote-wrapped token: !"!varname!"!
318
+ # Double-quote-wrapped token: !"!varname!"! — double embedded ``"``.
294
319
  tokendq = f'!"!{varname}!"!'
295
320
  idxdq = cmd_lower.find(tokendq)
296
321
  if idxdq != -1:
322
+ _check_nul(varname, sub)
297
323
  return (
298
- command_str[:idxdq] + '"' + sub + '"' + command_str[idxdq + len(tokendq) :],
324
+ command_str[:idxdq] + '"' + sub.replace('"', '""') + '"' + command_str[idxdq + len(tokendq) :],
299
325
  True,
300
326
  )
301
327
  return command_str, False
execsql/utils/auth.py CHANGED
@@ -29,11 +29,56 @@ import getpass
29
29
 
30
30
  import execsql.state as _state
31
31
 
32
- __all__ = ["get_password", "clear_stored_password", "password_from_keyring"]
32
+ __all__ = ["clear_stored_password", "get_password", "is_plaintext_keyring", "password_from_keyring"]
33
33
 
34
34
  # Tracks whether the most recent get_password() call returned a keyring-stored value.
35
35
  _last_from_keyring: bool = False
36
36
 
37
+ # Tracks whether we've already warned about a plaintext keyring backend
38
+ # this process — keyring is a one-shot warning, not a per-call nag.
39
+ _plaintext_warned: bool = False
40
+
41
+
42
+ def is_plaintext_keyring() -> bool:
43
+ """Return True if the active keyring backend stores secrets in cleartext.
44
+
45
+ B20/F040: on headless Linux without a real Secret Service, the
46
+ ``keyrings.alt.file.PlaintextKeyring`` (or
47
+ ``EncryptedKeyring`` with a hard-coded passphrase) backend is
48
+ used silently. Detect by inspecting the active backend's module
49
+ path so callers can warn the user instead of pretending secrets
50
+ are encrypted.
51
+ """
52
+ try:
53
+ import keyring
54
+
55
+ backend = keyring.get_keyring()
56
+ module = type(backend).__module__
57
+ return "keyrings.alt" in module or "fail" in module.lower()
58
+ except Exception:
59
+ return False
60
+
61
+
62
+ def _warn_if_plaintext_keyring() -> None:
63
+ """Print a one-time warning if the active keyring backend is plaintext."""
64
+ global _plaintext_warned
65
+ if _plaintext_warned or not is_plaintext_keyring():
66
+ return
67
+ _plaintext_warned = True
68
+ try:
69
+ import keyring
70
+ import sys
71
+
72
+ backend_name = type(keyring.get_keyring()).__name__
73
+ print(
74
+ f"WARNING: active keyring backend ({backend_name}) stores secrets in "
75
+ f"cleartext or with a hard-coded key. Stored passwords are not "
76
+ f"meaningfully protected.",
77
+ file=sys.stderr,
78
+ )
79
+ except Exception:
80
+ pass
81
+
37
82
 
38
83
  def _keyring_service(dbms_name: str, database_name: str, server_name: str | None) -> str:
39
84
  """Build a keyring service name from connection parameters."""
@@ -53,6 +98,9 @@ def _keyring_get(service: str, username: str) -> str | None:
53
98
 
54
99
  def _keyring_set(service: str, username: str, password: str) -> bool:
55
100
  """Try to store a password in the OS keyring. Returns True on success."""
101
+ # B20/F040: warn before storing into a plaintext backend so the user
102
+ # knows their secret is not meaningfully protected at rest.
103
+ _warn_if_plaintext_keyring()
56
104
  try:
57
105
  import keyring
58
106