Skip to content

Commit b8a9560

Browse files
authored
Merge pull request from GHSA-59r9-6jp6-jcm7
This change fixes an XSS vulnerability that has been present since the first commit of GraphQL Playground when used with an untrusted GraphQL server. It applies three strategies to fix via defense-in-depth: HTML escaping a string in a context that uses innerHTML; validating incoming schemas with graphql-js validateSchema; and using a single up-to-date Markdown library. See docs/security/2021-introspection-schema-xss.md for more details.
1 parent 5073062 commit b8a9560

File tree

10 files changed

+354
-212
lines changed

10 files changed

+354
-212
lines changed

Diff for: SECURITY.md

+2-136
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,4 @@
11
# Known Vulnerabilities
22

3-
## XSS Reflection Vulnerability
4-
5-
the origin of the vulnerability is in `renderPlaygroundPage`, found in `graphql-playground-html`
6-
7-
### Impact
8-
9-
When using
10-
11-
- `renderPlaygroundPage()`,
12-
- `koaPlayground()`
13-
- `expressPlayground()`
14-
- `koaPlayground()`
15-
- `lambdaPlayground()`
16-
- any downstream dependent packages that use these functions
17-
18-
without sanitization of user input, your application is vulnerable to an XSS Reflecton Attack. This is a serious vulnerability that could allow for exfiltration of data or user credentials, or to disrupt systems.
19-
20-
We've provided ['an example of the xss using the express middleware]('https://github.com/prisma-labs/graphql-playground/tree/main/packages/graphql-playground-middleware-express/examples/xss-attack')
21-
22-
### Impacted Packages
23-
24-
**All versions of these packages are impacted until those specified below**, which are now safe for user defined input:
25-
26-
- `graphql-playground-html`: **☔ safe** @ `1.6.22`
27-
- `graphql-playground-express` **☔ safe** @ `1.7.16`
28-
- `graphql-playground-koa` **☔ safe** @ `1.6.15`
29-
- `graphql-playground-hapi` **☔ safe** @ `1.6.13`
30-
- `graphql-playground-lambda` **☔ safe** @ `1.7.17`
31-
32-
### Static input was always safe
33-
34-
These examples are safe for _all versions_ **because input is static**
35-
36-
with `express` and `renderPlaygroundPage`:
37-
38-
```js
39-
app.get('/playground', (req) => {
40-
res.html(
41-
renderPlaygroundPage({
42-
endpoint: `/our/graphql`,
43-
}),
44-
)
45-
next()
46-
})
47-
```
48-
49-
with `expressPlayground`:
50-
51-
```js
52-
// params
53-
app.get('/playground', (req) =>
54-
expressPlayground({
55-
endpoint: `/our/graphql`,
56-
settings: { 'editor.theme': req.query.darkMode ? 'dark' : 'light' },
57-
}),
58-
)
59-
```
60-
61-
with `koaPlayground`:
62-
63-
```js
64-
const koa = require('koa')
65-
const koaRouter = require('koa-router')
66-
const koaPlayground = require('graphql-playground-middleware-koa')
67-
68-
const app = new koa()
69-
const router = new koaRouter()
70-
71-
router.all('/playground', koaPlayground({ endpoint: '/graphql' }))
72-
```
73-
74-
### Vulnerable Examples
75-
76-
Here are some examples where the vulnerability would be present before the patch, because of unfiltered user input
77-
78-
```js
79-
const express = require('express')
80-
const expressPlayground = require('graphql-playground-middleware-express')
81-
.default
82-
83-
const app = express()
84-
85-
app.use(express.json())
86-
87-
// params
88-
app.get('/playground/:id', (req) =>
89-
expressPlayground({
90-
endpoint: `/our/graphql/${req.params.id}`,
91-
}),
92-
)
93-
94-
// params
95-
app.get('/playground', (req) =>
96-
expressPlayground({
97-
endpoint: `/our/graphql`,
98-
// any settings that are unsanitized user input, not just `endpoint`
99-
settings: { 'editor.fontFamily': req.query.font },
100-
}),
101-
)
102-
```
103-
104-
[See a proof of concept](packages/graphql-playground-html/examples/xss-attack) to understand the vulnerability better
105-
106-
### Workaround
107-
108-
To fix this issue without the update, you can sanitize however you want.
109-
110-
We suggest using [`xss`](https://www.npmjs.com/package/xss) (what we use for our own fix)
111-
112-
For example, with `graphql-playground-middleware-express`:
113-
114-
```js
115-
const express = require('express')
116-
const { filterXSS } = require('xss')
117-
const expressPlayground = require('graphql-playground-middleware-express')
118-
.default
119-
120-
121-
const app = express()
122-
123-
const filter = (val) => filterXSS(val, {
124-
whitelist: [],
125-
stripIgnoreTag: true,
126-
stripIgnoreTagBody: ['script']
127-
})
128-
129-
// simple example
130-
app.get('/playground/:id', (req) =>
131-
expressPlayground({ endpoint: `/graphql/${filter(req.params.id)}` })
132-
133-
// advanced params
134-
app.get('/playground', (req) =>
135-
expressPlayground(JSON.parse(filter(JSON.stringify(req.query))))
136-
```
137-
138-
[See a proof of concept workaround](packages/graphql-playground-html/examples/xss-attack), example #3
3+
- [2021: Introspection Schema Phishing Attack XSS Vulnerability](docs/security/2021-schema-xss-phishing-attack.md)
4+
- [2020: XSS Reflection Vulnerability](docs/security/2020-xss-template-injection.md)

Diff for: docs/security/2020-xss-template-injection.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
## XSS Reflection Vulnerability
2+
3+
the origin of the vulnerability is in `renderPlaygroundPage`, found in `graphql-playground-html`
4+
5+
### Impact
6+
7+
When using
8+
9+
- `renderPlaygroundPage()`,
10+
- `koaPlayground()`
11+
- `expressPlayground()`
12+
- `koaPlayground()`
13+
- `lambdaPlayground()`
14+
- any downstream dependent packages that use these functions
15+
16+
without sanitization of user input, your application is vulnerable to an XSS Reflection Attack. This is a serious vulnerability that could allow for exfiltration of data or user credentials, or to disrupt systems.
17+
18+
We've provided ['an example of the xss using the express middleware]('https://github.com/prisma-labs/graphql-playground/tree/main/packages/graphql-playground-middleware-express/examples/xss-attack')
19+
20+
### Impacted Packages
21+
22+
**All versions of these packages are impacted until those specified below**, which are now safe for user defined input:
23+
24+
- `graphql-playground-html`: **☔ safe** @ `1.6.22`
25+
- `graphql-playground-express` **☔ safe** @ `1.7.16`
26+
- `graphql-playground-koa` **☔ safe** @ `1.6.15`
27+
- `graphql-playground-hapi` **☔ safe** @ `1.6.13`
28+
- `graphql-playground-lambda` **☔ safe** @ `1.7.17`
29+
30+
### Static input was always safe
31+
32+
These examples are safe for _all versions_ **because input is static**
33+
34+
with `express` and `renderPlaygroundPage`:
35+
36+
```js
37+
app.get('/playground', (req) => {
38+
res.html(
39+
renderPlaygroundPage({
40+
endpoint: `/our/graphql`,
41+
}),
42+
)
43+
next()
44+
})
45+
```
46+
47+
with `expressPlayground`:
48+
49+
```js
50+
// params
51+
app.get('/playground', (req) =>
52+
expressPlayground({
53+
endpoint: `/our/graphql`,
54+
settings: { 'editor.theme': req.query.darkMode ? 'dark' : 'light' },
55+
}),
56+
)
57+
```
58+
59+
with `koaPlayground`:
60+
61+
```js
62+
const koa = require('koa')
63+
const koaRouter = require('koa-router')
64+
const koaPlayground = require('graphql-playground-middleware-koa')
65+
66+
const app = new koa()
67+
const router = new koaRouter()
68+
69+
router.all('/playground', koaPlayground({ endpoint: '/graphql' }))
70+
```
71+
72+
### Vulnerable Examples
73+
74+
Here are some examples where the vulnerability would be present before the patch, because of unfiltered user input
75+
76+
```js
77+
const express = require('express')
78+
const expressPlayground = require('graphql-playground-middleware-express')
79+
.default
80+
81+
const app = express()
82+
83+
app.use(express.json())
84+
85+
// params
86+
app.get('/playground/:id', (req) =>
87+
expressPlayground({
88+
endpoint: `/our/graphql/${req.params.id}`,
89+
}),
90+
)
91+
92+
// params
93+
app.get('/playground', (req) =>
94+
expressPlayground({
95+
endpoint: `/our/graphql`,
96+
// any settings that are unsanitized user input, not just `endpoint`
97+
settings: { 'editor.fontFamily': req.query.font },
98+
}),
99+
)
100+
```
101+
102+
[See a proof of concept](packages/graphql-playground-html/examples/xss-attack) to understand the vulnerability better
103+
104+
### Workaround
105+
106+
To fix this issue without the update, you can sanitize however you want.
107+
108+
We suggest using [`xss`](https://www.npmjs.com/package/xss) (what we use for our own fix)
109+
110+
For example, with `graphql-playground-middleware-express`:
111+
112+
```js
113+
const express = require('express')
114+
const { filterXSS } = require('xss')
115+
const expressPlayground = require('graphql-playground-middleware-express')
116+
.default
117+
118+
119+
const app = express()
120+
121+
const filter = (val) => filterXSS(val, {
122+
whitelist: [],
123+
stripIgnoreTag: true,
124+
stripIgnoreTagBody: ['script']
125+
})
126+
127+
// simple example
128+
app.get('/playground/:id', (req) =>
129+
expressPlayground({ endpoint: `/graphql/${filter(req.params.id)}` })
130+
131+
// advanced params
132+
app.get('/playground', (req) =>
133+
expressPlayground(JSON.parse(filter(JSON.stringify(req.query))))
134+
```
135+
136+
[See a proof of concept workaround](packages/graphql-playground-html/examples/xss-attack), example #3

Diff for: docs/security/2021-schema-xss-phishing-attack.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## GraphQL Playground introspection schema template injection attack: Advisory Statement
2+
3+
This is a security advisory for an XSS vulnerability in `graphql-playground`.
4+
5+
A similar vulnerability affects `graphiql`, the package from which `graphql-playground` was forked. There is a corresponding `graphiql` [advisory](https://github.com/graphql/graphiql/security/advisories/GHSA-x4r7-m2q9-69c8).
6+
7+
- [1. Impact](#1-impact)
8+
- [2. Scope](#2-scope)
9+
- [3. Patches](#3-patches)
10+
- [4. Reproducing the exploit](#4-reproducing-the-exploit)
11+
- [5. Credit](#5-credit)
12+
- [6. For more information](#6-for-more-information)
13+
14+
### 1. Impact
15+
16+
All versions of `graphql-playground-react` older than `[email protected]` are vulnerable to compromised HTTP schema introspection responses or `schema` prop values with malicious GraphQL type names, exposing a dynamic XSS attack surface that can allow code injection on operation autocomplete.
17+
18+
In order for the attack to take place, the user must load a malicious schema in `graphql-playground`. There are several ways this can occur, including by specifying the URL to a malicious schema in the `endpoint` query parameter. If a user clicks on a link to a GraphQL Playground installation that specifies a malicious server, arbitrary JavaScript can run in the user's browser, which can be used to exfiltrate user credentials or other harmful goals.
19+
20+
### 2. Scope
21+
22+
This advisory describes the impact on the `graphql-playground-react` package. The vulnerability also affects `graphiql`, the package from which `graphql-playground` was forked, with a less severe impact; see the [`graphiql` advisory](https://github.com/graphql/graphiql/security/advisories/GHSA-x4r7-m2q9-69c8) for details. It affects all versions of `graphql-playground-react` older than `v1.7.28`.
23+
24+
This vulnerability was introduced with the first public release of `graphql-playground`, so it impacts both the original legacy `graphql-playground` and the contemporary `graphql-playground-react` npm package. It is most easily exploited on `[email protected]` and newer, as that release added functionality which made it possible to override the endpoint URL via query parameter even if it is explicitly specified in the code.
25+
26+
`graphql-playground-react` is commonly loaded via the `graphql-playground-html` package or a middleware package that wraps it (`graphql-playground-express`, `graphql-playground-middleware-koa`, `graphql-playground-middleware-hapi`, or `graphql-playground-middleware-lambda`). By default, these packages render an HTML page which loads the *latest* version of `graphql-playground-react` through a CDN. If you are using one of these packages to install GraphQL Playground on your domain *and you do not explicitly pass the `version` option to `renderPlaygroundPage` or the middleware function*, then you do not need to take any action to resolve this vulnerability, as the latest version of the React app will automatically be loaded.
27+
28+
`graphql-playground-react` is also commonly loaded via HTML served by Apollo Server. Apollo Server always pins a specific version of `graphql-playground-react`, so if you are using Apollo Server you do need to take action to resolve this vulnerability. See the [Apollo Server advisory](https://github.com/apollographql/apollo-server/security/advisories/GHSA-qm7x-rc44-rrqw) for details.
29+
30+
31+
### 3. Patches
32+
33+
`[email protected]` addresses this issue via defense in depth:
34+
35+
- **HTML-escaping text** that should be treated as text rather than HTML. In most of the app, this happens automatically because React escapes all interpolated text by default. However, one vulnerable component uses the unsafe `innerHTML` API and interpolated type names directly into HTML. We now properly escape that type name, which fixes the known vulnerability.
36+
37+
- **Validates the schema** upon receiving the introspection response or schema changes. Schemas with names that violate the GraphQL spec will no longer be loaded. (This includes preventing the Doc Explorer from loading.) This change is also sufficient to fix the known vulnerability.
38+
39+
- **Ensuring that user-generated HTML is safe**. Schemas can contain Markdown in `description` and `deprecationReason` fields, and the web app renders them to HTML using the `markdown-it` library. Prior to `[email protected]`, GraphQL Playground used two separate libraries to render Markdown: `markdown-it` and `marked`. As part of the development of `[email protected]`, we verified that our use of `markdown-it` prevents the inclusion of arbitrary HTML. We use `markdown-it` without setting `html: true`, so we are comfortable relying on [`markdown-it`'s HTML escaping](https://github.com/markdown-it/markdown-it/blob/master/docs/security.md) here. We considered running a second level of sanitization over all rendered Markdown using a library such as `dompurify` but believe that is unnecessary as `markdown-it`'s sanitization appears to be adequate. `[email protected]` does update to the latest version of `markdown-it` (v12, from v10) so that any security fixes in v11 and v12 will take effect. On the other hand, [`marked`](https://github.com/markedjs/marked) recommends the use of a separate HTML sanitizer if its input is untrusted. In this release, we switch the one component which uses `marked` to use `markdown-it` like the rest of the app.
40+
41+
**If you are using `graphql-playground-react` directly in your client app**, upgrade to version 1.7.28 or later.
42+
43+
**If you are using `graphql-playground-html` or a package which starts with `graphql-playground-middleware-` in your server** and you are passing the `version` option to a function imported from that package, change that `version` option to be at least `"1.7.28"`.
44+
45+
**If you are using `graphql-playground-html` or a package which starts with `graphql-playground-middleware-` in your server** and you are **NOT** passing the `version` option to a function imported from that package, no action is necessary; your app automatically loads the latest version of `graphql-playground-react` from CDN.
46+
47+
48+
### 4. Reproducing the exploit
49+
50+
We are hosting a "malicious" server at https://graphql-xss-schema.netlify.app/graphql . This server has a hard-coded introspection result that includes unsafe HTML in type names.
51+
52+
If you manually change a GraphQL Playground installation to use that endpoint, clear the operation pane, and type `{x` into the operation pane, an alert will pop up; this demonstrates execution of code provided by the malicious server.
53+
54+
An URL like https://YOUR-PLAYGROUND-SERVER/?endpoint=https%3A%2F%2Fgraphql-xss-schema.netlify.app%2Fgraphql&query=%7B will load already configured with the endpoint in question. (This URL-based exploit works on `[email protected]` and newer; older versions may be protected from this particular URL-based exploit depending on their configuration.)
55+
### 5. Credit
56+
57+
This vulnerability was discovered by [@Ry0taK](https://github.com/Ry0taK), thank you! :1st_place_medal:
58+
59+
Others who contributed:
60+
61+
- extensive help from [@glasser](https://github.com/glasser) at [Apollo](https://github.com/apollographql)
62+
- [@acao](https://github.com/acao)
63+
- [@imolorhe](https://github.com/imolorhe)
64+
- [@divyenduz](https://github.com/divyenduz)
65+
- [@dotansimha](https://github.com/dotansimha)
66+
- [@timsuchanek](http://github.com/timsuchanek)
67+
- [@benjie](https://github.com/Ry0taK) and many others who provided morale support
68+
69+
### 6. For more information
70+
71+
If you have any questions or comments about this advisory:
72+
73+
- The `graphiql` advisory document contains [more information](https://github.com/graphql/graphiql/blob/main/docs/security/2021-introspection-schema-xss.md#2-more-details-on-the-vulnerability) about how both the client-side and server-side vulnerabilities work
74+
- Open an issue in the [graphql-playground repo](https://github.com/graphql/graphql-playground/new/issues)

Diff for: packages/graphql-playground-react/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
"@babel/preset-env": "^7.0.0",
5353
"@babel/preset-react": "^7.0.0",
5454
"@types/deasync": "0.1.0",
55+
"@types/escape-html": "^1.0.1",
5556
"@types/jest": "22.2.3",
57+
"@types/markdown-it": "^12.2.3",
5658
"@types/node": "12.12.34",
5759
"@types/react": "16.9.32",
5860
"@types/zen-observable": "^0.5.3",
@@ -119,6 +121,7 @@
119121
"copy-to-clipboard": "^3.0.8",
120122
"cryptiles": "4.1.2",
121123
"cuid": "^1.3.8",
124+
"escape-html": "^1.0.3",
122125
"graphiql": "^0.17.5",
123126
"graphql": "^15.3.0",
124127
"immutable": "^4.0.0-rc.9",
@@ -128,8 +131,8 @@
128131
"keycode": "^2.1.9",
129132
"lodash": "^4.17.11",
130133
"lodash.debounce": "^4.0.8",
131-
"markdown-it": "^8.4.1",
132-
"marked": "^0.8.2",
134+
"lru-cache": "^6.0.0",
135+
"markdown-it": "^12.2.0",
133136
"prettier": "2.0.2",
134137
"prop-types": "^15.7.2",
135138
"query-string": "5",

0 commit comments

Comments
 (0)