Repository navigation
feat: add Rust framework support #947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ff7610d
6937381
020e319
7833e7a
62e9e2a
95c1af1
0c4d843
24bb9b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* Rust wizard using posthog-agent with PostHog MCP */ | ||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import type { WizardRunOptions } from '@utils/types'; | ||
| import type { FrameworkConfig } from '@lib/framework-config'; | ||
| import { cargoPackageManager } from '@lib/detection/package-manager'; | ||
| import { Integration } from '@lib/constants'; | ||
|
|
||
| type RustContext = { | ||
| asyncRuntime?: boolean; | ||
| }; | ||
|
|
||
| function readCargoToml(installDir: string): string | undefined { | ||
| const cargoTomlPath = path.join(installDir, 'Cargo.toml'); | ||
| if (!fs.existsSync(cargoTomlPath)) { | ||
| return undefined; | ||
| } | ||
| return fs.readFileSync(cargoTomlPath, 'utf-8'); | ||
| } | ||
|
|
||
| /** posthog-rs defaults to its async client; projects without an async runtime need the blocking one. */ | ||
| function hasAsyncRuntime(installDir: string): boolean { | ||
| const cargoToml = readCargoToml(installDir); | ||
| return !!cargoToml && /^\s*(tokio|async-std|smol)\s*[=.]/m.test(cargoToml); | ||
| } | ||
|
|
||
| export const RUST_AGENT_CONFIG: FrameworkConfig<RustContext> = { | ||
| metadata: { | ||
| name: 'Rust', | ||
| integration: Integration.rust, | ||
| docsUrl: 'https://posthog.com/docs/libraries/rust', | ||
| gatherContext: (options: WizardRunOptions) => { | ||
| const asyncRuntime = hasAsyncRuntime(options.installDir); | ||
| return Promise.resolve({ asyncRuntime }); | ||
| }, | ||
| }, | ||
|
|
||
| detection: { | ||
| packageName: 'posthog-rs', | ||
| packageDisplayName: 'Rust', | ||
| usesPackageJson: false, | ||
| getVersion: () => undefined, | ||
| // A Cargo.toml with a [package] section marks a crate root. A | ||
| // workspace-root-only manifest ([workspace], no [package]) falls | ||
| // through — the agentic monorepo scan finds the nested crates. | ||
| detect: (options) => { | ||
| const cargoToml = readCargoToml(options.installDir); | ||
| return Promise.resolve(!!cargoToml && /^\s*\[package\]/m.test(cargoToml)); | ||
| }, | ||
| detectPackageManager: cargoPackageManager, | ||
| }, | ||
|
|
||
| environment: { | ||
| uploadToHosting: false, | ||
| getEnvVars: (apiKey: string, host: string) => ({ | ||
| POSTHOG_API_KEY: apiKey, | ||
| POSTHOG_HOST: host, | ||
| }), | ||
| }, | ||
|
|
||
| analytics: { | ||
| getTags: (context) => ({ | ||
| asyncRuntime: context.asyncRuntime ? 'true' : 'false', | ||
| }), | ||
| }, | ||
|
|
||
| prompts: { | ||
| projectTypeDetection: | ||
| 'This is a Rust project. Look for Cargo.toml with a [package] section, src/main.rs or src/lib.rs, and Cargo.lock to confirm.', | ||
| packageInstallation: | ||
| 'Install the PostHog Rust SDK with `cargo add posthog-rs`. Do not manually edit Cargo.toml; cargo updates it automatically.', | ||
| getAdditionalContextLines: (context) => { | ||
| const lines = [ | ||
| `Framework docs ID: rust (use posthog://docs/frameworks/rust for documentation)`, | ||
| 'capture() hands events to a background worker — the client must flush() or shutdown() before the process exits or buffered events are lost.', | ||
| ]; | ||
| if (context.asyncRuntime) { | ||
| lines.push( | ||
| 'An async runtime is present — use the default async posthog-rs client.', | ||
| ); | ||
| } else { | ||
| lines.push( | ||
| 'No async runtime detected — install with `cargo add posthog-rs --no-default-features` and use the blocking client.', | ||
| ); | ||
| } | ||
| return lines; | ||
| }, | ||
| }, | ||
|
|
||
| ui: { | ||
| successMessage: 'PostHog integration complete', | ||
| estimatedDurationMinutes: 5, | ||
| getOutroChanges: () => [ | ||
| 'Analyzed your Rust project structure', | ||
| 'Installed the posthog-rs crate via cargo add', | ||
| 'Initialized a shared PostHog client configured from environment variables', | ||
| 'Instrumented meaningful events with client.capture', | ||
| ], | ||
| getOutroNextSteps: () => [ | ||
| 'Run your Rust application and trigger the instrumented code paths', | ||
| 'Visit your PostHog dashboard to see incoming events', | ||
| 'Use Event::new(event_name, distinct_id) and client.capture to track custom events', | ||
| 'Keep flush()/shutdown() in your exit path so queued events are sent', | ||
| ], | ||
| }, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,21 @@ const SIMPLE_MANAGERS: Record<string, readonly string[]> = { | |
| swift: ['package', 'build'], | ||
| pod: ['install', 'update', 'search'], | ||
| carthage: ['bootstrap', 'update'], | ||
| // cargo build/check also execute code (build.rs build scripts + proc-macros), | ||
| // accepted under the "builds are equivalent risk" model in the file header. | ||
| // run/test are denied so the finished binary/tests aren't run; install/publish | ||
| // are outward-facing. | ||
| cargo: [ | ||
| 'add', | ||
| 'remove', | ||
| 'build', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. High: Cargo build scripts bypass the execution fence An attacker can execute arbitrary commands on a developer's machine by placing a malicious |
||
| 'check', | ||
| 'fmt', | ||
| 'clippy', | ||
| 'metadata', | ||
| 'tree', | ||
| 'fetch', | ||
| ], | ||
| // mix runs arbitrary project-defined tasks, so only the dependency + build | ||
| // tasks are listed. compile also executes code (compile-time macros), accepted | ||
| // under the "builds are equivalent risk" model in the file header; run/test/ | ||
|
|
@@ -138,6 +153,7 @@ const ALLOWED_TOOLS_SUMMARY = | |
| 'xcodegen (generate), xcodebuild (build/clean/archive actions), gradle/gradlew (build|clean|dependencies|assemble*/compile*/bundle*/lint* tasks), ' + | ||
| 'mvn (install|compile|package|verify|dependency:tree), ' + | ||
| 'flutter/dart (pub add/remove/get/upgrade/outdated/deps, analyze, build, clean, doctor), ' + | ||
| 'cargo (add|remove|build|check|fmt|clippy|metadata|tree|fetch), ' + | ||
| 'mix (deps.get|deps.update|deps.tree|compile|format|hex.info), ' + | ||
| 'go (get|build|vet|fmt|version|list, mod tidy/download/verify/graph/why).'; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
High: Repository-controlled Cargo credential provider execution
Cargo loads
.cargo/config.tomlfrom the current project, and authenticated registries can configure credential providers as external executables. An attacker can commit a provider script and registry configuration, then cause the agent to run an allowedcargo addorcargo fetch; Cargo executes the script without exposing its command to this fence or the pre-execution scan. Deny these Cargo operations in the unsandboxed harness, or run them through a trusted wrapper that rejects repository-local executable configuration. (doc.rust-lang.org)