magehand 0.76.2__tar.gz → 0.76.3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: magehand
3
- Version: 0.76.2
3
+ Version: 0.76.3
4
4
  Summary: The developer CLI for building apps on davidlarrimore's homelab
5
5
  Project-URL: Source, https://github.com/davidlarrimore/magehand
6
6
  License-Expression: MIT
@@ -149,7 +149,7 @@ has it as `"next"`. Only commands whose output is the result itself (`guide`,
149
149
  | `magehand status [APP] [--watch]` | Where the app is: set up, registered, built, on dev and prod |
150
150
  | `magehand check [APP] [--ui]` | Check the app against the platform's rules, `build.yaml` exactly the template's included (before every push; `--ui` runs CI's browser check) |
151
151
  | `magehand verify` | Run the platform's static checks on what you'll push, as CI does |
152
- | `magehand api PATH [--prod]` | Call the app's API on dev or production, as you |
152
+ | `magehand api PATH [--prod]` | Call the app's API on dev or production, as you (a write carries the app's own `Origin`, as a browser's does) |
153
153
  | `magehand llm usage [--env dev\|prod] [--by kind\|model\|stage\|prompt] [--since D] [--until D]` | The app's LLM tokens and cost from the platform: each llm block, dev and prod apart, the budget and its reset date; a lab agent sees its own key's |
154
154
 
155
155
  **Ship an app**
@@ -134,7 +134,7 @@ has it as `"next"`. Only commands whose output is the result itself (`guide`,
134
134
  | `magehand status [APP] [--watch]` | Where the app is: set up, registered, built, on dev and prod |
135
135
  | `magehand check [APP] [--ui]` | Check the app against the platform's rules, `build.yaml` exactly the template's included (before every push; `--ui` runs CI's browser check) |
136
136
  | `magehand verify` | Run the platform's static checks on what you'll push, as CI does |
137
- | `magehand api PATH [--prod]` | Call the app's API on dev or production, as you |
137
+ | `magehand api PATH [--prod]` | Call the app's API on dev or production, as you (a write carries the app's own `Origin`, as a browser's does) |
138
138
  | `magehand llm usage [--env dev\|prod] [--by kind\|model\|stage\|prompt] [--since D] [--until D]` | The app's LLM tokens and cost from the platform: each llm block, dev and prod apart, the budget and its reset date; a lab agent sees its own key's |
139
139
 
140
140
  **Ship an app**
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "magehand"
7
- version = "0.76.2"
7
+ version = "0.76.3"
8
8
  description = "The developer CLI for building apps on davidlarrimore's homelab"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -1203,6 +1203,11 @@ def cmd_api(a):
1203
1203
  print(f'magehand: {method} to production: this changes {name}\'s real data, as you', file=sys.stderr)
1204
1204
  # Cloudflare (public production hosts) refuses Python's default User-Agent with a 403.
1205
1205
  headers = dict({'User-Agent': 'magehand'}, **extra)
1206
+ # Apps refuse a write whose Origin isn't their own host (the platform's CSRF rule), and a browser always sends
1207
+ # one: send the app's own, as a browser on its page would (magehand#158). A caller's -H Origin wins.
1208
+ if method not in ('GET', 'HEAD', 'OPTIONS', 'TRACE') and not any(k.lower() == 'origin' for k in extra):
1209
+ where = urllib.parse.urlsplit(url)
1210
+ headers['Origin'] = f'{where.scheme}://{where.netloc}'
1206
1211
  failed = []
1207
1212
  token = api_session_token(failed=failed)[0]
1208
1213
  started = time.monotonic()
@@ -10492,15 +10497,18 @@ def build_parser():
10492
10497
  'X-authentik-* headers. A sleeping dev app is woken first (its waiting page is never the answer).\n'
10493
10498
  'The body goes to stdout and a status line to stderr; exit 0 on 2xx/3xx, 1 otherwise (--json\n'
10494
10499
  'says why: signed_out, asleep, unreachable). Redirects are not followed. In an app\'s checkout it\n'
10495
- 'calls that app only. *.lab hosts need the home network.\n\n'
10500
+ 'calls that app only. *.lab hosts need the home network. A write (POST, PUT, PATCH, DELETE)\n'
10501
+ 'carries the app\'s own Origin, as a browser on its page would: apps refuse writes without it\n'
10502
+ '(-H "Origin: ..." replaces it).\n\n'
10496
10503
  'examples:\n magehand api /api/health\n magehand api -a career-scout /api/jobs --json\n'
10497
10504
  " magehand api /api/items -d '{\"name\": \"x\"}' POST, JSON\n"
10498
10505
  ' magehand api /api/items/7 -X DELETE\n'
10499
10506
  ' magehand api /api/import -d @items.json --prod production: real data\n\n'
10500
10507
  'Your own HTTP client (curl, requests, a test suite): `magehand api token` prints the access\n'
10501
10508
  'token (1h, renewed first so 10+ minutes are left; the same for every app). Send it as\n'
10502
- '`Authorization: Bearer TOKEN` to the app\'s URL (`magehand app` lists them); get a new one\n'
10503
- 'when it expires. Keep it out of files, logs and commits.\n'
10509
+ '`Authorization: Bearer TOKEN` to the app\'s URL (`magehand app` lists them), and on a write\n'
10510
+ '`Origin: https://APP-HOST` too; get a new token when it expires. Keep it out of files, logs\n'
10511
+ 'and commits.\n'
10504
10512
  ' curl -H "Authorization: Bearer $(magehand api token)" https://APP-dev.lab.davidlarrimore.com/api/x\n'
10505
10513
  ' magehand api token --json {token, expires_at, seconds_left}')
10506
10514
  p.add_argument('path', metavar='PATH', help="the path on the app, from / (with any ?query); or `token`")
@@ -7015,6 +7015,35 @@ class AppApi(unittest.TestCase):
7015
7015
  self.assertEqual((method, headers['Content-Type'], body), ('POST', 'application/json', b'{"name": "x"}'))
7016
7016
  self.assertEqual(json.loads(out)['status'], 201)
7017
7017
 
7018
+ def test_a_write_sends_the_apps_own_origin(self): # magehand#158: apps refuse a write with no Origin
7019
+ for argv, method, origin in (
7020
+ (('-d', '{}'), 'POST', 'https://demo-dev.lab.davidlarrimore.com'),
7021
+ (('-X', 'put', '-d', '{}'), 'PUT', 'https://demo-dev.lab.davidlarrimore.com'),
7022
+ (('-X', 'patch', '-d', '{}'), 'PATCH', 'https://demo-dev.lab.davidlarrimore.com'),
7023
+ (('-X', 'delete'), 'DELETE', 'https://demo-dev.lab.davidlarrimore.com'),
7024
+ (('-X', 'post', '-H', 'origin: https://example.com'), 'POST', None)):
7025
+ self.calls, self.replies = [], [(200, {}, b'')]
7026
+ code, _, _ = self.run_main('api', '-a', 'demo', '/api/items', *argv)
7027
+ _, sent, headers, _ = self.calls[0]
7028
+ self.assertEqual((code, sent), (0, method))
7029
+ if origin:
7030
+ self.assertEqual(headers['Origin'], origin)
7031
+ else: # the caller's own Origin wins, sent once
7032
+ self.assertEqual([(k, v) for k, v in headers.items() if k.lower() == 'origin'],
7033
+ [('origin', 'https://example.com')])
7034
+
7035
+ def test_a_write_to_production_sends_its_public_origin(self):
7036
+ self.replies = [(200, {}, b'')]
7037
+ with mock.patch.object(magehand, 'file_at', lambda repo, path, ref: ('demo:\n exposure: external\n', None)):
7038
+ code, _, _ = self.run_main('api', '-a', 'demo', '/x', '--prod', '-X', 'post')
7039
+ self.assertEqual((code, self.calls[0][2]['Origin']), (0, 'https://demo.davidlarrimore.com'))
7040
+
7041
+ def test_a_read_sends_no_origin(self):
7042
+ for method in ('get', 'head', 'options'):
7043
+ self.calls, self.replies = [], [(200, {}, b'')]
7044
+ self.run_main('api', '-a', 'demo', '/api/items', '-X', method)
7045
+ self.assertFalse(any(k.lower() == 'origin' for k in self.calls[0][2]), method)
7046
+
7018
7047
  def test_a_sleeping_dev_is_woken_not_returned(self):
7019
7048
  waiting = (200, {'Content-Type': 'text/html'}, b'<html><head><title>Sablier</title>')
7020
7049
  self.replies = [waiting, waiting, (200, {}, b'real')]
File without changes
File without changes
File without changes