Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

2.9 KB
Raw Blame
import { Operator } from '@/constants/agent';
import { cloneDeepWith, get, isPlainObject } from 'lodash';

const apiKeyOperators = [
  Operator.TavilySearch,
  Operator.TavilyExtract,
  Operator.Google,
  Operator.KeenableSearch,
  Operator.YouComSearch,
  Operator.SofyaSearch,
  Operator.BGPT,
  Operator.QueritContents,
  Operator.QueritSearch,
];

// Canvas nodes carry the operator under `data.label` and the key under
// `data.form.api_key`, so a graph export has to be sanitized separately from the
// agent-tool records above.
const nodeLabelApiKeyOperators: string[] = [
  Operator.YouComSearch,
  Operator.SofyaSearch,
];

const WebhookSecretFields = {
  token: 'token_value',
  basic_auth: 'password',
  jwt: 'secret',
  hmac: 'secret',
} as const;

function isQueritOperator(value: unknown) {
  if (typeof value !== 'string') {
    return false;
  }

  return ['querit', 'queritcontents', 'queritsearch'].includes(
    value.replace(/_/g, '').toLowerCase(),
  );
}

function isSearch1APIOperator(value: unknown) {
  if (typeof value !== 'string') {
    return false;
  }

  return ['search1api', 'search1apisearch', 'search1apicrawl'].includes(
    value.replace(/_/g, '').toLowerCase(),
  );
}

function isNodeLabelApiKeyOperator(value: unknown) {
  if (typeof value !== 'string') {
    return false;
  }

  return (
    isQueritOperator(value) ||
    isSearch1APIOperator(value) ||
    nodeLabelApiKeyOperators.includes(value)
  );
}

/** Returns a copy with recognized webhook credentials blanked. */
function clearWebhookSecrets(value: Record<string, any>) {
  const security = value.security;
  if (!isPlainObject(security)) {
    return;
  }

  let changed = false;
  const sanitizedSecurity = { ...security };
  for (const [section, field] of Object.entries(WebhookSecretFields)) {
    const auth = sanitizedSecurity[section];
    if (isPlainObject(auth) && auth[field]) {
      sanitizedSecurity[section] = { ...auth, [field]: '' };
      changed = true;
    }
  }

  if (changed) {
    return { ...value, security: sanitizedSecurity };
  }
}

/** Returns a copy of the DSL with recognized credentials removed. */
export function clearSensitiveFields<T>(obj: T): T {
  return cloneDeepWith(obj, (value) => {
    if (!isPlainObject(value)) {
      return;
    }

    if (
      (apiKeyOperators.includes(value.component_name) ||
        isQueritOperator(value.component_name) ||
        isSearch1APIOperator(value.component_name)) &&
      get(value, 'params.api_key')
    ) {
      return { ...value, params: { ...value.params, api_key: '' } };
    }

    if (
      isNodeLabelApiKeyOperator(get(value, 'data.label')) &&
      get(value, 'data.form.api_key')
    ) {
      return {
        ...value,
        data: {
          ...value.data,
          form: {
            ...value.data.form,
            api_key: '',
          },
        },
      };
    }

    const sanitizedWebhookForm = clearWebhookSecrets(value);
    if (sanitizedWebhookForm) {
      return sanitizedWebhookForm;
    }
  });
}