Skip to main content
interlace
Plugin: openai-securityRules

no-hardcoded-api-key

Forbid a literal API key in the OpenAI client options

Forbid a literal API key in the OpenAI client options.

Why

A key written into source is committed, pushed, mirrored into every clone and CI cache, and is billable by anyone who reads it. Because rotating it means a code change, leaked keys tend to stay live far longer than they should.

The rule is gated on the OpenAI SDK being imported, so it stays silent in files that do not construct the client. openai-edge is a different package with a different client and does not open the gate. An empty string is treated as a placeholder, not a credential, and a spread ({ ...base }) makes the options unreadable rather than guessed at.

Nothing in eslint-plugin-secure-coding reports this shape โ€” measured, not assumed. The generic credential rules look for password-shaped names and connection strings; an SDK's client options are a different shape.

Incorrect

import OpenAI from 'openai';
const client = new OpenAI({ apiKey: 'sk-proj-...' });

Correct

import OpenAI from 'openai';
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

Did this rule catch something? Star the repo to get new CWE coverage as we ship it โ€” or follow the AI-code-security benchmarks behind these rules.