|
| 1 | +"""Security regression tests for v0.1.1 hardening. |
| 2 | +
|
| 3 | +Covers: |
| 4 | + - store_property._eval_path AST allowlist (the eval -> AST-walker fix) |
| 5 | + - slack_webhook scheme + host validation |
| 6 | + - worker spawn_next blend-path leading-dash guard |
| 7 | +
|
| 8 | +These pin the security boundary so future refactors can't accidentally |
| 9 | +weaken it without a failing test. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from unittest import mock |
| 15 | + |
| 16 | +import bpy |
| 17 | + |
| 18 | +from stage.core.store_property import ( |
| 19 | + UnsafePathError, |
| 20 | + _eval_path, |
| 21 | + resolve_value, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +# --- AST-walker / _eval_path ----------------------------------------------- |
| 26 | + |
| 27 | + |
| 28 | +def test_eval_path_accepts_simple_attribute_chain(): |
| 29 | + # Real RNA path: should resolve without raising. Value identity isn't |
| 30 | + # important here — just that the walker permits the structure. |
| 31 | + val = _eval_path("bpy.context.scene") |
| 32 | + assert val is bpy.context.scene |
| 33 | + |
| 34 | + |
| 35 | +def test_eval_path_accepts_string_subscript(): |
| 36 | + # bpy.data.scenes['Scene'] — the most common stored-path shape after |
| 37 | + # the right-click hook normalises into bpy.context.scene anyway. |
| 38 | + if "Scene" not in bpy.data.scenes: |
| 39 | + bpy.data.scenes.new("Scene") |
| 40 | + val = _eval_path("bpy.data.scenes['Scene']") |
| 41 | + assert val is bpy.data.scenes["Scene"] |
| 42 | + |
| 43 | + |
| 44 | +def test_eval_path_rejects_function_call(): |
| 45 | + """A poisoned data_path with a Call node must be refused.""" |
| 46 | + raised = False |
| 47 | + try: |
| 48 | + _eval_path("bpy.ops.wm.quit_blender()") |
| 49 | + except UnsafePathError: |
| 50 | + raised = True |
| 51 | + assert raised, "Call nodes must raise UnsafePathError" |
| 52 | + |
| 53 | + |
| 54 | +def test_eval_path_rejects_lambda(): |
| 55 | + raised = False |
| 56 | + try: |
| 57 | + _eval_path("(lambda: bpy.context.scene)()") |
| 58 | + except UnsafePathError: |
| 59 | + raised = True |
| 60 | + assert raised |
| 61 | + |
| 62 | + |
| 63 | +def test_eval_path_rejects_arithmetic(): |
| 64 | + raised = False |
| 65 | + try: |
| 66 | + _eval_path("bpy.context.scene + bpy.context.scene") |
| 67 | + except UnsafePathError: |
| 68 | + raised = True |
| 69 | + assert raised |
| 70 | + |
| 71 | + |
| 72 | +def test_eval_path_rejects_dunder_access_via_non_bpy_root(): |
| 73 | + """A `__import__('os')...` attempt must fail because the root name |
| 74 | + isn't `bpy`.""" |
| 75 | + raised = False |
| 76 | + try: |
| 77 | + _eval_path("__import__('os').system('echo pwned')") |
| 78 | + except UnsafePathError: |
| 79 | + raised = True |
| 80 | + assert raised |
| 81 | + |
| 82 | + |
| 83 | +def test_eval_path_rejects_non_constant_subscript(): |
| 84 | + """Dynamic subscripts (`a[b]` where b is a Name) must be refused — |
| 85 | + the indexer must be a literal.""" |
| 86 | + raised = False |
| 87 | + try: |
| 88 | + _eval_path("bpy.data.scenes[bpy.context]") |
| 89 | + except UnsafePathError: |
| 90 | + raised = True |
| 91 | + assert raised |
| 92 | + |
| 93 | + |
| 94 | +def test_eval_path_rejects_syntax_error(): |
| 95 | + raised = False |
| 96 | + try: |
| 97 | + _eval_path("bpy.context.scene.<<") |
| 98 | + except UnsafePathError: |
| 99 | + raised = True |
| 100 | + assert raised |
| 101 | + |
| 102 | + |
| 103 | +def test_resolve_value_propagates_unsafe_path_error(): |
| 104 | + """Public entry point must surface the same protection.""" |
| 105 | + raised = False |
| 106 | + try: |
| 107 | + resolve_value("bpy.ops.wm.quit_blender()") |
| 108 | + except UnsafePathError: |
| 109 | + raised = True |
| 110 | + assert raised |
| 111 | + |
| 112 | + |
| 113 | +# --- slack_webhook scheme guard ------------------------------------------- |
| 114 | + |
| 115 | + |
| 116 | +def _stub_action(target: str, message: str = ""): |
| 117 | + """Minimal duck-typed stand-in for a PostRenderAction PropertyGroup.""" |
| 118 | + class _A: |
| 119 | + pass |
| 120 | + a = _A() |
| 121 | + a.target = target |
| 122 | + a.message = message |
| 123 | + return a |
| 124 | + |
| 125 | + |
| 126 | +def test_slack_webhook_refuses_file_scheme(): |
| 127 | + """file:// URLs would let a poisoned .blend exfiltrate local files |
| 128 | + via urllib's default opener. Must be refused outright — no network |
| 129 | + call attempted.""" |
| 130 | + from stage.core import post_render |
| 131 | + |
| 132 | + scene = bpy.context.scene |
| 133 | + if scene.stage_data.studios: |
| 134 | + studio = scene.stage_data.studios[0] |
| 135 | + else: |
| 136 | + studio = scene.stage_data.studios.add() |
| 137 | + studio.name = "TestStudio" |
| 138 | + studio.uuid = "sec-test" |
| 139 | + |
| 140 | + action = _stub_action("file:///etc/passwd") |
| 141 | + |
| 142 | + with mock.patch.object(post_render.urlrequest, "urlopen") as fake_open: |
| 143 | + post_render.slack_webhook(action, scene, studio, "/tmp/out.png") |
| 144 | + assert fake_open.call_count == 0, ( |
| 145 | + "urlopen must not be called for non-http(s) schemes" |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +def test_slack_webhook_refuses_gopher_scheme(): |
| 150 | + from stage.core import post_render |
| 151 | + |
| 152 | + scene = bpy.context.scene |
| 153 | + studio = scene.stage_data.studios[0] if scene.stage_data.studios else None |
| 154 | + if studio is None: |
| 155 | + studio = scene.stage_data.studios.add() |
| 156 | + studio.name = "TestStudio" |
| 157 | + studio.uuid = "sec-test" |
| 158 | + |
| 159 | + action = _stub_action("gopher://internal-host:70/0/secret") |
| 160 | + |
| 161 | + with mock.patch.object(post_render.urlrequest, "urlopen") as fake_open: |
| 162 | + post_render.slack_webhook(action, scene, studio, "/tmp/out.png") |
| 163 | + assert fake_open.call_count == 0 |
| 164 | + |
| 165 | + |
| 166 | +def test_slack_webhook_refuses_url_with_no_host(): |
| 167 | + from stage.core import post_render |
| 168 | + |
| 169 | + scene = bpy.context.scene |
| 170 | + studio = scene.stage_data.studios[0] if scene.stage_data.studios else None |
| 171 | + if studio is None: |
| 172 | + studio = scene.stage_data.studios.add() |
| 173 | + studio.name = "TestStudio" |
| 174 | + studio.uuid = "sec-test" |
| 175 | + |
| 176 | + action = _stub_action("https://") |
| 177 | + |
| 178 | + with mock.patch.object(post_render.urlrequest, "urlopen") as fake_open: |
| 179 | + post_render.slack_webhook(action, scene, studio, "/tmp/out.png") |
| 180 | + assert fake_open.call_count == 0 |
| 181 | + |
| 182 | + |
| 183 | +def test_slack_webhook_accepts_https_url(): |
| 184 | + """Sanity check the happy path — hooks.slack.com style URLs must\n still go through.""" |
| 185 | + from stage.core import post_render |
| 186 | + |
| 187 | + scene = bpy.context.scene |
| 188 | + studio = scene.stage_data.studios[0] if scene.stage_data.studios else None |
| 189 | + if studio is None: |
| 190 | + studio = scene.stage_data.studios.add() |
| 191 | + studio.name = "TestStudio" |
| 192 | + studio.uuid = "sec-test" |
| 193 | + |
| 194 | + action = _stub_action("https://hooks.slack.com/services/T0/B0/XXX") |
| 195 | + |
| 196 | + with mock.patch.object(post_render.urlrequest, "urlopen") as fake_open: |
| 197 | + post_render.slack_webhook(action, scene, studio, "/tmp/out.png") |
| 198 | + assert fake_open.call_count == 1, ( |
| 199 | + "valid https URL should reach urlopen" |
| 200 | + ) |
0 commit comments