Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Chore/improve graphql security #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -9,12 +9,12 @@ jobs:
with:
node-version: "12"
- uses: actions/checkout@v2
- uses: borales/actions-yarn@v2.0.0
- uses: actions/setup-node@v2-beta
with:
cmd: install
- uses: borales/actions-yarn@v2.0.0
- uses: actions/setup-node@v2-beta
with:
cmd: lint
- uses: borales/actions-yarn@v2.0.0
- uses: actions/setup-node@v2-beta
with:
cmd: test
63 changes: 59 additions & 4 deletions graphql/permissions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rule, allow, deny, or } from "nexus-plugin-shield";
import { rule, allow } from "nexus-plugin-shield";
import { getUserId } from "../utils/user";

const isAuthenticated = rule({ cache: "contextual" })(
@@ -11,6 +11,9 @@ const isAuthenticated = rule({ cache: "contextual" })(
const isAdmin = rule({ cache: "contextual" })(
async (parent, args, ctx: NexusContext, info) => {
const userId = getUserId(ctx.token);
if (!userId) {
return false;
}
const user = await ctx.db.user.findOne({
where: {
id: userId,
@@ -32,10 +35,62 @@ const rules = {
pages: allow,
blogPosts: allow,
},
Mutations: {
adminCreateOneBlogPost: isAdmin,
login: allow,
Mutation: {
"*": isAdmin,
#: allow,
login: allow,
logout: allow,
},
User: {
id: allow,
email: isAuthenticated,
roles: isAdmin,
blogPosts: isAuthenticated,
},
UserRole: {
id: isAuthenticated,
users: isAdmin,
},
BlogPost: {
id: allow,
title: allow,
content: allow,
author: allow,
authorId: allow,
published: isAdmin,
},
Page: {
id: allow,
published: isAdmin,
createdAt: isAdmin,
path: allow,
slug: allow,
sortKey: isAdmin,
parentPageId: isAdmin,
parentPage: isAdmin,
childPages: isAdmin,
navigationTitle: allow,
navigationTitle_en: isAdmin,
navigationTitle_de: isAdmin,
htmlTitle: allow,
htmlTitle_en: isAdmin,
htmlTitle_de: isAdmin,
content: allow,

meta_description: allow,
meta_description_en: isAdmin,
meta_description_de: isAdmin,

social_description: allow,
social_description_en: isAdmin,
social_description_de: isAdmin,

social_title: allow,
social_title_en: isAdmin,
social_title_de: isAdmin,
},
LoginResult: {
user: allow,
},
};

9 changes: 4 additions & 5 deletions graphql/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { schema, use, settings, server } from "nexus";
import { schema, use, settings } from "nexus";

import { prisma } from "nexus-plugin-prisma";
import { shield } from "nexus-plugin-shield";
import { deny, shield } from "nexus-plugin-shield";
import { auth } from "nexus-plugin-jwt-auth";

import { APP_SECRET } from "./utils/user";
@@ -22,9 +22,6 @@ schema.addToContext(({ req, res }) => {

settings.change({
server: {
graphql: {
introspection: true,
},
playground: {
settings: {
"request.credentials": "include",
@@ -54,6 +51,8 @@ use(
rules,
options: {
allowExternalErrors: true,
fallbackRule: deny,
debug: true,
},
})
);
16 changes: 5 additions & 11 deletions src/modules/layout/components/stories/MainMenu.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import React from "react"
import React from "react";
// import { action } from "@storybook/addon-actions";
import MainMenu from "../MainMenu"


import MainMenu from "../MainMenu";

export default {
component: MainMenu,
title: 'layout/MainMenu',
title: "layout/MainMenu",
};

export const defaultView = () => (
<MainMenu />
)



export const defaultView = () => <MainMenu />;