graphql-safe-guards 1.1.2 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +39 -10
  2. package/README.md +133 -40
  3. package/package.json +14 -9
package/CHANGELOG.md CHANGED
@@ -2,34 +2,50 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
- The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
5
+ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/).
7
7
 
8
8
  ---
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ - Internal improvements and documentation refinements.
13
+
14
+ ---
15
+
12
16
  ## [1.1.2] – 2026-01-22
13
17
 
18
+ ### Fixed
19
+
20
+ - Corrected edge cases where GraphQL introspection queries were still
21
+ partially evaluated under certain validation paths.
22
+ - Improved consistency when `ignoreIntrospection` is enabled to ensure
23
+ introspection queries fully bypass depth and complexity validation.
24
+
25
+ _No breaking changes._
26
+
27
+ ---
28
+
14
29
  ## [1.1.0] – 2026-01-22
15
30
 
16
31
  ### Added
17
32
 
18
33
  - `ignoreIntrospection` option to allow GraphQL introspection queries
19
34
  without applying depth and complexity validation.
20
- - Documentation explaining how to use `ignoreIntrospection` with
21
- GraphQL Playground and Apollo Sandbox.
22
- - Security guidelines for private APIs with introspection enabled.
35
+ - Security documentation explaining safe introspection usage for
36
+ private APIs and development tools (GraphQL Playground, Apollo Sandbox).
23
37
 
24
38
  ### Changed
25
39
 
26
- - `ignoreIntrospection` option to exclude GraphQL introspection queries
27
- from depth and complexity validation.
40
+ - Introspection queries are now explicitly detected and handled
41
+ during validation rather than relying on implicit field checks.
28
42
 
29
43
  ### Fixed
30
44
 
31
- - Ensured GraphQL introspection queries are excluded from depth and
32
- complexity validation when `ignoreIntrospection` is enabled.
45
+ - Prevented false positives when validating introspection-related fields
46
+ in schemas with strict depth or complexity limits.
47
+
48
+ _No breaking changes._
33
49
 
34
50
  ---
35
51
 
@@ -37,9 +53,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
37
53
 
38
54
  ### Added
39
55
 
40
- - Preset-based configuration (`strict`, `balanced`, `relaxed`)
56
+ - Preset-based configuration (`strict`, `balanced`, `relaxed`) for
57
+ common production use cases.
41
58
  - Integration tests validating combined depth and complexity limits
59
+ using native `graphql-js` validation.
42
60
 
43
61
  ### Fixed
44
62
 
45
- - Type-safe preset resolution
63
+ - Type-safe preset resolution to ensure predictable configuration behavior.
64
+
65
+ ---
66
+
67
+ ## [1.0.0]
68
+
69
+ ### Added
70
+
71
+ - Initial release.
72
+ - Unified depth limiting and query complexity validation.
73
+ - Native GraphQL validation rule integration.
74
+ - Framework-agnostic design compatible with `graphql-js` and major servers.
package/README.md CHANGED
@@ -4,12 +4,58 @@
4
4
  ![license](https://img.shields.io/npm/l/graphql-safe-guards)
5
5
  ![typescript](https://img.shields.io/badge/TypeScript-Ready-blue)
6
6
 
7
- # graphql-safe-guards
7
+ # graphql-safe-guards 🛡️
8
8
 
9
- Protect your GraphQL API with a single import.
9
+ Protect your GraphQL API from **deep, expensive, and abusive queries**
10
+ by enforcing **query depth** and **query complexity limits before execution**.
10
11
 
11
- A tiny utility that **combines depth limiting and query complexity validation**
12
- using **native GraphQL validation rules**.
12
+ A tiny, framework-agnostic utility that combines **depth limiting** and
13
+ **query complexity validation** using **native GraphQL validation rules**.
14
+
15
+ > Designed for production GraphQL APIs, public endpoints, and high-traffic systems.
16
+
17
+ ---
18
+
19
+ ## 🚨 The Problem
20
+
21
+ GraphQL gives clients a lot of power — sometimes **too much**.
22
+
23
+ Without safeguards, a single query can:
24
+
25
+ - Exhaust database connections
26
+ - Cause CPU spikes
27
+ - Trigger accidental or malicious DoS attacks
28
+ - Degrade performance for all users
29
+
30
+ This is especially dangerous in **public APIs** or **high-traffic applications**.
31
+
32
+ ---
33
+
34
+ ## ✅ The Solution
35
+
36
+ `graphql-safe-guards` acts as a **logical firewall** for GraphQL queries.
37
+
38
+ Before **any resolver executes**, it:
39
+
40
+ - Validates query depth
41
+ - Calculates query complexity
42
+ - Rejects unsafe operations early
43
+
44
+ No resolvers are executed.
45
+ No database calls are made.
46
+
47
+ ---
48
+
49
+ ## ✨ Features
50
+
51
+ - ✅ Limit query depth
52
+ - ✅ Limit query complexity
53
+ - ✅ Blocks abusive or accidental DoS-style queries
54
+ - ✅ Uses native GraphQL validation (AST-based)
55
+ - ✅ No schema directives
56
+ - ✅ No runtime execution cost
57
+ - ✅ Framework-agnostic
58
+ - ✅ Production-ready presets
13
59
 
14
60
  ---
15
61
 
@@ -17,11 +63,13 @@ using **native GraphQL validation rules**.
17
63
 
18
64
  ```bash
19
65
  npm install graphql-safe-guards
66
+ # or
67
+ yarn add graphql-safe-guards
20
68
  ```
21
69
 
22
70
  ---
23
71
 
24
- ## Usage (Apollo Server)
72
+ ## 🚀 Quick Start (Apollo Server)
25
73
 
26
74
  ```ts
27
75
  import { ApolloServer } from "@apollo/server";
@@ -36,7 +84,31 @@ const server = new ApolloServer({
36
84
  });
37
85
  ```
38
86
 
39
- ---
87
+ Any query exceeding the limits will be rejected before execution.
88
+
89
+ ## Presets (Recommended)
90
+
91
+ graphql-safe-guards includes opinionated, production-ready presets for common use cases.
92
+
93
+ ```ts
94
+ createSafeGuards({ preset: "strict" });
95
+ createSafeGuards({ preset: "balanced" });
96
+ createSafeGuards({ preset: "relaxed" });
97
+ ```
98
+
99
+ | Preset | Depth | Complexity | Use case |
100
+ | -------- | ----- | ---------- | --------------------------- |
101
+ | strict | 3 | 50 | Public APIs, read-only APIs |
102
+ | balanced | 4 | 100 | Private frontends |
103
+ | relaxed | 6 | 200 | Admin / internal tools |
104
+
105
+ Environment-based setup
106
+
107
+ ```ts
108
+ createSafeGuards({
109
+ preset: process.env.NODE_ENV === "production" ? "strict" : "balanced",
110
+ });
111
+ ```
40
112
 
41
113
  ## Configuration
42
114
 
@@ -55,7 +127,9 @@ createSafeGuards({
55
127
  });
56
128
  ```
57
129
 
58
- Security Note ⚠️
130
+ ---
131
+
132
+ ## 🔐 Security Note — Introspection⚠️
59
133
 
60
134
  - This library does not enable or disable GraphQL introspection
61
135
 
@@ -74,39 +148,61 @@ const server = new ApolloServer({
74
148
  });
75
149
  ```
76
150
 
77
- ## Presets (Recommended)
151
+ ---
78
152
 
79
- graphql-safe-guards includes opinionated, production-ready presets for common use cases.
153
+ ### 🔥 What It Protects Against
154
+
155
+ Deeply nested queries
80
156
 
81
157
  ```ts
82
- createSafeGuards({ preset: "strict" });
83
- createSafeGuards({ preset: "balanced" });
84
- createSafeGuards({ preset: "relaxed" });
158
+ a {
159
+ b {
160
+ c {
161
+ d {
162
+ e {
163
+ f {
164
+ g
165
+ }
166
+ }
167
+ }
168
+ }
169
+ }
170
+ }
85
171
  ```
86
172
 
87
- | Preset | Depth | Complexity | Use case |
88
- | -------- | ----- | ---------- | --------------------------- |
89
- | strict | 3 | 50 | Public APIs, read-only APIs |
90
- | balanced | 4 | 100 | Private frontends |
91
- | relaxed | 6 | 200 | Admin / internal tools |
173
+ High-cost queries
174
+
175
+ ```ts
176
+ posts(limit: 100) {
177
+ comments(limit: 100) {
178
+ author {
179
+ posts(limit: 100)
180
+ }
181
+ }
182
+ }
183
+ ```
92
184
 
93
185
  ---
94
186
 
95
- ## What It Does
187
+ ## 🧠 How It Works
96
188
 
97
- - Limits query depth
98
- - Limits query complexity
99
- - Uses native GraphQL validation
100
- - No schema directives
101
- - No runtime execution cost
102
- - Framework agnostic
189
+ - Parses the GraphQL AST
190
+ - Computes depth and complexity
191
+ - Runs during the validation phase
192
+ - Blocks unsafe queries before execution
103
193
 
104
- Internally, this package composes:
194
+ Fast, predictable, and scalable.
195
+
196
+ ---
197
+
198
+ ## 🧩 Internals
199
+
200
+ This package composes and unifies:
105
201
 
106
202
  - `graphql-safe-depth`
107
203
  - `graphql-complexity-validation`
108
204
 
109
- The combination is validated through integration tests using native GraphQL validation.
205
+ Validated through integration tests using native graphql-js validation.
110
206
 
111
207
  ---
112
208
 
@@ -120,21 +216,13 @@ The combination is validated through integration tests using native GraphQL vali
120
216
 
121
217
  ---
122
218
 
123
- ## Why this exists
124
-
125
- Depth limiting and complexity analysis solve **different problems**.
126
- Most GraphQL servers need **both**, but wiring them together is repetitive.
219
+ ## 📊 Production Recommendations
127
220
 
128
- `graphql-safe-guards` provides a **single, predictable entry point**
129
- for GraphQL query safety.
130
-
131
- ### Environment-based setup
132
-
133
- ```ts
134
- createSafeGuards({
135
- preset: process.env.NODE_ENV === "production" ? "strict" : "balanced",
136
- });
137
- ```
221
+ - For best results, combine this library with:
222
+ - DataLoader (avoid N+1 queries)
223
+ - Pagination on list fields
224
+ - HTTP caching / CDN
225
+ - Persisted queries (hash-based)
138
226
 
139
227
  ---
140
228
 
@@ -150,6 +238,11 @@ createSafeGuards({
150
238
 
151
239
  > Roadmap items may change based on feedback and real-world usage.
152
240
 
241
+ ## ⭐ Support
242
+
243
+ If this library helped you secure your GraphQL API,
244
+ please consider giving it a ⭐ on GitHub.
245
+
153
246
  ## License
154
247
 
155
248
  MIT © Mateo Diaz
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "graphql-safe-guards",
3
- "version": "1.1.2",
4
- "description": "Opinionated GraphQL security guards (depth + complexity) in one import",
3
+ "version": "1.2.1",
4
+ "description": "Protect GraphQL APIs from deep and expensive queries using depth and complexity limits",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
@@ -16,7 +16,6 @@
16
16
  "prepublishOnly": "npm run build"
17
17
  },
18
18
  "dependencies": {
19
- "-": "^0.0.1",
20
19
  "graphql-complexity-validation": "^1.0.4",
21
20
  "graphql-safe-depth": "^1.0.0"
22
21
  },
@@ -25,12 +24,18 @@
25
24
  },
26
25
  "keywords": [
27
26
  "graphql",
28
- "security",
29
- "depth",
30
- "complexity",
31
- "apollo",
32
- "nest",
33
- "yoga"
27
+ "graphql-security",
28
+ "graphql-validation",
29
+ "graphql-query",
30
+ "graphql-depth-limit",
31
+ "graphql-complexity",
32
+ "query-complexity",
33
+ "depth-limit",
34
+ "dos-protection",
35
+ "api-security",
36
+ "apollo-server",
37
+ "nestjs",
38
+ "graphql-yoga"
34
39
  ],
35
40
  "author": "Mateo diaz lopez <mateodiaz401@gmail.com>",
36
41
  "license": "MIT",