Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

1.7 KB
Raw Blame
import type { FiltersConfig } from 'librechat-data-provider';
import { ContentFilterError } from '../middleware/contentFilter';
import { assertDirectToolOutputAllowed } from './protection';

const filters: FiltersConfig = {
  toolArguments: {
    pii: {
      fields: ['output'],
      starterPatterns: [],
      customPatterns: [{ id: 'private', label: 'private output', regex: 'PRIVATE-OUTPUT' }],
    },
  },
};

describe('direct tool output protection', () => {
  it('blocks protected leaves in structured direct-call output', () => {
    expect(() =>
      assertDirectToolOutputAllowed(filters, 'execute_code', {
        nested: { value: 'PRIVATE-OUTPUT' },
      }),
    ).toThrow(ContentFilterError);
  });

  it('does not traverse output when output policy is inactive', () => {
    const output = {};
    Object.defineProperty(output, 'value', {
      enumerable: true,
      get() {
        throw new Error('must not read');
      },
    });

    expect(() =>
      assertDirectToolOutputAllowed(
        { toolArguments: { pii: { fields: ['arguments'], starterPatterns: [] } } },
        'execute_code',
        output,
      ),
    ).not.toThrow();
  });

  it('allows an untraversable direct-call output for an audit-only policy', () => {
    const output = new Proxy(
      { value: 'hidden' },
      {
        ownKeys: () => {
          throw new Error('opaque');
        },
      },
    );

    expect(() =>
      assertDirectToolOutputAllowed(
        {
          toolArguments: {
            pii: {
              ...filters.toolArguments?.pii,
              action: 'audit',
            },
          },
        },
        'execute_code',
        output,
      ),
    ).not.toThrow();
  });
});