-
Notifications
You must be signed in to change notification settings - Fork 22
fix(auth): require explicit GraphQL authorization #2074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Eli Bosley (elibosley)
merged 3 commits into
main
from
fix/graphql-authorization-default-deny
Sep 5, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import tseslint from 'typescript-eslint'; | ||
|
|
||
| import requireGraphqlAuthorization from './require-graphql-authorization.mjs'; | ||
|
|
||
| export const graphqlAuthorizationConfig = [ | ||
| { | ||
| files: ['**/*.ts'], | ||
| // Tests include deliberately unguarded fixtures; templates are not deployed handlers. | ||
| ignores: [ | ||
| '**/*.spec.ts', | ||
| '**/*.test.ts', | ||
| '**/__test__/**', | ||
| '**/__tests__/**', | ||
| '**/templates/**', | ||
| ], | ||
| languageOptions: { parser: tseslint.parser }, | ||
| plugins: { | ||
| 'unraid-auth': { rules: { 'require-graphql-authorization': requireGraphqlAuthorization } }, | ||
| }, | ||
| rules: { 'unraid-auth/require-graphql-authorization': 'error' }, | ||
| }, | ||
| ]; | ||
|
|
||
| // This focused pass does not evaluate the existing style-rule suppressions. | ||
| export default [ | ||
| ...graphqlAuthorizationConfig, | ||
| { | ||
| plugins: { '@typescript-eslint': tseslint.plugin }, | ||
| linterOptions: { reportUnusedDisableDirectives: 'off' }, | ||
| }, | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| const graphqlHandlers = new Set(['Query', 'Mutation', 'Subscription', 'ResolveField']); | ||
| const permissionSources = new Set(['@unraid/shared/use-permissions.directive.js', 'nest-authz']); | ||
| const accessMarkers = new Map([ | ||
| ['@app/unraid-api/auth/public.decorator.js', 'Public'], | ||
| ['@app/unraid-api/auth/authenticated.decorator.js', 'Authenticated'], | ||
| ]); | ||
|
|
||
| function importedDecorator(context, decorator) { | ||
| const call = decorator.expression; | ||
| if (call.type !== 'CallExpression') return; | ||
| const callee = call.callee; | ||
| const identifier = callee.type === 'Identifier' ? callee : callee.object; | ||
| if (identifier?.type !== 'Identifier') return; | ||
| let scope = context.sourceCode.getScope(decorator); | ||
| while (scope) { | ||
| const variable = scope.set.get(identifier.name); | ||
| if (variable) { | ||
| const definition = variable.defs.find((entry) => entry.type === 'ImportBinding'); | ||
| if (!definition) return; | ||
| const specifier = definition.node; | ||
| const source = definition.parent.source.value; | ||
| if (callee.type === 'Identifier' && specifier.type === 'ImportSpecifier') { | ||
| return { source, name: specifier.imported.name ?? specifier.imported.value, call }; | ||
| } | ||
| if (callee.type === 'MemberExpression' && specifier.type === 'ImportNamespaceSpecifier') { | ||
| const name = callee.computed ? callee.property.value : callee.property.name; | ||
| return { source, name, call }; | ||
| } | ||
| return; | ||
| } | ||
| scope = scope.upper; | ||
| } | ||
| } | ||
|
|
||
| function hasPermissionArgument(call) { | ||
| return ( | ||
| call.arguments.length > 0 && | ||
| call.arguments.every((argument) => { | ||
| if (argument.type === 'ObjectExpression') { | ||
| const keys = new Set( | ||
| argument.properties | ||
| .filter((property) => property.type === 'Property') | ||
| .map((property) => property.key.name ?? property.key.value) | ||
| ); | ||
| return keys.has('action') && keys.has('resource'); | ||
| } | ||
| return !['Literal', 'ArrayExpression'].includes(argument.type); | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| export default { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { description: 'Require an explicit access policy on each GraphQL handler' }, | ||
| schema: [], | ||
| messages: { | ||
| missing: | ||
| 'GraphQL handlers require @UsePermissions(...), @Public(), or @Authenticated() on the method. @UseGuards alone does not declare permissions.', | ||
| empty: '@UsePermissions must declare an action and resource; empty permission metadata is denied at runtime.', | ||
| }, | ||
| }, | ||
| create(context) { | ||
| return { | ||
| MethodDefinition(node) { | ||
| const decorators = (node.decorators ?? []) | ||
| .map((decorator) => importedDecorator(context, decorator)) | ||
| .filter(Boolean); | ||
| if ( | ||
| !decorators.some( | ||
| ({ source, name }) => source === '@nestjs/graphql' && graphqlHandlers.has(name) | ||
| ) | ||
| ) | ||
| return; | ||
| const permissions = decorators.filter( | ||
| ({ source, name }) => permissionSources.has(source) && name === 'UsePermissions' | ||
| ); | ||
| if (permissions.length) { | ||
| if (permissions.some(({ call }) => !hasPermissionArgument(call))) { | ||
| context.report({ node, messageId: 'empty' }); | ||
| } | ||
| return; | ||
| } | ||
| if ( | ||
| !decorators.some( | ||
| ({ source, name }) => | ||
| accessMarkers.has(source) && accessMarkers.get(source) === name | ||
| ) | ||
| ) { | ||
| context.report({ node, messageId: 'missing' }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| import { ESLint, RuleTester } from 'eslint'; | ||
| import tseslint from 'typescript-eslint'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import rule from './require-graphql-authorization.mjs'; | ||
|
|
||
| RuleTester.describe = describe; | ||
| RuleTester.it = it; | ||
| const tester = new RuleTester({ languageOptions: { parser: tseslint.parser } }); | ||
| const handlerImports = "import { Query, Mutation, Subscription, ResolveField } from '@nestjs/graphql';"; | ||
| const permissionsImport = | ||
| "import { UsePermissions } from '@unraid/shared/use-permissions.directive.js';"; | ||
| const publicImport = "import { Public } from '@app/unraid-api/auth/public.decorator.js';"; | ||
| const authenticatedImport = | ||
| "import { Authenticated } from '@app/unraid-api/auth/authenticated.decorator.js';"; | ||
| const permission = "@UsePermissions({ action: 'READ_ANY', resource: 'CONFIG' })"; | ||
|
|
||
| const code = (decorators: string, imports = permissionsImport) => `${handlerImports} ${imports} | ||
| class Resolver { ${decorators} arbitraryName() { return true; } }`; | ||
|
|
||
| tester.run('require-graphql-authorization', rule, { | ||
| valid: [ | ||
| ...['Query', 'Mutation', 'Subscription', 'ResolveField'].map((name) => | ||
| code(`@${name}(() => Boolean) ${permission}`) | ||
| ), | ||
| code(`${permission} @Query(() => Boolean)`), | ||
| code('@Query(() => Boolean) @Public()', publicImport), | ||
| code('@Mutation(() => Boolean) @Authenticated()', authenticatedImport), | ||
| code('@Query(() => Boolean) @UsePermissions(permission)', permissionsImport), | ||
| code(`@Query(() => Boolean) ${permission}`, "import { UsePermissions } from 'nest-authz';"), | ||
| "import { Query as Read } from '@nestjs/graphql'; import { UsePermissions as Policy } from 'nest-authz'; class R { @Read(() => Boolean) @Policy({ action: 'READ_ANY', resource: 'CONFIG' }) value() {} }", | ||
| "import * as gql from '@nestjs/graphql'; import * as auth from 'nest-authz'; class R { @gql.Query(() => Boolean) @auth.UsePermissions({ action: 'READ_ANY', resource: 'CONFIG' }) value() {} }", | ||
| "import * as gql from '@nestjs/graphql'; import * as auth from 'nest-authz'; class R { @(gql['Query'])(() => Boolean) @(auth['UsePermissions'])({ action: 'READ_ANY', resource: 'CONFIG' }) value() {} }", | ||
| code(''), | ||
| "import { Query } from 'unrelated'; class R { @Query() value() {} }", | ||
| ], | ||
| invalid: [ | ||
| ...['Query', 'Mutation', 'Subscription', 'ResolveField'].map((name) => ({ | ||
| code: code(`@${name}(() => Boolean)`), | ||
| errors: [{ messageId: 'missing' }], | ||
| })), | ||
| { | ||
| code: "import { Query as Read } from '@nestjs/graphql'; class R { @Read(() => Boolean) renamed() {} }", | ||
| errors: [{ messageId: 'missing' }], | ||
| }, | ||
| { | ||
| code: "import * as gql from '@nestjs/graphql'; class R { @gql.Mutation(() => Boolean) renamed() {} }", | ||
| errors: [{ messageId: 'missing' }], | ||
| }, | ||
| { | ||
| code: code('@Query(() => Boolean) @UseGuards(AuthZGuard)'), | ||
| errors: [{ messageId: 'missing' }], | ||
| }, | ||
| { | ||
| code: code('@Query(() => Boolean) @Public()', "import { Public } from 'unrelated';"), | ||
| errors: [{ messageId: 'missing' }], | ||
| }, | ||
| { | ||
| code: `${handlerImports} ${publicImport} @Public() class R { @Query(() => Boolean) value() {} }`, | ||
| errors: [{ messageId: 'missing' }], | ||
| }, | ||
| { | ||
| code: `${handlerImports} ${permissionsImport} function factory(UsePermissions) { return class { @Query(() => Boolean) ${permission} value() {} }; }`, | ||
| errors: [{ messageId: 'missing' }], | ||
| }, | ||
| ...['', '{}', '[]', 'null', "{ action: 'READ_ANY' }"].map((args) => ({ | ||
| code: code(`@Query(() => Boolean) @UsePermissions(${args})`), | ||
| errors: [{ messageId: 'empty' }], | ||
| })), | ||
| { | ||
| code: code( | ||
| '@Query(() => Boolean) @Public() @UsePermissions()', | ||
| `${publicImport} ${permissionsImport}` | ||
| ), | ||
| errors: [{ messageId: 'empty' }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // Loading the full ESLint config includes TypeScript transpilation and plugin startup. | ||
| // Allow for that cold start while CI runs coverage across packages concurrently. | ||
| describe('authorization lint configuration', { timeout: 30_000 }, () => { | ||
| const cwd = fileURLToPath(new URL('..', import.meta.url)); | ||
| it.each([ | ||
| ['.eslintrc.ts', 'src/new-handler.ts'], | ||
| ['eslint/graphql-authorization.config.mjs', 'api/src/new-handler.ts'], | ||
| [ | ||
| 'eslint/graphql-authorization.config.mjs', | ||
| 'packages/unraid-api-plugin-connect/src/new-handler.ts', | ||
| ], | ||
| ])('rejects an unguarded endpoint using %s at %s', async (config, filePath) => { | ||
| const eslint = new ESLint({ | ||
| cwd: config === '.eslintrc.ts' ? cwd : fileURLToPath(new URL('../..', import.meta.url)), | ||
| overrideConfigFile: fileURLToPath(new URL(`../${config}`, import.meta.url)), | ||
| }); | ||
| const results = await eslint.lintText(code('@Mutation(() => Boolean)'), { filePath }); | ||
| expect( | ||
| results | ||
| .flatMap((result) => result.messages) | ||
| .filter((message) => message.ruleId === 'unraid-auth/require-graphql-authorization') | ||
| ).toHaveLength(1); | ||
| }); | ||
| it('keeps deliberate test fixtures outside the production policy', async () => { | ||
| const eslint = new ESLint({ cwd, overrideConfigFile: '.eslintrc.ts' }); | ||
| const results = await eslint.lintText(code('@Query(() => Boolean)'), { | ||
| filePath: 'src/negative-fixture.spec.ts', | ||
| }); | ||
| expect( | ||
| results | ||
| .flatMap((result) => result.messages) | ||
| .filter((message) => message.ruleId === 'unraid-auth/require-graphql-authorization') | ||
| ).toHaveLength(0); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { SetMetadata } from '@nestjs/common'; | ||
|
|
||
| export const IS_AUTHENTICATED_ENDPOINT_KEY = 'isAuthenticatedEndpoint'; | ||
|
|
||
| // For empty mutation namespaces; their child handlers enforce resource permissions. | ||
| export const Authenticated = (): MethodDecorator => SetMetadata(IS_AUTHENTICATED_ENDPOINT_KEY, true); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Reject explicit empty permission values.
Line 45 accepts
@UsePermissions({ action: undefined, resource: undefined })because both keys exist. The rule then reports no error for unusable permission metadata. Require non-empty values for both properties, while still allowing enum expressions. Add invalid fixtures forundefined,null, and empty-string values.🤖 Prompt for AI Agents