-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(server): protecting against scope elevation in PAT creation (#1901)
* test DX improvements + tests for token:write scope * protecting against scope elevation
- Loading branch information
Showing
9 changed files
with
383 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,3 @@ | ||
export const canCreateToken = (userScopes: string[], tokenScopes: string[]) => { | ||
return tokenScopes.every((scope) => userScopes.includes(scope)) | ||
} |
This file contains 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,124 @@ | ||
import { BasicTestUser, createTestUsers } from '@/test/authHelper' | ||
import { | ||
CreateTokenDocument, | ||
RevokeTokenDocument | ||
} from '@/test/graphql/generated/graphql' | ||
import { | ||
TestApolloServer, | ||
createTestContext, | ||
testApolloServer | ||
} from '@/test/graphqlHelper' | ||
import { beforeEachContext } from '@/test/hooks' | ||
import { AllScopes, Roles, Scopes } from '@speckle/shared' | ||
import { expect } from 'chai' | ||
import { difference } from 'lodash' | ||
|
||
/** | ||
* Older API token test cases can be found in `graph.spec.js` | ||
*/ | ||
describe('API Tokens', () => { | ||
const userOne: BasicTestUser = { | ||
name: 'Dimitrie Stefanescu', | ||
email: '[email protected]', | ||
password: 'sn3aky-1337-b1m', | ||
id: '' | ||
} | ||
|
||
let apollo: TestApolloServer | ||
|
||
before(async () => { | ||
await beforeEachContext() | ||
await createTestUsers([userOne]) | ||
|
||
apollo = await testApolloServer({ | ||
context: createTestContext({ | ||
auth: true, | ||
userId: userOne.id, | ||
role: Roles.Server.Admin, | ||
token: 'asd', | ||
scopes: AllScopes | ||
}) | ||
}) | ||
}) | ||
|
||
it("can't create PATs with scopes that the authenticated req itself doesn't have", async () => { | ||
const { data, errors } = await apollo.execute( | ||
CreateTokenDocument, | ||
{ | ||
token: { | ||
name: 'invalidone', | ||
scopes: [Scopes.Profile.Read, Scopes.Streams.Read] | ||
} | ||
}, | ||
{ | ||
context: { | ||
scopes: [Scopes.Profile.Read, Scopes.Tokens.Write] | ||
} | ||
} | ||
) | ||
|
||
expect(data?.apiTokenCreate).to.not.be.ok | ||
expect(errors).to.be.ok | ||
expect( | ||
errors!.find((e) => | ||
e.message.includes("You can't create a token with scopes that you don't have") | ||
) | ||
).to.be.ok | ||
}) | ||
|
||
describe('without the tokens:write scope', () => { | ||
const limitedTokenScopes = difference(AllScopes, [Scopes.Tokens.Write]) | ||
let limitedToken: string | ||
|
||
before(async () => { | ||
const res = await apollo.execute(CreateTokenDocument, { | ||
token: { name: 'limited', scopes: limitedTokenScopes } | ||
}) | ||
|
||
limitedToken = res.data?.apiTokenCreate || '' | ||
if (!limitedToken.length) { | ||
throw new Error("Couldn't prepare token for test") | ||
} | ||
}) | ||
|
||
it("can't create PAT tokens", async () => { | ||
const { data, errors } = await apollo.execute( | ||
CreateTokenDocument, | ||
{ | ||
token: { name: 'invalidone', scopes: [Scopes.Profile.Read] } | ||
}, | ||
{ | ||
context: { | ||
scopes: limitedTokenScopes, | ||
token: limitedToken | ||
} | ||
} | ||
) | ||
|
||
expect(data?.apiTokenCreate).to.not.be.ok | ||
expect(errors).to.be.ok | ||
expect( | ||
errors!.find((e) => e.message.includes('do not have the required privileges')) | ||
).to.be.ok | ||
}) | ||
|
||
it("can't delete PAT tokens", async () => { | ||
const { data, errors } = await apollo.execute( | ||
RevokeTokenDocument, | ||
{ token: limitedToken }, | ||
{ | ||
context: { | ||
scopes: limitedTokenScopes, | ||
token: limitedToken | ||
} | ||
} | ||
) | ||
|
||
expect(data?.apiTokenRevoke).to.not.be.ok | ||
expect(errors).to.be.ok | ||
expect( | ||
errors!.find((e) => e.message.includes('do not have the required privileges')) | ||
).to.be.ok | ||
}) | ||
}) | ||
}) |
This file contains 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 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,13 @@ | ||
import { gql } from 'apollo-server-express' | ||
|
||
export const createTokenMutation = gql` | ||
mutation CreateToken($token: ApiTokenCreateInput!) { | ||
apiTokenCreate(token: $token) | ||
} | ||
` | ||
|
||
export const revokeTokenMutation = gql` | ||
mutation RevokeToken($token: String!) { | ||
apiTokenRevoke(token: $token) | ||
} | ||
` |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.