no-identity-from-metadata
Do not authorize a gRPC caller by an identity read from metadata
Next step
no-identity-from-metadata ships in eslint-plugin-grpc-security, with 4 of its 4 rules in the recommended preset.
npm install -D eslint-plugin-grpc-securityimport { configs } from 'eslint-plugin-grpc-security';
export default [configs.recommended];
Do not authorize a gRPC caller by an identity read from metadata.
- CWE: CWE-290 — Authentication Bypass by Spoofing
- OWASP: A07:2021 — Identification and Authentication Failures
- CVSS: 8.1 (High)
- Recommended:
error
Why
gRPC metadata is HTTP/2 headers, and the client sets every one of them. Services behind an API gateway or a service mesh often read "who is calling" from a header the gateway is supposed to set, such as x-authenticated-user. That holds only while every request comes through the gateway. Once the backend is reachable another way (a debug port, an SSRF, a misrouted path, a sidecar that forwards client headers), anyone can send the header and become anyone.
Rule details
The rule decides from the flow, not from the header's name. It reports a metadata read when all three hold:
- The value comes from call metadata. That means
.get(…)on themetadataof a gRPC handler's first parameter. A handler is a method of the object passed to.addService(service, implementation), directly or through aconst. The rule follows{ metadata }destructuring, aconstholding the metadata, and same-file helpers whose call sites pass it (callerOf(call.metadata), an IIFE). - The value reaches the comparison unchanged in substance. It can pass through consts (including
const [h] = md.get(…)),[0]and?.[0],String(…), templates, string methods,??and||, ternaries,await, casts, and the return value of same-file functions.- A lookup keyed by the value (
TOKENS[h]) or a call that takes it (verify(h)) resolves it into something else, and the flow stops there.
- A lookup keyed by the value (
- The value is used as an identity. It sits on one side of
===,!==,==or!=inside the test of anifor a ternary, and the other side is a field of a per-request record (acc.owner,(await db.find(id)).owner).- A comparison with
process.env.Xor a field of a module-level constant is a credential check, not an identity, and stays silent.
- A comparison with
Incorrect
function callerOf(metadata) {
const forwarded = metadata.get('x-authenticated-user')[0];
if (forwarded) return String(forwarded);
// ...
}
server.addService(pkg.bank.Accounts.service, {
GetAccount(call, cb) {
const user = callerOf(call.metadata);
const acc = ACCOUNTS[call.request.account_id];
if (acc.owner !== user) return cb({ code: grpc.status.PERMISSION_DENIED });
cb(null, acc);
},
});Correct
function callerOf(metadata) {
const auth = String(metadata.get('authorization')[0] ?? '');
return TOKENS[auth.replace(/^Bearer\s+/i, '')] ?? null;
}Here the caller is whatever a server-side table says the presented token belongs to. The header is a key into that table, not the identity itself. A signed token verified in the backend, or the peer certificate of an mTLS connection, works the same way.
Limits
- Only equality checks against a record field count as authorization. An identity used as a lookup key (
DATA_BY_USER[user]) or compared against a string literal (user === 'admin') is not reported. - Flows that leave the file, or pass through a class method, are not followed.
Further reading
Use this rule
npm install -D eslint-plugin-grpc-securityimport grpcSecurity from 'eslint-plugin-grpc-security';export default [ { plugins: { 'grpc-security': grpcSecurity }, rules: { 'grpc-security/no-identity-from-metadata': 'error' }, },];npm install -D oxlint-plugin-grpc-security{ "jsPlugins": ["oxlint-plugin-grpc-security"], "rules": { "grpc-security/no-identity-from-metadata": "error" }}import { defineConfig } from 'vite-plus';export default defineConfig({ lint: { jsPlugins: ['oxlint-plugin-grpc-security'], rules: { 'grpc-security/no-identity-from-metadata': 'error', }, },});oxlint-plugin-grpc-security re-exports eslint-plugin-grpc-security/oxlint: the same rules under the same IDs, and no ESLint install. Why oxlint.
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.