half-orm 0.18.13__tar.gz → 0.18.14__tar.gz

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 (37) hide show
  1. {half_orm-0.18.13/half_orm.egg-info → half_orm-0.18.14}/PKG-INFO +13 -2
  2. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/__main__.py +7 -3
  3. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/cli.py +129 -34
  4. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/field.py +68 -3
  5. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/hotest.py +18 -1
  6. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/model.py +166 -26
  7. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/pg_meta.py +17 -8
  8. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/relation.py +69 -34
  9. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/transaction.py +25 -3
  10. half_orm-0.18.14/half_orm/version.txt +1 -0
  11. {half_orm-0.18.13 → half_orm-0.18.14/half_orm.egg-info}/PKG-INFO +13 -2
  12. {half_orm-0.18.13 → half_orm-0.18.14}/test/test_cli.py +185 -6
  13. half_orm-0.18.13/half_orm/version.txt +0 -1
  14. {half_orm-0.18.13 → half_orm-0.18.14}/AUTHORS +0 -0
  15. {half_orm-0.18.13 → half_orm-0.18.14}/LICENSE +0 -0
  16. {half_orm-0.18.13 → half_orm-0.18.14}/README.md +0 -0
  17. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/__init__.py +0 -0
  18. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/cli_utils.py +0 -0
  19. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/field_errors.py +0 -0
  20. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/fkey.py +0 -0
  21. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/model_errors.py +0 -0
  22. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/null.py +0 -0
  23. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/relation_errors.py +0 -0
  24. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/relation_factory.py +0 -0
  25. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/sql_adapter.py +0 -0
  26. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/sql_ast.py +0 -0
  27. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm/utils.py +0 -0
  28. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm.egg-info/SOURCES.txt +0 -0
  29. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm.egg-info/dependency_links.txt +0 -0
  30. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm.egg-info/entry_points.txt +0 -0
  31. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm.egg-info/requires.txt +0 -0
  32. {half_orm-0.18.13 → half_orm-0.18.14}/half_orm.egg-info/top_level.txt +0 -0
  33. {half_orm-0.18.13 → half_orm-0.18.14}/pyproject.toml +0 -0
  34. {half_orm-0.18.13 → half_orm-0.18.14}/setup.cfg +0 -0
  35. {half_orm-0.18.13 → half_orm-0.18.14}/setup.py +0 -0
  36. {half_orm-0.18.13 → half_orm-0.18.14}/test/test_main.py +0 -0
  37. {half_orm-0.18.13 → half_orm-0.18.14}/test/test_sql_ast.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: half_orm
3
- Version: 0.18.13
3
+ Version: 0.18.14
4
4
  Summary: A simple PostgreSQL to Python mapper.
5
5
  Home-page: https://github.com/half-orm/half-orm
6
6
  Author: Joël Maïzi
@@ -29,6 +29,17 @@ License-File: AUTHORS
29
29
  Requires-Dist: psycopg[binary]
30
30
  Requires-Dist: click
31
31
  Requires-Dist: importlib-metadata; python_version < "3.8"
32
+ Dynamic: author
33
+ Dynamic: author-email
34
+ Dynamic: classifier
35
+ Dynamic: description
36
+ Dynamic: description-content-type
37
+ Dynamic: home-page
38
+ Dynamic: license
39
+ Dynamic: license-file
40
+ Dynamic: project-url
41
+ Dynamic: requires-dist
42
+ Dynamic: summary
32
43
 
33
44
  # halfORM
34
45
 
@@ -40,7 +40,7 @@ def check_databases_access():
40
40
  try:
41
41
  model = Model(cnx_file)
42
42
  print(f"✅ {cnx_file}")
43
- except (psycopg.Error, model_errors.MalformedConfigFile) as exc:
43
+ except (psycopg.Error, model_errors.MalformedConfigFile, ValueError) as exc:
44
44
  sys.stderr.write(f'❌ {cnx_file}: {exc}\n')
45
45
  else:
46
46
  print("💡 No database configuration files found.")
@@ -77,10 +77,11 @@ def main():
77
77
  if database == '':
78
78
  sys.stderr.write("❌ Empty database name\n")
79
79
  sys.exit(1)
80
+ return
80
81
  try:
81
82
  model = Model(database)
82
83
  print(model)
83
- except (psycopg.Error, model_errors.MalformedConfigFile) as exc:
84
+ except (psycopg.Error, model_errors.MalformedConfigFile, ValueError) as exc:
84
85
  sys.stderr.write(f"❌ Error connecting to database '{database}': {exc}\n")
85
86
  sys.stderr.write(f"💡 Use 'python -m half_orm --help' for usage information\n")
86
87
  sys.exit(1)
@@ -92,14 +93,17 @@ def main():
92
93
  if database == '':
93
94
  sys.stderr.write("❌ Empty database name\n")
94
95
  sys.exit(1)
96
+ return
95
97
  if relation == '':
96
98
  sys.stderr.write("❌ Empty relation name\n")
97
99
  sys.exit(1)
100
+ return
98
101
  try:
99
102
  model = Model(database)
100
103
  relation_class = model.get_relation_class(relation)
101
104
  print(relation_class())
102
- except (psycopg.Error, model_errors.MalformedConfigFile, model_errors.UnknownRelation, model_errors.MissingSchemaInName) as exc:
105
+ except (psycopg.Error, model_errors.MalformedConfigFile, ValueError,
106
+ model_errors.UnknownRelation, model_errors.MissingSchemaInName) as exc:
103
107
  sys.stderr.write(f"❌ Error accessing relation '{relation}' in database '{database}': {exc}\n")
104
108
  sys.stderr.write(f"💡 Use 'python -m half_orm --help' for usage information\n")
105
109
  sys.exit(1)
@@ -83,34 +83,113 @@ _cached_extensions = None
83
83
  _trust_extensions = False
84
84
 
85
85
  # Liste des extensions officielles
86
+ # Extensions trusted without asking. Every name here must be a project the
87
+ # halfORM maintainers actually own on PyPI: an unclaimed name on this list is
88
+ # a free pass for whoever registers it first.
86
89
  OFFICIAL_EXTENSIONS = {
87
- 'half_orm_test_extension',
88
90
  'half_orm_inspect',
89
91
  'half_orm_dev',
90
92
  }
91
93
 
94
+ # Pre-1.0 trust store, kept only to warn that it is no longer honoured.
95
+ LEGACY_CONFIG_NAME = '.half_orm_cli'
96
+
97
+ # Directory entries that mark the root of a project.
98
+ PROJECT_MARKERS = ('.hop', '.git')
99
+
100
+ _legacy_config_warned = False
101
+
92
102
  def get_config_file():
93
- """Get path to project-specific halfORM CLI configuration."""
94
- return Path.cwd() / '.half_orm_cli'
103
+ """Get path to the per-user halfORM CLI configuration.
104
+
105
+ This file holds the extension trust store, which suppresses the security
106
+ prompt raised for unofficial extensions. It therefore lives outside any
107
+ project directory: a checkout is untrusted input, and a repository able to
108
+ write its own trust store would silently grant itself consent.
109
+
110
+ Honours HALF_ORM_CLI_CONFIG, then the platform's per-user configuration
111
+ directory: %APPDATA% on Windows, $XDG_CONFIG_HOME (default ~/.config)
112
+ elsewhere. Both live inside the user's profile, which is what keeps the
113
+ file confidential -- the explicit 0600 applied on save is a Unix-only
114
+ reinforcement, where a permissive umask could otherwise widen it.
115
+ """
116
+ override = os.environ.get('HALF_ORM_CLI_CONFIG')
117
+ if override:
118
+ return Path(override).expanduser()
119
+
120
+ if sys.platform == 'win32':
121
+ appdata = os.environ.get('APPDATA')
122
+ base = Path(appdata) if appdata else Path.home() / 'AppData' / 'Roaming'
123
+ else:
124
+ xdg = os.environ.get('XDG_CONFIG_HOME')
125
+ base = Path(xdg).expanduser() if xdg else Path.home() / '.config'
126
+ return base / 'half_orm' / 'cli.json'
127
+
128
+ def get_project_key(start=None):
129
+ """Identify the project a trust decision applies to.
130
+
131
+ Walks up from `start` (default: the current directory) to the nearest
132
+ project marker, so that running a command from a subdirectory reuses the
133
+ decision already recorded for the project instead of asking again -- a
134
+ prompt that keeps reappearing is a prompt that gets answered unread.
135
+ """
136
+ try:
137
+ current = Path(start) if start else Path.cwd()
138
+ current = current.resolve()
139
+ except OSError:
140
+ return os.path.normcase(str(start or ''))
141
+
142
+ for directory in (current, *current.parents):
143
+ for marker in PROJECT_MARKERS:
144
+ if (directory / marker).exists():
145
+ return os.path.normcase(str(directory))
146
+ # normcase is a no-op on POSIX; on Windows it keeps C:\Proj and c:\proj
147
+ # from becoming two separate trust scopes for one directory.
148
+ return os.path.normcase(str(current))
149
+
150
+ def _warn_legacy_config():
151
+ """Warn once that a pre-1.0 project-local trust store is being ignored."""
152
+ global _legacy_config_warned
153
+ if _legacy_config_warned:
154
+ return
155
+ _legacy_config_warned = True
156
+
157
+ try:
158
+ legacy = Path.cwd() / LEGACY_CONFIG_NAME
159
+ if not legacy.exists():
160
+ return
161
+ except OSError:
162
+ return
163
+
164
+ click.echo(
165
+ f"⚠️ Ignoring '{legacy}': extension trust is no longer read from the "
166
+ "project directory, where a checkout could grant itself consent.",
167
+ err=True)
168
+ click.echo(f" Trust is now recorded in {get_config_file()}", err=True)
95
169
 
96
170
  def load_cli_config():
97
- """Load project-specific CLI configuration."""
98
- config_file = get_config_file()
99
- if config_file.exists():
100
- try:
101
- with open(config_file, 'r') as f:
102
- return json.load(f)
103
- except (json.JSONDecodeError, OSError):
104
- return {}
105
- return {}
171
+ """Load the per-user CLI configuration."""
172
+ _warn_legacy_config()
173
+ try:
174
+ with open(get_config_file(), 'r') as f:
175
+ config = json.load(f)
176
+ except (json.JSONDecodeError, OSError):
177
+ return {}
178
+ return config if isinstance(config, dict) else {}
106
179
 
107
180
  def save_cli_config(config):
108
- """Save project-specific CLI configuration."""
181
+ """Save the per-user CLI configuration, readable by its owner only."""
109
182
  config_file = get_config_file()
110
183
  try:
184
+ config_file.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
111
185
  config['last_updated'] = datetime.now().isoformat()
112
- with open(config_file, 'w') as f:
186
+ fd = os.open(config_file, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
187
+ with os.fdopen(fd, 'w') as f:
113
188
  json.dump(config, f, indent=2)
189
+ # O_CREAT only applies the mode on creation; an existing file keeps its
190
+ # own, so narrow it explicitly. On Windows this only clears the
191
+ # read-only flag, and confidentiality comes from the profile ACL.
192
+ os.chmod(config_file, 0o600)
114
193
  return True
115
194
  except OSError:
116
195
  return False
@@ -142,41 +221,57 @@ def is_official_extension(package_name):
142
221
  hyph_package_name = package_name.replace('-', '_')
143
222
  return package_name in OFFICIAL_EXTENSIONS or hyph_package_name in OFFICIAL_EXTENSIONS
144
223
 
145
- def is_trusted_extension(package_name, current_version=None):
146
- """Check if specific version of extension is trusted."""
147
- config = load_cli_config()
148
- trusted_extensions = config.get('trusted_extensions', {})
224
+ def _trusted_for_project(config, project_key):
225
+ """Return the trust entries recorded for one project.
149
226
 
150
- if package_name in trusted_extensions:
151
- trusted_version = trusted_extensions[package_name].get('version')
152
- return trusted_version == current_version
227
+ Tolerates a malformed store rather than raising: a corrupt file must not
228
+ break the CLI, and every shape it can take here means "not trusted".
229
+ """
230
+ trusted = config.get('trusted_extensions')
231
+ if not isinstance(trusted, dict):
232
+ return {}
233
+ entries = trusted.get(project_key)
234
+ return entries if isinstance(entries, dict) else {}
153
235
 
154
- return False
236
+ def is_trusted_extension(package_name, current_version=None):
237
+ """Check if this version of an extension is trusted for this project."""
238
+ entries = _trusted_for_project(load_cli_config(), get_project_key())
239
+ entry = entries.get(package_name)
240
+ if not isinstance(entry, dict):
241
+ return False
242
+ return entry.get('version') == current_version
155
243
 
156
244
  def add_trusted_extension(package_name, version):
157
- """Add specific version of extension to trusted list."""
245
+ """Trust a specific version of an extension, for this project only."""
158
246
  config = load_cli_config()
159
- trusted = config.get('trusted_extensions', {})
247
+ project_key = get_project_key()
160
248
 
161
- trusted[package_name] = {
249
+ trusted = config.get('trusted_extensions')
250
+ if not isinstance(trusted, dict):
251
+ trusted = {}
252
+ entries = dict(_trusted_for_project(config, project_key))
253
+ entries[package_name] = {
162
254
  'version': version,
163
255
  'trusted_at': datetime.now().isoformat()
164
256
  }
165
257
 
258
+ trusted[project_key] = entries
166
259
  config['trusted_extensions'] = trusted
167
260
  save_cli_config(config)
168
261
 
169
262
  def remove_trusted_extension(package_name):
170
- """Remove extension from trusted list."""
263
+ """Remove an extension from this project's trusted list."""
171
264
  config = load_cli_config()
172
- trusted = config.get('trusted_extensions', {})
265
+ project_key = get_project_key()
266
+ entries = dict(_trusted_for_project(config, project_key))
173
267
 
174
- if package_name in trusted:
175
- del trusted[package_name]
176
- config['trusted_extensions'] = trusted
177
- save_cli_config(config)
178
- return True
179
- return False
268
+ if package_name not in entries:
269
+ return False
270
+
271
+ del entries[package_name]
272
+ config['trusted_extensions'][project_key] = entries
273
+ save_cli_config(config)
274
+ return True
180
275
 
181
276
  def warn_unofficial_extension(package_name, current_version):
182
277
  """Show warning for non-official extensions."""
@@ -309,7 +404,7 @@ def get_extension_info(extensions: Dict[str, Any]) -> str:
309
404
  )
310
405
  @click.version_option(version=half_orm.__version__, prog_name='halfORM')
311
406
  @click.option('--list-extensions', is_flag=True, help='List all installed extensions')
312
- @click.option('--untrust', metavar='EXTENSION', help='Remove extension from trusted list')
407
+ @click.option('--untrust', metavar='EXTENSION', help="Remove extension from this project's trusted list")
313
408
  @click.option('--trusted-extensions', is_flag=True, help='Skip security warnings')
314
409
  @click.pass_context
315
410
  def main(ctx, list_extensions, untrust, trusted_extensions):
@@ -12,6 +12,57 @@ from half_orm.sql_adapter import SQL_ADAPTER
12
12
  from half_orm.sql_ast import FieldExpr
13
13
 
14
14
 
15
+ # Comparators are interpolated into the WHERE clause, not bound as parameters,
16
+ # so they must be constrained to things that can only ever *be* an operator.
17
+ #
18
+ # Word operators are enumerated. Symbolic ones are accepted by pattern rather
19
+ # than by list, because PostgreSQL extensions define their own -- pgvector's
20
+ # `<->`, PostGIS's `&&&`, pg_trgm's `%` -- and halfORM has always let those
21
+ # through. The pattern below is PostgreSQL's own operator character set: a
22
+ # comparator matching it cannot contain a space, a quote or a letter, so it
23
+ # cannot close the condition and start another one.
24
+ _WORD_COMPS = frozenset({
25
+ 'in', 'not in',
26
+ 'is', 'is not',
27
+ 'is distinct from', 'is not distinct from',
28
+ 'like', 'not like', 'ilike', 'not ilike',
29
+ 'similar to', 'not similar to',
30
+ })
31
+
32
+ _SYMBOLIC_COMP_RE = re.compile(r'^[-+*/<>=~!@#%^&|`?]{1,32}$')
33
+
34
+ # The operator character set can also spell the SQL comment openers, which
35
+ # would let a comparator swallow the rest of the condition.
36
+ _COMMENT_TOKENS = ('--', '/*', '*/')
37
+
38
+
39
+ def check_comparator(comp: str) -> str:
40
+ """Validate `comp` as a comparison operator and return its normalized form.
41
+
42
+ Accepts the SQL word operators listed in ``_WORD_COMPS`` (case- and
43
+ whitespace-insensitive) and any operator spelled with PostgreSQL's
44
+ operator characters. Everything else -- anything carrying a space, a
45
+ quote, a letter outside the word list, or a comment opener -- is
46
+ rejected, because it would reach the query as raw SQL.
47
+
48
+ Raises:
49
+ ValueError: if `comp` is not a usable comparison operator.
50
+ """
51
+ if not isinstance(comp, str):
52
+ raise ValueError(
53
+ f"comparator must be a string, got {type(comp).__name__}: {comp!r}")
54
+ normalized = ' '.join(comp.lower().split())
55
+ if normalized in _WORD_COMPS:
56
+ return normalized
57
+ if (_SYMBOLIC_COMP_RE.match(normalized)
58
+ and not any(tok in normalized for tok in _COMMENT_TOKENS)):
59
+ return normalized
60
+ raise ValueError(
61
+ f"Invalid comparator: {comp!r}. Use one of "
62
+ f"{', '.join(sorted(_WORD_COMPS))}, or a PostgreSQL operator such as "
63
+ f"'=', '!=', '<', '>=', '@>', '@@'.")
64
+
65
+
15
66
  class Expr:
16
67
  """A raw SQL expression for use with :meth:`Field.set`.
17
68
 
@@ -271,8 +322,18 @@ class Field():
271
322
  The constraint value. Pass ``None`` to *remove* the constraint.
272
323
  Pass a ``(comparator, value)`` tuple to use an operator other than
273
324
  ``=``. Supported comparators: ``=``, ``!=``, ``<``, ``<=``,
274
- ``>``, ``>=``, ``like``, ``ilike``, ``in``, ``is``, ``is not``,
275
- and any other PostgreSQL operator accepted in a WHERE clause.
325
+ ``>``, ``>=``, ``like``, ``not like``, ``ilike``, ``not ilike``,
326
+ ``similar to``, ``not similar to``, ``in``, ``not in``, ``is``,
327
+ ``is not``, ``is distinct from``, ``is not distinct from``, and
328
+ any operator spelled with PostgreSQL's operator characters --
329
+ including those added by extensions, such as ``@@``, ``@>``,
330
+ ``&&`` or pgvector's ``<->``.
331
+
332
+ The comparator is interpolated into the WHERE clause rather than
333
+ bound as a parameter, so anything else raises ``ValueError``.
334
+ Never build one by concatenating untrusted input; for a
335
+ condition that genuinely needs arbitrary SQL, pass an
336
+ :class:`Expr` as the *value*.
276
337
 
277
338
  Pass a sibling ``Field`` of the **same** relation instance, or an
278
339
  :class:`Expr` for arbitrary SQL expressions, to compare columns of
@@ -322,6 +383,11 @@ class Field():
322
383
  comp, value = value
323
384
  if value is None:
324
385
  raise ValueError("Can't have a None value with a comparator!")
386
+ if comp is not None:
387
+ # Validated here rather than further down: the Expr and Field
388
+ # branches below return early, and their comparator reaches the
389
+ # WHERE clause with no bound parameter at all.
390
+ comp = check_comparator(comp)
325
391
  # Expr (raw SQL expression)
326
392
  if isinstance(value, Expr):
327
393
  self.__is_set = True
@@ -345,7 +411,6 @@ class Field():
345
411
  comp = '='
346
412
  if isinstance(value, (list, set)):
347
413
  value = tuple(value)
348
- comp = comp.lower()
349
414
  if value is NULL and comp not in {'is', 'is not'}:
350
415
  raise ValueError("comp should be 'is' or 'is not' with NULL value!")
351
416
  self.__is_set = True
@@ -4,6 +4,19 @@ import unittest
4
4
  from typing import List
5
5
  from half_orm.relation import Relation
6
6
 
7
+ def _walk_attributes(obj, path: str):
8
+ """Follow `path` as a dotted attribute path on `obj`.
9
+
10
+ This was `eval(f"relation().{alias}")`, which let the string be any Python
11
+ expression while every caller only ever named an attribute -- including
12
+ the test files half-orm-dev generates, which reach the same object through
13
+ `rel.__dict__[alias]`.
14
+ """
15
+ for name in path.split('.'):
16
+ obj = getattr(obj, name)
17
+ return obj
18
+
19
+
7
20
  class HoTestCase(unittest.TestCase):
8
21
  def hotAssertIsPkey(self, relation: Relation, field_names: List[str]):
9
22
  "it shoud be the primary key"
@@ -30,7 +43,11 @@ class HoTestCase(unittest.TestCase):
30
43
  raise self.fail(f"{relation.__class__.__name__}()._ho_fkeys['{fk_name}']() does not reference {f_relation.__name__}")
31
44
 
32
45
  def hotAssertAliasReferences(self, relation: Relation, alias: str, f_relation: Relation):
33
- referenced = eval(f"relation().{alias}")
46
+ """Check that `alias` on `relation` points at `f_relation`.
47
+
48
+ `alias` names an attribute, or a dotted path of them.
49
+ """
50
+ referenced = _walk_attributes(relation(), alias)
34
51
  if referenced()._qrn != f_relation._qrn:
35
52
  raise self.fail(f"{relation.__class__.__name__}.{alias}() does not reference {f_relation.__name__}")
36
53
 
@@ -21,6 +21,7 @@ Example:
21
21
 
22
22
  import importlib
23
23
  import os
24
+ import re
24
25
  import sys
25
26
  import threading
26
27
  import typing
@@ -43,6 +44,102 @@ CONF_DIR = os.path.abspath(environ.get('HALFORM_CONF_DIR', '/etc/half_orm'))
43
44
 
44
45
  register = register_class
45
46
 
47
+ # One segment of a qualified SQL name: a bare identifier, or a double-quoted
48
+ # one (embedded quotes doubled, as PostgreSQL spells them).
49
+ # [^\W\d] is a word character that is not a digit -- a letter or underscore,
50
+ # Unicode-aware, which is what PostgreSQL accepts to start an identifier.
51
+ _NAME_SEGMENT = r'(?:"(?:[^"]|"")+"|[^\W\d][\w$]*)'
52
+ _QUALIFIED_NAME_RE = re.compile(rf'^{_NAME_SEGMENT}(?:\.{_NAME_SEGMENT})*$')
53
+ _NAME_SEGMENT_RE = re.compile(_NAME_SEGMENT)
54
+
55
+
56
+ def _split_qualified_name(name):
57
+ """Split ``schema.relation`` into its parts, or return None.
58
+
59
+ Understands the quoted form, so ``"a""b"."t"`` yields ``['a"b', 't']``.
60
+ Returns None for anything that is not a sequence of quoted or bare
61
+ segments, leaving the caller to fall back on a plain split.
62
+ """
63
+ parts = []
64
+ pos = 0
65
+ while True:
66
+ match = _NAME_SEGMENT_RE.match(name, pos)
67
+ if match is None:
68
+ return None
69
+ segment = match.group(0)
70
+ parts.append(segment[1:-1].replace('""', '"')
71
+ if segment.startswith('"') else segment)
72
+ pos = match.end()
73
+ if pos == len(name):
74
+ return parts
75
+ if name[pos] != '.':
76
+ return None
77
+ pos += 1
78
+
79
+
80
+ def _check_config_file_name(name):
81
+ """Check `name` designates a file *inside* ``CONF_DIR`` and return it.
82
+
83
+ ``Model``'s argument is a connection file *name*, not a path, but it went
84
+ to ``os.path.join(CONF_DIR, name)``, which confines nothing: ``..``
85
+ climbs out, and an absolute path discards ``CONF_DIR`` entirely. That
86
+ matters wherever the name is derived from a request -- a multi-tenant
87
+ application picking a connection per tenant -- since it decides which
88
+ database, and as which role, the process connects.
89
+
90
+ Raises:
91
+ ValueError: if `name` is empty or looks like a path.
92
+ """
93
+ if not isinstance(name, str) or not name:
94
+ raise ValueError(
95
+ f"config_file must be a non-empty string, got {name!r}")
96
+ if name in ('.', '..') or os.sep in name or (os.altsep and os.altsep in name):
97
+ raise ValueError(
98
+ f"config_file must be a file name inside {CONF_DIR}, not a path: {name!r}")
99
+ return name
100
+
101
+
102
+ def _check_qualified_name(name, what):
103
+ """Check `name` is a (possibly schema-qualified) SQL name and return it.
104
+
105
+ Function and procedure names are interpolated into the statement -- only
106
+ their *arguments* are bound -- so a name is the one part of these calls an
107
+ application must not build from untrusted input without checking. A name
108
+ matching this grammar carries no space, parenthesis, semicolon or stray
109
+ quote, and so cannot close the call and start something else.
110
+
111
+ The name is returned unchanged rather than re-quoted, so that PostgreSQL
112
+ keeps folding bare identifiers to lower case exactly as before.
113
+
114
+ Raises:
115
+ ValueError: if `name` is not a usable SQL name.
116
+ """
117
+ if not isinstance(name, str):
118
+ raise ValueError(f"{what} must be a string, got {type(name).__name__}: {name!r}")
119
+ if not _QUALIFIED_NAME_RE.match(name):
120
+ raise ValueError(
121
+ f"Invalid {what}: {name!r}. Expected a SQL name such as 'my_function' "
122
+ f"or 'my_schema.my_function'.")
123
+ return name
124
+
125
+
126
+ def _check_named_params(kwargs, what):
127
+ """Check that `kwargs` keys are usable as PostgreSQL named arguments.
128
+
129
+ They are rendered as ``key => %s``, and ``f(**{'a => 1) --': v})`` is
130
+ legal Python, so the keys need the same treatment as the callable's name.
131
+
132
+ Raises:
133
+ ValueError: if a key is not a plain identifier.
134
+ """
135
+ for key in kwargs:
136
+ if not key.isidentifier():
137
+ raise ValueError(
138
+ f"Invalid parameter name for {what}: {key!r}. Named parameters "
139
+ f"must be plain identifiers.")
140
+ return kwargs
141
+
142
+
46
143
  class Model:
47
144
  """Connection to a PostgreSQL database.
48
145
 
@@ -84,14 +181,14 @@ class Model:
84
181
  Raises:
85
182
  MissingConfigFile: If the **config_file** is not found in *HALFORM_CONF_DIR*.
86
183
  MalformedConfigFile: if the *name* is missing in the **config_file**.
184
+ ValueError: if **config_file** is not a plain file name.
87
185
  RuntimeError: If the reconnection is attempted on another database.
88
186
  """
89
- self.__config_file = config_file
187
+ config_file = _check_config_file_name(config_file)
90
188
  config = ConfigParser()
91
- self.__config_file_path = os.path.join(CONF_DIR, self.__config_file)
92
- file_ = self.__config_file_path
93
- self.__config_file_found = bool(config.read([file_]))
94
- if self.__config_file_found:
189
+ file_ = os.path.join(CONF_DIR, config_file)
190
+ found = bool(config.read([file_]))
191
+ if found:
95
192
  try:
96
193
  database = config['database']
97
194
  except KeyError as exc:
@@ -100,26 +197,38 @@ class Model:
100
197
  dbname = database['name']
101
198
  except KeyError as exc:
102
199
  raise model_errors.MalformedConfigFile(file_, 'Missing mandatory parameter', 'name') from exc
103
-
104
- if self._dbinfo and dbname != self.__dbname:
105
- raise RuntimeError(
106
- f"Can't reconnect to another database: {dbname} != {self.__dbname}")
107
- self._dbinfo['dbname'] = dbname
108
-
109
200
  else:
110
201
  dbname = config_file
111
- self._dbinfo['dbname'] = dbname
112
202
  # WARNING: use peer authentication only in development environment
113
203
  database = {'user': None, 'password': None, 'host': None, 'port': None, 'devel': True}
114
204
 
115
- self._dbinfo['user'] = database.get('user')
116
- self._dbinfo['password'] = database.get('password')
117
- self._dbinfo['host'] = database.get('host')
118
- self._dbinfo['port'] = database.get('port')
119
- self._dbinfo['connect_timeout'] = database.get('timeout', 3)
120
- self._production_mode = database.get('production', False)
121
- if self._production_mode == 'False': # production = False
122
- self._production_mode = False
205
+ # Checked for both branches. It used to guard only the one above, so
206
+ # reconnecting through a *missing* file skipped it entirely: the Model
207
+ # silently retargeted itself at another database and, with no file to
208
+ # read credentials from, dropped user/password/host to fall back on
209
+ # peer authentication.
210
+ if self._dbinfo and dbname != self.__dbname:
211
+ raise RuntimeError(
212
+ f"Can't reconnect to another database: {dbname} != {self.__dbname}")
213
+
214
+ # Nothing above this line touched self, so a rejected reconnect leaves
215
+ # the Model on its current configuration rather than half-way to one
216
+ # it refused.
217
+ production = database.get('production', False)
218
+ if production == 'False': # production = False
219
+ production = False
220
+ self._production_mode = production
221
+ self.__config_file = config_file
222
+ self.__config_file_path = file_
223
+ self.__config_file_found = found
224
+ self._dbinfo.update({
225
+ 'dbname': dbname,
226
+ 'user': database.get('user'),
227
+ 'password': database.get('password'),
228
+ 'host': database.get('host'),
229
+ 'port': database.get('port'),
230
+ 'connect_timeout': database.get('timeout', 3),
231
+ })
123
232
 
124
233
  def __connect(self, config_file: str=None, reload: bool=False):
125
234
  """Setup a new connection to the database.
@@ -132,10 +241,13 @@ class Model:
132
241
  reload (bool): If set to True, reloads the metadata from the database. Usefull if
133
242
  the model has changed.
134
243
  """
135
- self.disconnect()
136
-
244
+ # Read the new configuration before dropping the working connection:
245
+ # __load_config refuses a file naming another database, and the Model
246
+ # should come out of a refused reconnect exactly as it went in rather
247
+ # than disconnected and unusable.
137
248
  if config_file:
138
249
  self.__load_config(config_file)
250
+ self.disconnect()
139
251
  try:
140
252
  conn = psycopg.connect(**self._dbinfo, row_factory=dict_row, autocommit=True)
141
253
  except psycopg.OperationalError as exc:
@@ -190,10 +302,18 @@ class Model:
190
302
  ```
191
303
 
192
304
  """
193
- try:
194
- schema, table = relation_name.replace('"', '').rsplit('.', 1)
195
- except ValueError as err:
196
- raise model_errors.MissingSchemaInName(relation_name) from err
305
+ # Not `relation_name.replace('"', '')`: deleting the quotes made
306
+ # `"a""b"."t"` and `ab.t` the same name, so a relation whose name
307
+ # holds one could not be reached -- or reached a different relation
308
+ # that happened to spell the same without it.
309
+ parts = _split_qualified_name(relation_name)
310
+ if parts is None:
311
+ # A name outside the quoted grammar (a dash, say) still splits on
312
+ # its last dot, as it always did.
313
+ parts = relation_name.rsplit('.', 1)
314
+ if len(parts) < 2:
315
+ raise model_errors.MissingSchemaInName(relation_name)
316
+ schema, table = '.'.join(parts[:-1]), parts[-1]
197
317
  return factory({'fqrn': (self.__dbname, schema, table), 'model': self.__deja_vu[self.__dbname], 'fields_aliases':fields_aliases})
198
318
 
199
319
 
@@ -422,9 +542,14 @@ class Model:
422
542
 
423
543
  Raises:
424
544
  RuntimeError: if both ``*args`` and ``**kwargs`` are provided.
545
+ ValueError: if the name or a named parameter is not a SQL
546
+ identifier. The name is interpolated into the statement,
547
+ unlike the arguments, which are bound.
425
548
  """
426
549
  if bool(args) and bool(kwargs):
427
550
  raise RuntimeError("You can't mix args and kwargs with the execute_function method!")
551
+ fct_name = _check_qualified_name(fct_name, 'function name')
552
+ _check_named_params(kwargs, 'execute_function')
428
553
  cursor = self._connection.cursor(row_factory=dict_row)
429
554
  if kwargs:
430
555
  params = ', '.join([f'{key} => %s' for key in kwargs])
@@ -449,9 +574,14 @@ class Model:
449
574
 
450
575
  Raises:
451
576
  RuntimeError: if both ``*args`` and ``**kwargs`` are provided.
577
+ ValueError: if the name or a named parameter is not a SQL
578
+ identifier. The name is interpolated into the statement,
579
+ unlike the arguments, which are bound.
452
580
  """
453
581
  if bool(args) and bool(kwargs):
454
582
  raise RuntimeError("You can't mix args and kwargs with the call_procedure method!")
583
+ proc_name = _check_qualified_name(proc_name, 'procedure name')
584
+ _check_named_params(kwargs, 'call_procedure')
455
585
  if kwargs:
456
586
  params = ', '.join([f'{key} => %s' for key in kwargs])
457
587
  values = tuple(kwargs.values())
@@ -479,9 +609,14 @@ class Model:
479
609
 
480
610
  Raises:
481
611
  RuntimeError: if both ``*args`` and ``**kwargs`` are provided.
612
+ ValueError: if the name or a named parameter is not a SQL
613
+ identifier. The name is interpolated into the statement,
614
+ unlike the arguments, which are bound.
482
615
  """
483
616
  if bool(args) and bool(kwargs):
484
617
  raise RuntimeError("You can't mix args and kwargs with the aexecute_function method!")
618
+ fct_name = _check_qualified_name(fct_name, 'function name')
619
+ _check_named_params(kwargs, 'aexecute_function')
485
620
  if kwargs:
486
621
  params = ', '.join([f'{key} => %s' for key in kwargs])
487
622
  values = tuple(kwargs.values())
@@ -505,9 +640,14 @@ class Model:
505
640
 
506
641
  Raises:
507
642
  RuntimeError: if both ``*args`` and ``**kwargs`` are provided.
643
+ ValueError: if the name or a named parameter is not a SQL
644
+ identifier. The name is interpolated into the statement,
645
+ unlike the arguments, which are bound.
508
646
  """
509
647
  if bool(args) and bool(kwargs):
510
648
  raise RuntimeError("You can't mix args and kwargs with the acall_procedure method!")
649
+ proc_name = _check_qualified_name(proc_name, 'procedure name')
650
+ _check_named_params(kwargs, 'acall_procedure')
511
651
  if kwargs:
512
652
  params = ', '.join([f'{key} => %s' for key in kwargs])
513
653
  values = tuple(kwargs.values())
@@ -49,6 +49,15 @@ def __get_qrn(fqrn: tuple) -> tuple:
49
49
  "Returns the qualified relation name <schema>.<relation> from the fully qualified relation name"
50
50
  return fqrn[1:]
51
51
 
52
+ def quote_ident(name) -> str:
53
+ """Quote one SQL identifier, escaping the quotes it contains.
54
+
55
+ PostgreSQL allows a quote in a relation name -- `create table "t""bl"` --
56
+ and these names end up interpolated into statements, where an unescaped
57
+ one closes the identifier early.
58
+ """
59
+ return '"' + str(name).replace('"', '""') + '"'
60
+
52
61
  def normalize_fqrn(t_fqrn: tuple) -> str:
53
62
  """
54
63
  Transform the tuple (<db name>, <schema name>, <table name>) in
@@ -56,16 +65,16 @@ def normalize_fqrn(t_fqrn: tuple) -> str:
56
65
  Dots are allowed only in the schema name.
57
66
  """
58
67
  dbname, schemaname, tablename = t_fqrn
59
- return f'"{dbname}":"{schemaname}"."{tablename}"'
68
+ return f'{quote_ident(dbname)}:{quote_ident(schemaname)}.{quote_ident(tablename)}'
60
69
 
61
- def normalize_qrn(t_qrn):
62
- """
63
- qrn is a tuple for the qualified relation name (<schema name>, <talbe name>)
64
- A schema name can have any number of dots in it.
65
- A table name can't have a dot in it.
66
- returns "<schema name>"."<relation name>"
70
+ def normalize_qrn(t_fqrn):
71
+ """Return "<schema name>"."<relation name>" for a *fully* qualified name.
72
+
73
+ Takes the (<db name>, <schema name>, <relation name>) tuple and drops the
74
+ database part -- despite the name, which every caller reads past. A schema
75
+ name may hold any number of dots; a relation name may not.
67
76
  """
68
- return '.'.join([f'"{elt}"' for elt in __get_qrn(t_qrn)])
77
+ return '.'.join([quote_ident(elt) for elt in __get_qrn(t_fqrn)])
69
78
 
70
79
  def camel_case(string):
71
80
  "Retruns the string transformed to camel case"
@@ -20,6 +20,7 @@ Example:
20
20
  3
21
21
  """
22
22
 
23
+ import csv
23
24
  import inspect
24
25
  from dataclasses import dataclass
25
26
  from functools import wraps
@@ -40,6 +41,47 @@ from half_orm.sql_ast import (
40
41
  And as ASTAnd, Not as ASTNot, Group as ASTGroup, SetOp as ASTSetOp,
41
42
  )
42
43
 
44
+ def _ho_quote_ident(name):
45
+ """Quote one SQL identifier, escaping the quotes it contains."""
46
+ return '"' + str(name).replace('"', '""') + '"'
47
+
48
+
49
+ def _ho_copy_columns(obj, data, columns, method):
50
+ """Resolve and check the column list a COPY statement will name.
51
+
52
+ No parameter can stand for an identifier, so these names are interpolated
53
+ into the statement -- and they arrive from a CSV header or from the keys of
54
+ the caller's dicts, both of which are routinely deserialised from outside
55
+ the program. Only a name the relation actually has may pass.
56
+
57
+ Consumes the header line when *data* is a file-like object and *columns*
58
+ is None, leaving the stream positioned on the first row.
59
+ """
60
+ if columns is None:
61
+ if hasattr(data, 'read'):
62
+ header = data.readline()
63
+ if not header:
64
+ raise ValueError(f'{method}: empty input, no header row')
65
+ # PostgreSQL parses the body as CSV, so the header is read the
66
+ # same way: splitting on ',' mangles any quoted name holding one.
67
+ columns = next(csv.reader([header]), [])
68
+ else:
69
+ if not data:
70
+ raise ValueError(f'{method}: data must not be empty')
71
+ columns = list(data[0].keys())
72
+
73
+ columns = [column.strip() if isinstance(column, str) else column
74
+ for column in columns]
75
+ if not columns:
76
+ raise ValueError(f'{method}: no columns to copy')
77
+
78
+ unknown = [column for column in columns if column not in obj._ho_fields]
79
+ if unknown:
80
+ raise relation_errors.UnknownAttributeError(
81
+ ', '.join(str(column) for column in unknown))
82
+ return columns
83
+
84
+
43
85
  class _SetOperators:
44
86
  """_SetOperators class stores the set operations made on the Relation class objects
45
87
 
@@ -353,15 +395,15 @@ class Relation:
353
395
  )
354
396
  ```
355
397
  """
356
- cls()._ho_check_writable()
398
+ obj = cls()
399
+ obj._ho_check_writable()
400
+ is_file = hasattr(data, 'read')
401
+ columns = _ho_copy_columns(obj, data, columns, 'ho_copy')
402
+ cols = ', '.join(_ho_quote_ident(c) for c in columns)
357
403
  conn = cls._ho_model._connection
358
- if hasattr(data, 'read'):
404
+
405
+ if is_file:
359
406
  # File-like object — stream CSV directly into COPY
360
- if columns is None:
361
- # First line is the header
362
- header = data.readline().rstrip('\n')
363
- columns = [c.strip() for c in header.split(',')]
364
- cols = ', '.join(f'"{c}"' for c in columns)
365
407
  stmt = f'copy {cls._qrn} ({cols}) from stdin (format csv)'
366
408
  count = 0
367
409
  with conn.cursor().copy(stmt) as copy:
@@ -369,17 +411,13 @@ class Relation:
369
411
  copy.write(line)
370
412
  count += 1
371
413
  return count
372
- else:
373
- # list[dict]
374
- if not data:
375
- raise ValueError('ho_copy: data must not be empty')
376
- columns = list(data[0].keys())
377
- cols = ', '.join(f'"{c}"' for c in columns)
378
- stmt = f'copy {cls._qrn} ({cols}) from stdin'
379
- with conn.cursor().copy(stmt) as copy:
380
- for row in data:
381
- copy.write_row([row[c] for c in columns])
382
- return len(data)
414
+
415
+ # list[dict]
416
+ stmt = f'copy {cls._qrn} ({cols}) from stdin'
417
+ with conn.cursor().copy(stmt) as copy:
418
+ for row in data:
419
+ copy.write_row([row[c] for c in columns])
420
+ return len(data)
383
421
 
384
422
  def _ho_result_is_relation(self, *args) -> bool:
385
423
  """Returns True if ho_select(*args) produces a proper relation in the
@@ -1629,13 +1667,14 @@ Fkeys = {"""
1629
1667
 
1630
1668
  *New in version 0.18.12.*
1631
1669
  """
1632
- cls()._ho_check_writable()
1670
+ obj = cls()
1671
+ obj._ho_check_writable()
1672
+ is_file = hasattr(data, 'read')
1673
+ columns = _ho_copy_columns(obj, data, columns, 'ho_acopy')
1674
+ cols = ', '.join(_ho_quote_ident(c) for c in columns)
1633
1675
  aconn = cls._ho_model._aconnection
1634
- if hasattr(data, 'read'):
1635
- if columns is None:
1636
- header = data.readline().rstrip('\n')
1637
- columns = [c.strip() for c in header.split(',')]
1638
- cols = ', '.join(f'"{c}"' for c in columns)
1676
+
1677
+ if is_file:
1639
1678
  stmt = f'copy {cls._qrn} ({cols}) from stdin (format csv)'
1640
1679
  count = 0
1641
1680
  async with aconn.cursor().copy(stmt) as copy:
@@ -1643,16 +1682,12 @@ Fkeys = {"""
1643
1682
  await copy.write(line)
1644
1683
  count += 1
1645
1684
  return count
1646
- else:
1647
- if not data:
1648
- raise ValueError('ho_acopy: data must not be empty')
1649
- columns = list(data[0].keys())
1650
- cols = ', '.join(f'"{c}"' for c in columns)
1651
- stmt = f'copy {cls._qrn} ({cols}) from stdin'
1652
- async with aconn.cursor().copy(stmt) as copy:
1653
- for row in data:
1654
- await copy.write_row([row[c] for c in columns])
1655
- return len(data)
1685
+
1686
+ stmt = f'copy {cls._qrn} ({cols}) from stdin'
1687
+ async with aconn.cursor().copy(stmt) as copy:
1688
+ for row in data:
1689
+ await copy.write_row([row[c] for c in columns])
1690
+ return len(data)
1656
1691
 
1657
1692
  #@utils.trace
1658
1693
  def __what(self):
@@ -92,10 +92,19 @@ class Transaction:
92
92
  cur.execute(f'RELEASE SAVEPOINT {sp_name}')
93
93
  else:
94
94
  try:
95
- conn.commit()
96
- conn.autocommit = True
95
+ if exc_type is not None:
96
+ conn.rollback()
97
+ else:
98
+ conn.commit()
97
99
  except psycopg.Error:
98
- conn.rollback()
100
+ # A failed COMMIT must reach the caller: silently rolling back
101
+ # would leave the application believing its work was persisted.
102
+ # A failed ROLLBACK, on the other hand, must not displace the
103
+ # exception the caller is already handling.
104
+ if exc_type is None:
105
+ raise
106
+ finally:
107
+ _restore_autocommit(conn)
99
108
  return False
100
109
 
101
110
  @property
@@ -104,3 +113,16 @@ class Transaction:
104
113
 
105
114
  def is_set(self):
106
115
  return self.__transaction.get('level', 0) > 0
116
+
117
+
118
+ def _restore_autocommit(conn):
119
+ """Put `conn` back in autocommit mode after the outermost block.
120
+
121
+ Best-effort: if the connection is broken, the failure is irrelevant here --
122
+ ``Model._connection`` reconnects on the next use -- and must not mask the
123
+ exception on its way out of ``__exit__``.
124
+ """
125
+ try:
126
+ conn.autocommit = True
127
+ except psycopg.Error:
128
+ pass
@@ -0,0 +1 @@
1
+ 0.18.14
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: half_orm
3
- Version: 0.18.13
3
+ Version: 0.18.14
4
4
  Summary: A simple PostgreSQL to Python mapper.
5
5
  Home-page: https://github.com/half-orm/half-orm
6
6
  Author: Joël Maïzi
@@ -29,6 +29,17 @@ License-File: AUTHORS
29
29
  Requires-Dist: psycopg[binary]
30
30
  Requires-Dist: click
31
31
  Requires-Dist: importlib-metadata; python_version < "3.8"
32
+ Dynamic: author
33
+ Dynamic: author-email
34
+ Dynamic: classifier
35
+ Dynamic: description
36
+ Dynamic: description-content-type
37
+ Dynamic: home-page
38
+ Dynamic: license
39
+ Dynamic: license-file
40
+ Dynamic: project-url
41
+ Dynamic: requires-dist
42
+ Dynamic: summary
32
43
 
33
44
  # halfORM
34
45
 
@@ -11,6 +11,8 @@ Tests cover:
11
11
 
12
12
  import pytest
13
13
  import json
14
+ import os
15
+ import stat
14
16
  import tempfile
15
17
  import shutil
16
18
  from pathlib import Path
@@ -20,13 +22,27 @@ from click.testing import CliRunner
20
22
  # Import the CLI module
21
23
  import sys
22
24
  sys.path.insert(0, '.')
25
+ from half_orm import cli as half_orm_cli
23
26
  from half_orm.cli import (
24
27
  main, discover_extensions, check_version_compatibility,
25
- is_trusted_extension, add_trusted_extension, remove_trusted_extension,
26
- load_cli_config, save_cli_config, get_config_file, OFFICIAL_EXTENSIONS
28
+ is_trusted_extension, is_official_extension, add_trusted_extension,
29
+ remove_trusted_extension,
30
+ load_cli_config, save_cli_config, get_config_file, get_project_key,
31
+ LEGACY_CONFIG_NAME, OFFICIAL_EXTENSIONS
27
32
  )
28
33
 
29
34
 
35
+ @pytest.fixture(autouse=True)
36
+ def isolated_cli_config(tmp_path, monkeypatch):
37
+ """Keep every test out of the developer's own trust store.
38
+
39
+ get_config_file() deliberately resolves under the user's home, so without
40
+ this the suite would read -- and write -- the real configuration.
41
+ """
42
+ monkeypatch.setenv('HALF_ORM_CLI_CONFIG', str(tmp_path / 'cli.json'))
43
+ monkeypatch.setattr(half_orm_cli, '_legacy_config_warned', False)
44
+
45
+
30
46
  class TestVersionCompatibility:
31
47
  """Test version compatibility checking."""
32
48
 
@@ -67,10 +83,9 @@ class TestConfigManagement:
67
83
  with patch('half_orm.cli.Path.cwd', return_value=Path(self.temp_dir)):
68
84
  config = {'test': 'value'}
69
85
  assert save_cli_config(config) == True
70
-
71
- config_file = Path(self.temp_dir) / '.half_orm_cli'
72
- assert config_file.exists()
73
-
86
+
87
+ assert get_config_file().exists()
88
+
74
89
  loaded = load_cli_config()
75
90
  assert loaded['test'] == 'value'
76
91
  assert 'last_updated' in loaded
@@ -341,6 +356,170 @@ class TestSecurityWarnings:
341
356
  mock_trust.assert_called_once_with('half-orm-test', '0.16.0')
342
357
 
343
358
 
359
+ class TestTrustStoreLocation:
360
+ """The trust store must not be writable by the code it is protecting.
361
+
362
+ It suppresses the consent prompt for unofficial extensions, so a checkout
363
+ able to write its own entries would grant itself silent consent -- and a
364
+ `git clone` produces files owned by the victim, which is why no permission
365
+ check can substitute for keeping the store out of the project.
366
+ """
367
+
368
+ def test_store_lives_outside_any_project(self, monkeypatch, tmp_path):
369
+ """The default location is under the user's profile, not the CWD."""
370
+ monkeypatch.delenv('HALF_ORM_CLI_CONFIG', raising=False)
371
+ monkeypatch.delenv('XDG_CONFIG_HOME', raising=False)
372
+ monkeypatch.chdir(tmp_path)
373
+
374
+ config_file = get_config_file()
375
+ assert config_file.name == 'cli.json'
376
+ assert config_file.parent.name == 'half_orm'
377
+ assert Path.home() in config_file.parents
378
+ assert tmp_path not in config_file.parents
379
+
380
+ @pytest.mark.skipif(sys.platform == 'win32',
381
+ reason='Windows uses %APPDATA%, not XDG')
382
+ def test_xdg_config_home_is_honoured(self, monkeypatch, tmp_path):
383
+ monkeypatch.delenv('HALF_ORM_CLI_CONFIG', raising=False)
384
+ monkeypatch.setenv('XDG_CONFIG_HOME', str(tmp_path))
385
+ assert get_config_file() == tmp_path / 'half_orm' / 'cli.json'
386
+
387
+ @pytest.mark.skipif(sys.platform != 'win32', reason='Windows-only layout')
388
+ def test_appdata_is_honoured(self, monkeypatch, tmp_path):
389
+ monkeypatch.delenv('HALF_ORM_CLI_CONFIG', raising=False)
390
+ monkeypatch.setenv('APPDATA', str(tmp_path))
391
+ assert get_config_file() == tmp_path / 'half_orm' / 'cli.json'
392
+
393
+ def test_explicit_override_wins(self, monkeypatch, tmp_path):
394
+ monkeypatch.setenv('XDG_CONFIG_HOME', str(tmp_path / 'xdg'))
395
+ monkeypatch.setenv('HALF_ORM_CLI_CONFIG', str(tmp_path / 'chosen.json'))
396
+ assert get_config_file() == tmp_path / 'chosen.json'
397
+
398
+ def test_project_local_store_grants_nothing(self, monkeypatch, tmp_path):
399
+ """A .half_orm_cli shipped in a repository must not be honoured."""
400
+ project = tmp_path / 'hostile'
401
+ project.mkdir()
402
+ (project / LEGACY_CONFIG_NAME).write_text(json.dumps({
403
+ 'trusted_extensions': {
404
+ 'half-orm-evil': {'version': '1.0.0'}
405
+ }
406
+ }))
407
+ monkeypatch.chdir(project)
408
+
409
+ assert is_trusted_extension('half-orm-evil', '1.0.0') is False
410
+
411
+ def test_project_local_store_is_reported_not_ignored_silently(
412
+ self, monkeypatch, tmp_path, capsys):
413
+ """Silently dropping the old file would look like a bug to its owner."""
414
+ project = tmp_path / 'legacy'
415
+ project.mkdir()
416
+ (project / LEGACY_CONFIG_NAME).write_text('{}')
417
+ monkeypatch.chdir(project)
418
+
419
+ load_cli_config()
420
+ first = capsys.readouterr().err
421
+ assert LEGACY_CONFIG_NAME in first
422
+
423
+ # ...but only once, so it cannot drown the security prompt itself.
424
+ load_cli_config()
425
+ assert capsys.readouterr().err == ''
426
+
427
+ def test_trust_does_not_leak_to_another_project(self, monkeypatch, tmp_path):
428
+ one = tmp_path / 'one'
429
+ two = tmp_path / 'two'
430
+ for path in (one, two):
431
+ (path / '.git').mkdir(parents=True)
432
+
433
+ monkeypatch.chdir(one)
434
+ add_trusted_extension('half-orm-ext', '1.0.0')
435
+ assert is_trusted_extension('half-orm-ext', '1.0.0') is True
436
+
437
+ monkeypatch.chdir(two)
438
+ assert is_trusted_extension('half-orm-ext', '1.0.0') is False
439
+
440
+ def test_trust_is_reused_from_a_subdirectory(self, monkeypatch, tmp_path):
441
+ """Re-prompting inside the same project trains users to answer blind."""
442
+ project = tmp_path / 'proj'
443
+ (project / '.git').mkdir(parents=True)
444
+ deep = project / 'a' / 'b'
445
+ deep.mkdir(parents=True)
446
+
447
+ monkeypatch.chdir(project)
448
+ add_trusted_extension('half-orm-ext', '1.0.0')
449
+
450
+ monkeypatch.chdir(deep)
451
+ assert get_project_key() == str(project.resolve())
452
+ assert is_trusted_extension('half-orm-ext', '1.0.0') is True
453
+
454
+ def test_untrust_only_clears_the_current_project(self, monkeypatch, tmp_path):
455
+ one = tmp_path / 'one'
456
+ two = tmp_path / 'two'
457
+ for path in (one, two):
458
+ (path / '.git').mkdir(parents=True)
459
+
460
+ for path in (one, two):
461
+ monkeypatch.chdir(path)
462
+ add_trusted_extension('half-orm-ext', '1.0.0')
463
+
464
+ monkeypatch.chdir(one)
465
+ assert remove_trusted_extension('half-orm-ext') is True
466
+ assert is_trusted_extension('half-orm-ext', '1.0.0') is False
467
+
468
+ monkeypatch.chdir(two)
469
+ assert is_trusted_extension('half-orm-ext', '1.0.0') is True
470
+
471
+ @pytest.mark.skipif(sys.platform == 'win32',
472
+ reason='POSIX permission bits; Windows relies on the '
473
+ 'profile ACL of %APPDATA%')
474
+ def test_store_is_written_owner_only(self, monkeypatch, tmp_path):
475
+ monkeypatch.chdir(tmp_path)
476
+ add_trusted_extension('half-orm-ext', '1.0.0')
477
+
478
+ config_file = get_config_file()
479
+ assert stat.S_IMODE(config_file.stat().st_mode) == 0o600
480
+ assert stat.S_IMODE(config_file.parent.stat().st_mode) == 0o700
481
+
482
+ @pytest.mark.skipif(sys.platform == 'win32',
483
+ reason='os.chmod only toggles the read-only flag there')
484
+ def test_widened_permissions_are_narrowed_again(self, monkeypatch, tmp_path):
485
+ monkeypatch.chdir(tmp_path)
486
+ add_trusted_extension('half-orm-ext', '1.0.0')
487
+ config_file = get_config_file()
488
+
489
+ os.chmod(config_file, 0o644)
490
+ add_trusted_extension('half-orm-other', '1.0.0')
491
+ assert stat.S_IMODE(config_file.stat().st_mode) == 0o600
492
+
493
+ @pytest.mark.parametrize('content', [
494
+ 'not json at all {{{',
495
+ '[1, 2, 3]',
496
+ '{"trusted_extensions": "everything"}',
497
+ '{"trusted_extensions": {"__project__": "everything"}}',
498
+ '{"trusted_extensions": {"__project__": {"half-orm-ext": "yes"}}}',
499
+ ])
500
+ def test_malformed_store_grants_nothing(self, monkeypatch, tmp_path, content):
501
+ """Every shape a broken store can take must read as "not trusted"."""
502
+ monkeypatch.chdir(tmp_path)
503
+ content = content.replace('__project__', get_project_key())
504
+ get_config_file().parent.mkdir(parents=True, exist_ok=True)
505
+ get_config_file().write_text(content)
506
+
507
+ assert is_trusted_extension('half-orm-ext', '1.0.0') is False
508
+
509
+
510
+ class TestOfficialExtensionList:
511
+ """An unclaimed name on the allowlist is a free pass to whoever takes it."""
512
+
513
+ def test_test_extension_is_not_official(self):
514
+ """half-orm-test-extension is unregistered on PyPI, so squattable."""
515
+ assert is_official_extension('half-orm-test-extension') is False
516
+ assert is_official_extension('half_orm_test_extension') is False
517
+
518
+ def test_maintained_extensions_are_official(self):
519
+ for name in ('half-orm-inspect', 'half-orm-dev'):
520
+ assert is_official_extension(name) is True
521
+
522
+
344
523
  class TestIntegrationTests:
345
524
  """Integration tests for complete CLI workflows."""
346
525
 
@@ -1 +0,0 @@
1
- 0.18.13
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes