Skip to content

Restricted VBA source: SolidWorks and nothing else - #248

Merged
andrewbartels1 merged 13 commits into
mainfrom
feat/vba-restricted
Nov 18, 2026
Merged

andrewbartels1 merged 13 commits into
mainfrom
feat/vba-restricted

Conversation

@andrewbartels1

Copy link
Copy Markdown
Owner

Merge after #245, #246 and #247, in that order. Until they merge, this PR shows their commits too.

Restricted VBA source: SolidWorks and nothing else (queue item 28)

#246 runs VBA source with full access when allowed. VBA runs inside SolidWorks with your Windows rights, and Windows can't take rights away from one macro, so this adds a check of the source before it runs.

SOLIDWORKS_MCP_ALLOW_VBA_SOURCE now takes three values. This replaces #246's on/off (1) before it merges.

Value vba_source
off (default, and when unset) refused as vba_source_off
restricted runs only if it passes the check; else vba_source_refused, listing each refused name and line
full runs unchecked

Anything else, including 1 or true, stops the server at start with the three values listed, so nobody gets full by guessing.

The check (tools/vba_check.py, with no COM) ignores comments and quoted text. Every other name must be one of:

  • a VBA keyword or safe function (maths, text, conversions, dates, MsgBox, InputBox, Err, Debug.Print, vb… constants);
  • a name the macro declares;
  • a SolidWorks enum or type name;
  • Application.SldWorks;
  • after a ., a member of the SolidWorks type library installed on this machine.

It refuses VBA that reaches outside SolidWorks: Shell, CreateObject/GetObject, CallByName, Declare, file statements, Environ, SendKeys and the registry functions. It also refuses about 100 SolidWorks members, picked from the type library, that:

  • save or export;
  • open or load files;
  • run macros or commands;
  • close or quit;
  • change saved settings, add-ins or toolbars;
  • print or upload.

Anything not written in the source can only be reached through calls that are refused, so checking the written names covers what the macro can do.

Disclosure. The installation guide lists:

  • every value and the default;
  • what restricted allows and refuses, with the full blocked list, which a test keeps in step with the code;
  • what the check can't promise: it isn't a sandbox, and allowed SolidWorks calls can still change the open documents.

Checks

  • 26 checker tests:

    • a SolidWorks-only macro using With, continuations, Type, labels and functions passes;
    • each refused kind is named with its line;
    • quoted or commented Shell doesn't count;
    • the docs list matches the code.
  • Tool tests: restricted passes and refuses; restricted without a type library refuses; guessed values stop the server.

  • Checked against the real 2026 type library: the live test's macro passes, and with Shell added it is refused at line 11.

  • 4,328 unit tests pass at 100% coverage; ruff and mypy are clean; the docs build in strict mode.

  • Live: not run yet. tests/live/test_live_sw_macro_source.py now runs three cases: restricted source adds a sketch; the same source with a Shell line is refused; off refuses.

    powershell -ExecutionPolicy Bypass -File tests\scripts\live_demo.ps1 -Test tests/live/test_live_sw_macro_source.py

andrewbartels1 and others added 13 commits November 16, 2026 22:48
make typecheck runs mypy over the package (checked as on Windows), and CI
runs it before the tests. The fixes change no behaviour, apart from one
fault mypy found: dimension layout raised TypeError for a leader with no
lines when asked to place it inside its view; it now goes outside. Every
type: ignore names its error code, checked by a test. Queue item 17, #157.
…stants library

The API index now also reads swconst.tlb (983 enums on SolidWorks 2026;
index format 2.1, so a saved index is rebuilt once). get_api_enum takes an
enum's name or a member's and returns the enum's members and integer
values. Checked live: swUnitSystem_IPS is 3. Named under the tool-naming
rule (get_, reads one thing). Queue item 23, #95. 140 tools.
They were written as a literal \n, so the shell saw an unfinished if.
CI's environment has no PIL, faiss or sentence_transformers, and its
pydantic doesn't need the ignore on the annotated-union TypeAdapter.
tests/integration/test_tool_catalog.py fails when a registered tool has no
entry on its family's page, an entry's parameter table doesn't list exactly
the tool's inputs, or its sample call isn't one the tool's input model
accepts. Entries for the 20 tools that had none, about 30 stale tables and
samples fixed, a Server Admin page. The generator that overwrote the
hand-written pages is removed, with the harness tests that read its JSON
(always skipped in CI). Queue item 24, #156.
While execute_macro runs, a watchdog thread (Win32 window calls only, no
COM) looks for a dialog the SolidWorks process opened. One still open after
dialog_timeout seconds (30) is answered with Cancel, or its only button,
or closed when it has none; the run then fails as macro_waited_for_user,
naming the dialog and its answer. A Yes/No prompt is left for the user and
logged. Unit-tested; the live check is written for the user's run.
Queue item 25, #97.
A macro that waits for the user no longer hangs the server
With SOLIDWORKS_MCP_ALLOW_VBA_SOURCE=1, execute_macro takes vba_source:
the text is written to a .swb under the temp folder and run like a macro
file, with the dialog watchdog; the reply names the file and repeats the
source. Off by default (the user's choice): VBA can do anything the user
can, so the call is refused as vba_source_off and nothing is written.
Queue item 26, #96.
SolidWorks' Save Bodies can leave a new, unsaved assembly open, and it
becomes the active document. The adapter now notes the open documents
before the call, closes any new unsaved assembly afterwards, reactivates
the part, and lists what it closed in closed_documents. Nothing open
before, or with a file, is touched. Queue item 27, #101.
SOLIDWORKS_MCP_ALLOW_VBA_SOURCE is now off (default), restricted or full;
any other value, including 1, stops the server at start. restricted reads
the source first: every name outside comments and strings must be a VBA
keyword or safe function, a name the macro declares, a SolidWorks enum or
type, Application.SldWorks, or a member of the installed type library.
Shell, CreateObject, file statements and about 100 SolidWorks members that
save, open, run, close or change settings are refused as
vba_source_refused, naming each line. The installation guide lists every
value, the default, what each allows and refuses, and the limits; a test
keeps its list in step with the code. Queue item 28.
@andrewbartels1
andrewbartels1 merged commit 03def58 into main Nov 18, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant