Skip to content

RFC: server-side plugins - #1075

Draft
Rerowros wants to merge 31 commits into
PasarGuard:devfrom
Rerowros:feat/plugins
Draft

Rerowros wants to merge 31 commits into
PasarGuard:devfrom
Rerowros:feat/plugins

Conversation

@Rerowros

Copy link
Copy Markdown
Contributor

Summary

RFC / draft. A small server-side plugin system, so panel owners can add their own logic and pages without forking. The goal is to cover the common needs with a minimal, explicit API, not to build a full extension framework. Common needs here: reports, notifications, subscription tweaks and small admin pages.

  • Plugins are Python packages in PLUGINS_DIR, enabled by name with PLUGINS=a,b. Nothing loads by default. Each plugin has a plugin.toml manifest and a setup(ctx) entry point.
  • ctx gives a plugin:
    • an API router at /api/plugins/<id>; every route requires an authenticated admin, and owner-only routes are available;
    • panel events (user.created, user.status_changed, node.error, admin.login, …), the same set as panel notifications;
    • subscription hooks: transform the content, add headers, add template variables;
    • periodic jobs, by interval or cron;
    • settings described by a JSON schema, stored in plugin_settings (migration d4e7a9c2b5f1), editable in the dashboard and synced to other workers over NATS;
    • a DB session.
  • Optional UI: static files shown in an iframe under the dashboard's "Plugins" section, for owners only.
  • If a plugin fails to load, the error is logged and shown on the Plugins page. None of the plugin's routes, hooks or jobs are registered, and the panel still starts.
  • Two examples in examples/plugins/:
    • expiring-report: a page listing users that expire within N days, with CSV export and a daily log line;
    • sub-banner: an info line on top of link subscriptions, plus a {PLAN} variable.
  • Docs are in the README.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Refactor / cleanup
  • Documentation
  • Tests / CI

Checklist

  • I tested the change locally or explained why it cannot be tested.
  • I added or updated tests for behavior changes.
  • I updated documentation, translations, or examples if needed.
  • I checked database migrations when models or schema changed.
  • I did not include secrets, tokens, private keys, or unrelated changes.

Testing

uv run pytest tests/api/test_plugins_api.py tests/api/test_plugins_examples.py tests/test_plugins_loader.py -q   # 33 passed
cd dashboard && bun test src/features/plugins/settings-schema.test.ts
bun run build

By hand with PLUGINS=expiring-report,sub-banner: both plugins load, the plugin page opens in the iframe, the settings form saves, and the banner appears in a link subscription.

Screenshots

Will add in a comment.

Notes for reviewers

  • Plugins are trusted code by design, like any Python dependency. They run in the panel process with full database access. The plugin UI iframe is same-origin and receives the owner's token, so it is not isolated either. The README states this plainly.
  • Each event reaches plugins once, in the process where it happened, with the same deduplication as notifications.
  • Open questions where your direction would help:
    1. Is this API surface the right size, or should something be cut before it becomes a compatibility promise?
    2. Should JS-only UI plugins, without Python, be supported?
    3. Should plugin.toml declare a minimum panel version?
  • The migration branches from c8d2e4f6a1b3.

@coderabbitai

coderabbitai Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment thread app/routers/plugin.py Fixed
CI runs the TimescaleDB and MariaDB jobs with NATS and two workers, where the
15 s notification dedup dropped login and user events already claimed by
earlier tests.
…cure on private IPs

The plugin UI cookie held the owner's full API token and outlived logout.
It now holds a 30 minute token (access=plugin_ui, pid=<plugin>) that the
panel API rejects and that opens only the UI files of that plugin; the
cookie is a session cookie and the dashboard renews it while the page is
open. Plain HTTP on a private or loopback IP no longer sets Secure, so the
iframe loads there too.
…ling /sub

A headers hook that returned a non latin-1 value, a CR/LF or a bad name
made every /sub request answer 400. Each pair is now checked with the
same rules as admin headers (moved to app/utils/headers.py), lowercased,
and a bad pair is logged and skipped. Cache-Control is set lowercase so a
plugin cannot add a second one.
… form

A nullable string saved as '' after any save, and an empty optional enum
was sent as '' and refused with 422. An empty input now becomes null when
the schema allows null; otherwise the key is left out, except a text field
that already had a value.
…load

If loading the settings failed, the form stayed enabled and Save replaced
the stored settings with empty values. Fields and Save now stay disabled
until the settings load, and the error is shown with a retry button.
Also link field descriptions and errors to the inputs for screen readers
and reuse the common save/saving texts.
… direction

The plugins pages showed "no plugins" or "not found" when the request
failed; they now show the error with a retry button. The plugin name in
the page header is no longer passed through i18n. The plugin frame is
keyed by plugin id, and the SDK gets the text direction from the
dashboard instead of guessing it from lang === "fa".
The facade argument is named api_key, which is in SENSITIVE_KEYS, so the
whole key object was dropped and plugins got no id, name or status. Only
plain values are dropped by name now; models keep their fields and still
lose the raw api_key field.
With UVICORN_WORKERS>1 plugin events shared the notification dedup slot,
keyed by event name and object id only, so a state change inside the
15 s window (disabled then enabled, failed then successful login) was
lost. Plugin events now claim their own slot keyed by the event and its
payload without usage counters and timestamps; notifications keep their
dedup as before.
…ground

The NATS listener awaits handlers one by one, so a slow on_change hook
held up every other sync message (settings, nodes, cores, hosts) on all
workers. A refresh now applies the values and clears the subscription
cache at once, then runs the hooks in a background task; hooks of one
plugin run one change at a time, in order. A sync hook runs in a worker
thread with the same 10 s limit. Pending hooks are awaited at shutdown.
The settings PUT still waits for the hooks in its own worker. The test
of the PUT now also checks the message published to other workers and a
PUT from a non-owner.
/settings, /ui-session and /ui/... under /api/plugins/<id> belong to the
panel and are matched first, so such a plugin route never ran while the
plugin showed as loaded. Activation now fails with a clear error, also
for routes from included sub-routers.
…ngs at once

The second INSERT broke the primary key and the owner got a 503. The
save now rolls back and updates the row the other worker created. The
extra refresh after commit is gone.
init_plugins: stored settings are applied at startup, the NATS handler is
registered on the plugin topic, and a failing async setup or activation
leaves the plugin failed with no routes. Changing settings clears cached
subscriptions (via PUT and via a NATS refresh), and invalid stored
settings fall back to defaults.
… hooks

The banner opened a second pooled connection on every uncached render
while the request still held its own; under load it waited for the pool
timeout and the banner silently disappeared. The plugin now keeps user
id -> group names in memory: loaded in an async setup at startup,
refreshed in the background every 60 s, and updated from user events.
The hooks only read that map.
…last

Two errors on the same field overwrote each other in the 422 detail; they
are joined now.
It shared its name with the PluginSettings ORM model; the env variables
are unchanged.
…outes

Protected plugin routes let in any admin or API key, sub-admins and
limited keys included. Plugins can now require a role permission the same
way panel routes do; the docstring says plainly what "admin" means.
Covers /sub/<token> with the format picked by User-Agent: the plugin sees
that format, the admin's response header beats the plugin header, a
built-in header the admin did not set is overridden by the plugin, and
the admin's custom variable beats the plugin variable.
…in all READMEs

The README is a short landing page kept in four languages, so it now has
a few lines on plugins (what they are, the trust warning, PLUGINS_DIR and
PLUGINS) in English, Persian, Russian and Chinese, linking to the full
guide in examples/plugins/README.md. The guide is updated: what "admin"
means for plugin routes and ctx.deps.permission, reserved paths, the
short plugin-only UI cookie, the text direction, and keeping database
reads out of subscription hooks.
With 2 workers and NATS the cross-worker claim could wait about 5 s for a
NATS timeout, so the handler did not run in time.
The notification claim runs before the plugin event and can also wait for
a NATS timeout with 2 workers.
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.

2 participants