-
Notifications
You must be signed in to change notification settings - Fork 87
Add source column to AuditLog with request context propagation #7825
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9c1d473
Add "source" tracking to AuditLog with propagation and nonce-based co…
cstns 37cb401
Merge remote-tracking branch 'origin/main' into feat/7562-audit-sourc…
cstns f883bdd
Add optional "source" field to AuditLog interface in generated types
cstns 6061a59
Update AuditLog tests to remove redundant `app` parameter in controll…
cstns 27f864e
Merge branch 'main' into feat/7562-audit-source-column
cstns 7dfc783
Add tests for source context resolution and prevent metadata mutation…
cstns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* eslint-disable no-unused-vars */ | ||
| /** | ||
| * Add source column to AuditLogs table | ||
| * | ||
| * Tracks how an audited action was triggered: | ||
| * null - legacy / cookie session (UI) | ||
| * 'api' - PAT-authenticated direct API call | ||
| * 'mcp:expert' - first-party expert agent via MCP | ||
| * 'mcp' - third-party MCP agent (future) | ||
| */ | ||
|
|
||
| const { DataTypes } = require('sequelize') | ||
|
|
||
| module.exports = { | ||
| up: async (context) => { | ||
| await context.addColumn('AuditLogs', 'source', { | ||
| type: DataTypes.STRING, | ||
| allowNull: true, | ||
| defaultValue: null | ||
| }) | ||
| }, | ||
| down: async (context) => { | ||
| await context.removeColumn('AuditLogs', 'source') | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** | ||
| * Generic in-memory nonce store. | ||
| * | ||
| * Each nonce is single-use (consumed on first read) and expires after a | ||
| * configurable TTL. Callers attach arbitrary metadata when creating a nonce | ||
| * and receive it back on consumption. | ||
| * | ||
| * In-memory only: intended for process-local trust boundaries such as | ||
| * Fastify's app.inject(), where the producer and consumer always live in | ||
| * the same Node.js process. | ||
| * | ||
| * Registered as a Fastify plugin on `app.nonceStore`. | ||
| */ | ||
|
|
||
| const crypto = require('crypto') | ||
|
|
||
| const fp = require('fastify-plugin') | ||
|
|
||
| const DEFAULT_TTL_MS = 30_000 // 30 seconds | ||
| const SWEEP_INTERVAL_MS = 60_000 // 1 minute | ||
|
|
||
| class NonceStore { | ||
| constructor () { | ||
| /** @type {Map<string, { metadata: object, expiresAt: number }>} */ | ||
| this._store = new Map() | ||
| this._sweepTimer = setInterval(() => this._sweep(), SWEEP_INTERVAL_MS) | ||
| this._sweepTimer.unref() | ||
| } | ||
|
|
||
| /** | ||
| * Create a nonce and associate metadata with it. | ||
| * @param {object} metadata - Arbitrary data returned on consume() | ||
| * @param {number} [ttl] - Time-to-live in ms (default 30 000) | ||
| * @returns {string} The nonce token | ||
| */ | ||
| create (metadata, ttl = DEFAULT_TTL_MS) { | ||
| const nonce = crypto.randomBytes(16).toString('hex') | ||
| this._store.set(nonce, { | ||
| metadata, | ||
| expiresAt: Date.now() + ttl | ||
| }) | ||
| return nonce | ||
| } | ||
|
|
||
| /** | ||
| * Create a nonce for source context propagation. | ||
| * Validates that source is present. Generates a correlationId if not provided. | ||
| * @param {{ source: string, correlationId?: string, toolName?: string, tokenId?: number }} metadata | ||
| * @param {number} [ttl] - Time-to-live in ms (default 30 000) | ||
| * @returns {string} The nonce token | ||
| */ | ||
| createSourceNonce (metadata, ttl) { | ||
| if (!metadata?.source) { | ||
| throw new Error('source is required for source context nonces') | ||
| } | ||
| const meta = { ...metadata } | ||
| if (!meta.correlationId) { | ||
| meta.correlationId = crypto.randomUUID() | ||
| } | ||
| return this.create(meta, ttl) | ||
| } | ||
|
|
||
| /** | ||
| * Consume a nonce, returning its metadata exactly once. | ||
| * Returns null if the nonce does not exist or has expired. | ||
| * @param {string} nonce | ||
| * @returns {object|null} | ||
| */ | ||
| consume (nonce) { | ||
| const entry = this._store.get(nonce) | ||
| if (!entry) { | ||
| return null | ||
| } | ||
| this._store.delete(nonce) | ||
| if (entry.expiresAt < Date.now()) { | ||
| return null | ||
| } | ||
| return entry.metadata | ||
| } | ||
|
|
||
| /** Remove all expired entries. */ | ||
| _sweep () { | ||
| const now = Date.now() | ||
| for (const [key, entry] of this._store) { | ||
| if (entry.expiresAt < now) { | ||
| this._store.delete(key) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Stop the periodic sweep timer. */ | ||
| close () { | ||
| clearInterval(this._sweepTimer) | ||
| this._store.clear() | ||
| } | ||
| } | ||
|
|
||
| module.exports = fp(async function (app) { | ||
| const store = new NonceStore() | ||
| app.decorate('nonceStore', store) | ||
| app.addHook('onClose', () => store.close()) | ||
| }, { name: 'app.nonceStore' }) | ||
|
|
||
| module.exports.NonceStore = NonceStore | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.