Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

17.6 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';

import type { DecryptedConnector } from '@/database/models/connector';
import {
  ensureFreshConnectorToken,
  ensureFreshLinkToken,
} from '@/server/services/connector/tokens';

import {
  ForbiddenWidgetCredentialsError,
  MissingWidgetEnvError,
  resolveWidgetEnv,
  selectConnectorSecret,
  type WidgetCredentialScope,
} from '../credentials';

vi.mock('@/server/services/connector/tokens', () => ({
  ensureFreshConnectorToken: vi.fn(async (connector) => connector),
  ensureFreshLinkToken: vi.fn(async (_connector, link) => link),
}));

const connector = (overrides: Partial<DecryptedConnector>): DecryptedConnector =>
  ({
    agentId: null,
    credentials: { token: 'tok', type: 'bearer' },
    identifier: 'github',
    isEnabled: true,
    status: 'connected',
    userId: 'u1',
    workspaceId: null,
    ...overrides,
  }) as DecryptedConnector;

const modelReturning = (rows: DecryptedConnector[], accounts: object[] = []) => ({
  getLinkModel: () => ({ listByConnectorIds: async () => accounts }),
  resolveByIdentifiers: vi.fn().mockResolvedValue(rows),
  update: vi.fn(),
});

const personal: WidgetCredentialScope = {
  agentId: null,
  authorUserId: 'u1',
  projectId: null,
  userId: 'u1',
  workspaceId: null,
};
const workspace: WidgetCredentialScope = { ...personal, workspaceId: 'ws1' };

describe('selectConnectorSecret', () => {
  it.each([
    [{ accessToken: 'a', type: 'oauth2' }, 'a'],
    [{ token: 'b', type: 'bearer' }, 'b'],
    [{ apiKey: 'c', type: 'apikey' }, 'c'],
    [{ headers: { Authorization: 'Bearer d' }, type: 'header' }, 'd'],
  ] as const)('extracts the secret from %o', (credentials, expected) => {
    expect(selectConnectorSecret(credentials as any)).toEqual({ secret: expected });
  });

  it('returns no secret without credentials', () => {
    expect(selectConnectorSecret(null)).toEqual({ secret: undefined });
  });

  const multiHeader = {
    headers: { 'X-Api-Key': 'key-1', 'X-Tenant': 'tenant-1' },
    type: 'header',
  } as const;

  it('picks the named header, case-insensitively', () => {
    expect(selectConnectorSecret(multiHeader, 'x-tenant')).toEqual({ secret: 'tenant-1' });
    expect(selectConnectorSecret(multiHeader, 'X-Api-Key')).toEqual({ secret: 'key-1' });
  });

  it('refuses to guess between several headers', () => {
    expect(selectConnectorSecret(multiHeader)).toEqual({
      error: 'ambiguous_field',
      fields: ['X-Api-Key', 'X-Tenant'],
    });
  });

  it('reports a named header the connector does not hold', () => {
    expect(selectConnectorSecret(multiHeader, 'X-Org')).toEqual({
      error: 'unknown_field',
      fields: ['X-Api-Key', 'X-Tenant'],
    });
  });
});

describe('resolveWidgetEnv', () => {
  it('returns an empty env when nothing is declared', async () => {
    const model = modelReturning([]);

    expect(
      await resolveWidgetEnv({} as any, personal, undefined, { connectorModel: model as any }),
    ).toEqual({ env: {}, inject: [] });
    expect(model.resolveByIdentifiers).not.toHaveBeenCalled();
  });

  it('resolves through the agent-aware chain on the widget scope', async () => {
    const model = modelReturning([
      connector({ credentials: { token: 'agent-tok', type: 'bearer' } }),
    ]);

    const { env } = await resolveWidgetEnv(
      {} as any,
      { ...personal, agentId: 'agt_1' },
      [{ connector: 'github', name: 'GITHUB_TOKEN' }],
      { connectorModel: model as any },
    );

    expect(env).toEqual({ GITHUB_TOKEN: 'agent-tok' });
    expect(model.resolveByIdentifiers).toHaveBeenCalledWith(['github'], { agentId: 'agt_1' });
  });

  it('never lets a personal credential reach a workspace widget', async () => {
    const model = modelReturning([connector({ workspaceId: null })]);

    await expect(
      resolveWidgetEnv({} as any, workspace, [{ connector: 'github', name: 'GITHUB_TOKEN' }], {
        connectorModel: model as any,
      }),
    ).rejects.toBeInstanceOf(MissingWidgetEnvError);
  });

  it('skips disabled connectors and connectors without credentials', async () => {
    const model = modelReturning([
      connector({ isEnabled: false }),
      connector({ credentials: null, identifier: 'linear' }),
    ]);

    const error = await resolveWidgetEnv(
      {} as any,
      personal,
      [
        { connector: 'github', name: 'GITHUB_TOKEN' },
        { connector: 'linear', name: 'LINEAR_TOKEN' },
      ],
      { connectorModel: model as any },
    ).catch((e) => e);

    expect(error).toBeInstanceOf(MissingWidgetEnvError);
    expect(error.missing.map((m: { name: string }) => m.name)).toEqual([
      'GITHUB_TOKEN',
      'LINEAR_TOKEN',
    ]);
  });

  it('uses an API-key connector whose tool sync never ran (status disconnected)', async () => {
    const model = modelReturning([connector({ status: 'disconnected' })]);

    const { env } = await resolveWidgetEnv(
      {} as any,
      personal,
      [{ connector: 'github', name: 'GITHUB_TOKEN' }],
      { connectorModel: model as any },
    );

    expect(env).toEqual({ GITHUB_TOKEN: 'tok' });
  });

  it('lists every missing required variable in one explicit error', async () => {
    const error = await resolveWidgetEnv(
      {} as any,
      workspace,
      [
        { connector: 'github', name: 'GITHUB_TOKEN' },
        { name: 'ORG' },
        { connector: 'slack', name: 'SLACK_TOKEN', required: false },
      ],
      { connectorModel: modelReturning([]) as any },
    ).catch((e) => e);

    expect(error).toBeInstanceOf(MissingWidgetEnvError);
    expect(error.message).toBe(
      'Missing required environment: GITHUB_TOKEN (connect "github" for this widget\'s agent or workspace); ORG (no connector declared to provide it)',
    );
  });

  it('leaves optional variables out when they cannot be resolved', async () => {
    const { env } = await resolveWidgetEnv(
      {} as any,
      workspace,
      [{ connector: 'slack', name: 'SLACK_TOKEN', required: false }],
      { connectorModel: modelReturning([]) as any },
    );

    expect(env).toEqual({});
  });

  it('maps each header of a multi-header connector to the variable that names it', async () => {
    const model = modelReturning([
      connector({
        credentials: { headers: { 'X-Api-Key': 'key-1', 'X-Tenant': 'tenant-1' }, type: 'header' },
      }),
    ]);

    const { env } = await resolveWidgetEnv(
      {} as any,
      personal,
      [
        { connector: 'github', field: 'X-Api-Key', name: 'API_KEY' },
        { connector: 'github', field: 'X-Tenant', name: 'TENANT' },
      ],
      { connectorModel: model as any },
    );

    expect(env).toEqual({ API_KEY: 'key-1', TENANT: 'tenant-1' });
  });

  it('fails with an explicit error when a multi-header connector is not narrowed', async () => {
    const model = modelReturning([
      connector({
        credentials: { headers: { 'X-Api-Key': 'key-1', 'X-Tenant': 'tenant-1' }, type: 'header' },
      }),
    ]);

    const error = await resolveWidgetEnv(
      {} as any,
      personal,
      [
        { connector: 'github', name: 'API_KEY', required: false },
        { connector: 'github', field: 'X-Org', name: 'ORG' },
      ],
      { connectorModel: model as any },
    ).catch((e) => e);

    expect(error).toBeInstanceOf(MissingWidgetEnvError);
    expect(error.message).toBe(
      'Missing required environment: API_KEY (connector "github" holds several headers; set "field" to one of: X-Api-Key, X-Tenant); ORG (connector "github" has no header "X-Org"; it holds: X-Api-Key, X-Tenant)',
    );
  });

  describe('who may receive a connector secret', () => {
    const teammates = connector({ userId: 'u2', workspaceId: 'ws1' });
    const requirement = [{ connector: 'github', name: 'GITHUB_TOKEN' }];

    it('refuses a workspace connector another member created, before touching its token', async () => {
      const isWorkspaceOwner = vi.fn().mockResolvedValue(false);
      vi.mocked(ensureFreshConnectorToken).mockClear();

      const error = await resolveWidgetEnv({} as any, workspace, requirement, {
        connectorModel: modelReturning([teammates]) as any,
        isWorkspaceOwner,
      }).catch((e) => e);

      expect(error).toBeInstanceOf(ForbiddenWidgetCredentialsError);
      expect(error.connectors).toEqual(['github']);
      expect(isWorkspaceOwner).toHaveBeenCalledWith('u1', 'ws1');
      expect(ensureFreshConnectorToken).not.toHaveBeenCalled();
    });

    it('lets the workspace owner use any workspace connector', async () => {
      const { env } = await resolveWidgetEnv({} as any, workspace, requirement, {
        connectorModel: modelReturning([teammates]) as any,
        isWorkspaceOwner: vi.fn().mockResolvedValue(true),
      });

      expect(env).toEqual({ GITHUB_TOKEN: 'tok' });
    });

    it('lets the creator use their own workspace connector without an owner lookup', async () => {
      const isWorkspaceOwner = vi.fn();

      const { env } = await resolveWidgetEnv({} as any, workspace, requirement, {
        connectorModel: modelReturning([connector({ workspaceId: 'ws1' })]) as any,
        isWorkspaceOwner,
      });

      expect(env).toEqual({ GITHUB_TOKEN: 'tok' });
      expect(isWorkspaceOwner).not.toHaveBeenCalled();
    });

    // A script can read and send its environment anywhere
    describe("an OAuth connector's accounts", () => {
      const oauth = connector({
        credentials: null,
        id: 'c1',
        oidcConfig: { clientId: 'client', scheme: 'dcr' },
        workspaceId: 'ws1',
      } as Partial<DecryptedConnector>);
      const account = (id: string, userId: string, createdAt: number) => ({
        createdAt: new Date(createdAt),
        credentials: { accessToken: `token-${id}`, type: 'oauth2' },
        id,
        name: id,
        status: 'connected',
        tokenExpiresAt: null,
        userConnectorId: 'c1',
        userId,
      });
      const teammateFirst = [account('lnk_teammate', 'u2', 1), account('lnk_mine', 'u1', 2)];

      it("never hands the creator a teammate's token, even the primary one", async () => {
        const { env } = await resolveWidgetEnv({} as any, workspace, requirement, {
          connectorModel: modelReturning([oauth], teammateFirst) as any,
          isWorkspaceOwner: vi.fn().mockResolvedValue(false),
        });

        expect(env).toEqual({ GITHUB_TOKEN: 'token-lnk_mine' });
      });

      it('refuses when only teammates connected accounts, before refreshing any', async () => {
        vi.mocked(ensureFreshLinkToken).mockClear();
        const error = await resolveWidgetEnv({} as any, workspace, requirement, {
          connectorModel: modelReturning([oauth], [account('lnk_teammate', 'u2', 1)]) as any,
          isWorkspaceOwner: vi.fn().mockResolvedValue(false),
        }).catch((e) => e);

        expect(error).toBeInstanceOf(ForbiddenWidgetCredentialsError);
        expect(error.connectors).toEqual(['github']);
        expect(ensureFreshLinkToken).not.toHaveBeenCalled();
      });

      it("lets the workspace owner use a member's account when they have none", async () => {
        const { env } = await resolveWidgetEnv({} as any, workspace, requirement, {
          connectorModel: modelReturning([oauth], [account('lnk_teammate', 'u2', 1)]) as any,
          isWorkspaceOwner: vi.fn().mockResolvedValue(true),
        });

        expect(env).toEqual({ GITHUB_TOKEN: 'token-lnk_teammate' });
      });
    });

    it('injects nothing once the author no longer exists', async () => {
      await expect(
        resolveWidgetEnv({} as any, { ...workspace, authorUserId: null }, requirement, {
          connectorModel: modelReturning([connector({ workspaceId: 'ws1' })]) as any,
          isWorkspaceOwner: vi.fn().mockResolvedValue(true),
        }),
      ).rejects.toBeInstanceOf(ForbiddenWidgetCredentialsError);
    });
  });

  describe('LobeHub Market connectors', () => {
    const market = (overrides: Partial<DecryptedConnector> = {}) =>
      connector({ credentials: null, sourceType: 'marketplace', ...overrides });
    const github = [{ connector: 'github', name: 'GITHUB_TOKEN' }];
    const placeholder = /^lh_ph_[\da-f]{32}$/;

    it('exposes a placeholder and asks the egress proxy to inject the Market token', async () => {
      const options = {
        connectorModel: modelReturning([market()]) as any,
        networkAllow: ['api.github.com'],
      };

      const first = await resolveWidgetEnv({} as any, personal, github, options);
      const second = await resolveWidgetEnv({} as any, personal, github, options);

      expect(first.env.GITHUB_TOKEN).toMatch(placeholder);
      expect(second.env.GITHUB_TOKEN).not.toBe(first.env.GITHUB_TOKEN);
      expect(first.inject).toEqual([
        {
          header: 'Authorization',
          host: 'api.github.com',
          prefix: 'Bearer ',
          secretRef: { key: 'github' },
        },
      ]);
    });

    it('keeps a usable local secret over Market injection', async () => {
      const result = await resolveWidgetEnv({} as any, personal, github, {
        connectorModel: modelReturning([
          market({ credentials: { token: 'local-tok', type: 'bearer' } }),
        ]) as any,
        networkAllow: ['api.github.com'],
      });

      expect(result).toEqual({ env: { GITHUB_TOKEN: 'local-tok' }, inject: [] });
    });

    it('fails, even for an optional variable, when the inject host is not allowed', async () => {
      const error = await resolveWidgetEnv(
        {} as any,
        personal,
        [{ connector: 'github', name: 'GITHUB_TOKEN', required: false }],
        { connectorModel: modelReturning([market()]) as any, networkAllow: ['github.com'] },
      ).catch((e) => e);

      expect(error).toBeInstanceOf(MissingWidgetEnvError);
      expect(error.message).toBe(
        'Missing required environment: GITHUB_TOKEN (connector "github" is a LobeHub Market connector: its token is sent as the Authorization header to api.github.com; add that host to manifest.network.allow)',
      );
    });

    it('accepts an inject host covered by a wildcard allow entry', async () => {
      const { inject } = await resolveWidgetEnv({} as any, personal, github, {
        connectorModel: modelReturning([market()]) as any,
        networkAllow: ['*.github.com'],
      });

      expect(inject.map((i) => i.host)).toEqual(['api.github.com']);
    });

    it('does not treat a Composio row as a Market connector', async () => {
      const error = await resolveWidgetEnv(
        {} as any,
        personal,
        [{ connector: 'notion', name: 'NOTION_TOKEN' }],
        {
          connectorModel: modelReturning([
            market({
              identifier: 'notion',
              metadata: { composio: { appSlug: 'NOTION' } as any },
            }),
          ]) as any,
          networkAllow: ['api.notion.com'],
        },
      ).catch((e) => e);

      expect(error).toBeInstanceOf(MissingWidgetEnvError);
      expect(error.missing).toEqual([
        { connector: 'notion', name: 'NOTION_TOKEN', reason: 'connector_without_secret' },
      ]);
    });

    it('asks for an inject host when the provider has no default', async () => {
      const error = await resolveWidgetEnv(
        {} as any,
        personal,
        [{ connector: 'posthog', name: 'POSTHOG_TOKEN' }],
        { connectorModel: modelReturning([market({ identifier: 'posthog' })]) as any },
      ).catch((e) => e);

      expect(error.message).toBe(
        'Missing required environment: POSTHOG_TOKEN (connector "posthog" is a LobeHub Market connector with no default API host; set manifest.env[].inject.host to the API host the script calls)',
      );
    });

    it('routes the token by the manifest override, without a prefix for other headers', async () => {
      const { inject } = await resolveWidgetEnv(
        {} as any,
        personal,
        [
          {
            connector: 'posthog',
            inject: { header: 'X-Api-Key', host: ['us.posthog.com', '*.posthog.com'] },
            name: 'POSTHOG_TOKEN',
          },
        ],
        {
          connectorModel: modelReturning([market({ identifier: 'posthog' })]) as any,
          networkAllow: ['us.posthog.com', '*.posthog.com'],
        },
      );

      expect(inject).toEqual([
        { header: 'X-Api-Key', host: 'us.posthog.com', secretRef: { key: 'posthog' } },
        { header: 'X-Api-Key', host: '*.posthog.com', secretRef: { key: 'posthog' } },
      ]);
    });

    it('requires a wildcard inject host to be allowed verbatim', async () => {
      const error = await resolveWidgetEnv(
        {} as any,
        personal,
        [{ connector: 'github', inject: { host: '*.api.github.com' }, name: 'GITHUB_TOKEN' }],
        { connectorModel: modelReturning([market()]) as any, networkAllow: ['*.github.com'] },
      ).catch((e) => e);

      expect(error.missing[0]).toMatchObject({
        hosts: ['*.api.github.com'],
        reason: 'inject_host_not_allowed',
      });
    });

    it('injects one header per host when several variables share a connector', async () => {
      const { env, inject } = await resolveWidgetEnv(
        {} as any,
        personal,
        [
          { connector: 'github', name: 'GITHUB_TOKEN' },
          { connector: 'github', name: 'GH_TOKEN' },
        ],
        { connectorModel: modelReturning([market()]) as any, networkAllow: ['api.github.com'] },
      );

      expect(Object.keys(env)).toEqual(['GITHUB_TOKEN', 'GH_TOKEN']);
      expect(inject).toHaveLength(1);
    });

    it("still refuses a teammate's Market connector to a non-owner author", async () => {
      const error = await resolveWidgetEnv({} as any, workspace, github, {
        connectorModel: modelReturning([market({ userId: 'u2', workspaceId: 'ws1' })]) as any,
        isWorkspaceOwner: vi.fn().mockResolvedValue(false),
        networkAllow: ['api.github.com'],
      }).catch((e) => e);

      expect(error).toBeInstanceOf(ForbiddenWidgetCredentialsError);
    });
  });
});