|
| 1 | +import { fileURLToPath } from 'node:url'; |
| 2 | + |
| 3 | +import { ESLint, RuleTester } from 'eslint'; |
| 4 | +import tseslint from 'typescript-eslint'; |
| 5 | +import { describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +import rule from './require-graphql-authorization.mjs'; |
| 8 | + |
| 9 | +RuleTester.describe = describe; |
| 10 | +RuleTester.it = it; |
| 11 | +const tester = new RuleTester({ languageOptions: { parser: tseslint.parser } }); |
| 12 | +const handlerImports = "import { Query, Mutation, Subscription, ResolveField } from '@nestjs/graphql';"; |
| 13 | +const permissionsImport = |
| 14 | + "import { UsePermissions } from '@unraid/shared/use-permissions.directive.js';"; |
| 15 | +const publicImport = "import { Public } from '@app/unraid-api/auth/public.decorator.js';"; |
| 16 | +const authenticatedImport = |
| 17 | + "import { Authenticated } from '@app/unraid-api/auth/authenticated.decorator.js';"; |
| 18 | +const permission = "@UsePermissions({ action: 'READ_ANY', resource: 'CONFIG' })"; |
| 19 | + |
| 20 | +const code = (decorators: string, imports = permissionsImport) => `${handlerImports} ${imports} |
| 21 | +class Resolver { ${decorators} arbitraryName() { return true; } }`; |
| 22 | + |
| 23 | +tester.run('require-graphql-authorization', rule, { |
| 24 | + valid: [ |
| 25 | + ...['Query', 'Mutation', 'Subscription', 'ResolveField'].map((name) => |
| 26 | + code(`@${name}(() => Boolean) ${permission}`) |
| 27 | + ), |
| 28 | + code(`${permission} @Query(() => Boolean)`), |
| 29 | + code('@Query(() => Boolean) @Public()', publicImport), |
| 30 | + code('@Mutation(() => Boolean) @Authenticated()', authenticatedImport), |
| 31 | + code('@Query(() => Boolean) @UsePermissions(permission)', permissionsImport), |
| 32 | + code(`@Query(() => Boolean) ${permission}`, "import { UsePermissions } from 'nest-authz';"), |
| 33 | + "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() {} }", |
| 34 | + "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() {} }", |
| 35 | + "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() {} }", |
| 36 | + code(''), |
| 37 | + "import { Query } from 'unrelated'; class R { @Query() value() {} }", |
| 38 | + ], |
| 39 | + invalid: [ |
| 40 | + ...['Query', 'Mutation', 'Subscription', 'ResolveField'].map((name) => ({ |
| 41 | + code: code(`@${name}(() => Boolean)`), |
| 42 | + errors: [{ messageId: 'missing' }], |
| 43 | + })), |
| 44 | + { |
| 45 | + code: "import { Query as Read } from '@nestjs/graphql'; class R { @Read(() => Boolean) renamed() {} }", |
| 46 | + errors: [{ messageId: 'missing' }], |
| 47 | + }, |
| 48 | + { |
| 49 | + code: "import * as gql from '@nestjs/graphql'; class R { @gql.Mutation(() => Boolean) renamed() {} }", |
| 50 | + errors: [{ messageId: 'missing' }], |
| 51 | + }, |
| 52 | + { |
| 53 | + code: code('@Query(() => Boolean) @UseGuards(AuthZGuard)'), |
| 54 | + errors: [{ messageId: 'missing' }], |
| 55 | + }, |
| 56 | + { |
| 57 | + code: code('@Query(() => Boolean) @Public()', "import { Public } from 'unrelated';"), |
| 58 | + errors: [{ messageId: 'missing' }], |
| 59 | + }, |
| 60 | + { |
| 61 | + code: `${handlerImports} ${publicImport} @Public() class R { @Query(() => Boolean) value() {} }`, |
| 62 | + errors: [{ messageId: 'missing' }], |
| 63 | + }, |
| 64 | + { |
| 65 | + code: `${handlerImports} ${permissionsImport} function factory(UsePermissions) { return class { @Query(() => Boolean) ${permission} value() {} }; }`, |
| 66 | + errors: [{ messageId: 'missing' }], |
| 67 | + }, |
| 68 | + ...['', '{}', '[]', 'null', "{ action: 'READ_ANY' }"].map((args) => ({ |
| 69 | + code: code(`@Query(() => Boolean) @UsePermissions(${args})`), |
| 70 | + errors: [{ messageId: 'empty' }], |
| 71 | + })), |
| 72 | + { |
| 73 | + code: code( |
| 74 | + '@Query(() => Boolean) @Public() @UsePermissions()', |
| 75 | + `${publicImport} ${permissionsImport}` |
| 76 | + ), |
| 77 | + errors: [{ messageId: 'empty' }], |
| 78 | + }, |
| 79 | + ], |
| 80 | +}); |
| 81 | + |
| 82 | +// Loading the full ESLint config includes TypeScript transpilation and plugin startup. |
| 83 | +// Allow for that cold start while CI runs coverage across packages concurrently. |
| 84 | +describe('authorization lint configuration', { timeout: 30_000 }, () => { |
| 85 | + const cwd = fileURLToPath(new URL('..', import.meta.url)); |
| 86 | + it.each([ |
| 87 | + ['.eslintrc.ts', 'src/new-handler.ts'], |
| 88 | + ['eslint/graphql-authorization.config.mjs', 'api/src/new-handler.ts'], |
| 89 | + [ |
| 90 | + 'eslint/graphql-authorization.config.mjs', |
| 91 | + 'packages/unraid-api-plugin-connect/src/new-handler.ts', |
| 92 | + ], |
| 93 | + ])('rejects an unguarded endpoint using %s at %s', async (config, filePath) => { |
| 94 | + const eslint = new ESLint({ |
| 95 | + cwd: config === '.eslintrc.ts' ? cwd : fileURLToPath(new URL('../..', import.meta.url)), |
| 96 | + overrideConfigFile: fileURLToPath(new URL(`../${config}`, import.meta.url)), |
| 97 | + }); |
| 98 | + const results = await eslint.lintText(code('@Mutation(() => Boolean)'), { filePath }); |
| 99 | + expect( |
| 100 | + results |
| 101 | + .flatMap((result) => result.messages) |
| 102 | + .filter((message) => message.ruleId === 'unraid-auth/require-graphql-authorization') |
| 103 | + ).toHaveLength(1); |
| 104 | + }); |
| 105 | + it('keeps deliberate test fixtures outside the production policy', async () => { |
| 106 | + const eslint = new ESLint({ cwd, overrideConfigFile: '.eslintrc.ts' }); |
| 107 | + const results = await eslint.lintText(code('@Query(() => Boolean)'), { |
| 108 | + filePath: 'src/negative-fixture.spec.ts', |
| 109 | + }); |
| 110 | + expect( |
| 111 | + results |
| 112 | + .flatMap((result) => result.messages) |
| 113 | + .filter((message) => message.ruleId === 'unraid-auth/require-graphql-authorization') |
| 114 | + ).toHaveLength(0); |
| 115 | + }); |
| 116 | +}); |
0 commit comments