Skip to content

Commit 7862e9d

Browse files
Disable query string in key (#319)
* feat(core): initial commit for disabling query string in key * feat(core): add disable_query parameter * docs: update README with new configuration * fix: missing new configuration parameters in two plugin files * test: fix tests because of disable_query changes, add new tests for disable_query
1 parent e9a8e70 commit 7862e9d

File tree

8 files changed

+50
-5
lines changed

8 files changed

+50
-5
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ cache_keys:
8383
disable_body: true # Prevent the body from being used in the cache key
8484
disable_host: true # Prevent the host from being used in the cache key
8585
disable_method: true # Prevent the method from being used in the cache key
86+
disable_query: true # Prevent the query string from being used in the cache key
8687
headers: # Add headers to the key
8788
- Authorization # Add the header value in the key
8889
- Content-Type # Add the header value in the key
@@ -102,6 +103,7 @@ default_cache:
102103
disable_body: true # Prevent the body from being used in the cache key
103104
disable_host: true # Prevent the host from being used in the cache key
104105
disable_method: true # Prevent the method from being used in the cache key
106+
disable_query: true # Prevent the query string from being used in the cache key
105107
headers: # Add headers to the key
106108
- Authorization # Add the header value in the key
107109
- Content-Type # Add the header value in the key
@@ -165,6 +167,7 @@ surrogate_keys:
165167
| `cache_keys.{your regexp}.disable_body` | Disable the body part in the key matching the regexp (GraphQL context) | `true`<br/><br/>`(default: false)` |
166168
| `cache_keys.{your regexp}.disable_host` | Disable the host part in the key matching the regexp | `true`<br/><br/>`(default: false)` |
167169
| `cache_keys.{your regexp}.disable_method` | Disable the method part in the key matching the regexp | `true`<br/><br/>`(default: false)` |
170+
| `cache_keys.{your regexp}.disable_query` | Disable the query string part in the key matching the regexp | `true`<br/><br/>`(default: false)` |
168171
| `cache_keys.{your regexp}.headers` | Add headers to the key matching the regexp | `- Authorization`<br/><br/>`- Content-Type`<br/><br/>`- X-Additional-Header` |
169172
| `cache_keys.{your regexp}.hide` | Prevent the key from being exposed in the `Cache-Status` HTTP response header | `true`<br/><br/>`(default: false)` |
170173
| `cdn` | The CDN management, if you use any cdn to proxy your requests Souin will handle that | |
@@ -189,6 +192,7 @@ surrogate_keys:
189192
| `default_cache.key.disable_body` | Disable the body part in the key (GraphQL context) | `true`<br/><br/>`(default: false)` |
190193
| `default_cache.key.disable_host` | Disable the host part in the key | `true`<br/><br/>`(default: false)` |
191194
| `default_cache.key.disable_method` | Disable the method part in the key | `true`<br/><br/>`(default: false)` |
195+
| `default_cache.key.disable_query` | Disable the query string part in the key | `true`<br/><br/>`(default: false)` |
192196
| `default_cache.key.headers` | Add headers to the key matching the regexp | `- Authorization`<br/><br/>`- Content-Type`<br/><br/>`- X-Additional-Header` |
193197
| `default_cache.key.hide` | Prevent the key from being exposed in the `Cache-Status` HTTP response header | `true`<br/><br/>`(default: false)` |
194198
| `default_cache.nuts` | Configure the Nuts cache storage | |
@@ -457,6 +461,7 @@ There is the fully configuration below
457461
disable_body
458462
disable_host
459463
disable_method
464+
disable_query
460465
headers X-Token Authorization
461466
hide
462467
}
@@ -476,6 +481,7 @@ There is the fully configuration below
476481
disable_body
477482
disable_host
478483
disable_method
484+
disable_query
479485
headers Content-Type Authorization
480486
}
481487
log_level debug

configurationtypes/types.go

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type Key struct {
115115
DisableBody bool `json:"disable_body" yaml:"disable_body"`
116116
DisableHost bool `json:"disable_host" yaml:"disable_host"`
117117
DisableMethod bool `json:"disable_method" yaml:"disable_method"`
118+
DisableQuery bool `json:"disable_query" yaml:"disable_query"`
118119
Hide bool `json:"hide" yaml:"hide"`
119120
Headers []string `json:"headers" yaml:"headers"`
120121
}

context/key.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type keyContext struct {
1818
disable_body bool
1919
disable_host bool
2020
disable_method bool
21+
disable_query bool
2122
displayable bool
2223
headers []string
2324
overrides map[*regexp.Regexp]keyContext
@@ -38,14 +39,15 @@ func (g *keyContext) SetupContext(c configurationtypes.AbstractConfigurationInte
3839
disable_body: v.DisableBody,
3940
disable_host: v.DisableHost,
4041
disable_method: v.DisableMethod,
42+
disable_query: v.DisableQuery,
4143
displayable: v.Hide,
4244
headers: v.Headers,
4345
}
4446
}
4547
}
4648

4749
func (g *keyContext) SetContext(req *http.Request) *http.Request {
48-
key := req.URL.RequestURI()
50+
key := req.URL.Path
4951
var headers []string
5052

5153
scheme := "http-"
@@ -58,6 +60,10 @@ func (g *keyContext) SetContext(req *http.Request) *http.Request {
5860
headerValues := ""
5961
displayable := g.displayable
6062

63+
if !g.disable_query && len(req.URL.RawQuery) > 0 {
64+
key += "?" + req.URL.RawQuery
65+
}
66+
6167
if !g.disable_body {
6268
body = req.Context().Value(HashBody).(string)
6369
}

context/key_test.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func Test_KeyContext_SetContext(t *testing.T) {
6161
ctx := keyContext{}
6262
req := httptest.NewRequest(http.MethodGet, "http://domain.com", nil)
6363
req = ctx.SetContext(req.WithContext(context.WithValue(req.Context(), HashBody, "-with_the_hash")))
64-
if req.Context().Value(Key).(string) != "GET-http-domain.com-/-with_the_hash" {
65-
t.Errorf("The Key context must be equal to GET-http-domain.com-/-with_the_hash, %s given.", req.Context().Value(Key).(string))
64+
if req.Context().Value(Key).(string) != "GET-http-domain.com--with_the_hash" {
65+
t.Errorf("The Key context must be equal to GET-http-domain.com--with_the_hash, %s given.", req.Context().Value(Key).(string))
6666
}
6767

6868
m := make(map[*regexp.Regexp]keyContext)
@@ -101,4 +101,28 @@ func Test_KeyContext_SetContext(t *testing.T) {
101101
if req4.Context().Value(Key).(string) != "http-domain.com-/something" {
102102
t.Errorf("The Key context must be equal to http-domain.com-/something, %s given.", req4.Context().Value(Key).(string))
103103
}
104+
105+
// Added tests for disable_query
106+
ctx4 := keyContext{
107+
disable_query: true,
108+
disable_method: false,
109+
disable_host: false,
110+
}
111+
req5 := httptest.NewRequest(http.MethodGet, "http://domain.com/matched?query=string", nil)
112+
req5 = ctx4.SetContext(req5.WithContext(context.WithValue(req5.Context(), HashBody, "")))
113+
if req5.Context().Value(Key).(string) != "GET-http-domain.com-/matched" {
114+
t.Errorf("The Key context must be equal to GET-http-domain.com-/matched, %s given.", req5.Context().Value(Key).(string))
115+
}
116+
117+
ctx5 := keyContext{
118+
disable_query: false,
119+
disable_method: false,
120+
disable_host: false,
121+
}
122+
req6 := httptest.NewRequest(http.MethodGet, "http://domain.com/matched?query=string", nil)
123+
req6 = ctx5.SetContext(req6.WithContext(context.WithValue(req6.Context(), HashBody, "")))
124+
if req6.Context().Value(Key).(string) != "GET-http-domain.com-/matched?query=string" {
125+
t.Errorf("The Key context must be equal to GET-http-domain.com-/matched?query=string, %s given.", req6.Context().Value(Key).(string))
126+
}
127+
104128
}

context/types_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func Test_Context_SetContext(t *testing.T) {
3838
co.Init(&c)
3939
req := httptest.NewRequest(http.MethodGet, "http://domain.com", nil)
4040
req = co.SetContext(req)
41-
if req.Context().Value(Key) != "GET-http-domain.com-/" {
42-
t.Errorf("The Key context must be equal to GET-http-domain.com-/, %s given.", req.Context().Value(Key))
41+
if req.Context().Value(Key) != "GET-http-domain.com-" {
42+
t.Errorf("The Key context must be equal to GET-http-domain.com-, %s given.", req.Context().Value(Key))
4343
}
4444
if req.Context().Value(GraphQL) != false {
4545
t.Error("The GraphQL context must be false.")

plugins/caddy/configuration.go

+4
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
331331
ck.DisableHost = true
332332
case "disable_method":
333333
ck.DisableMethod = true
334+
case "disable_query":
335+
ck.DisableQuery = true
334336
case "hide":
335337
ck.Hide = true
336338
case "headers":
@@ -395,6 +397,8 @@ func parseConfiguration(cfg *Configuration, h *caddyfile.Dispenser, isBlocking b
395397
config_key.DisableHost = true
396398
case "disable_method":
397399
config_key.DisableMethod = true
400+
case "disable_query":
401+
config_key.DisableQuery = true
398402
case "hide":
399403
config_key.Hide = true
400404
case "headers":

plugins/kratos/configuration.go

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ func parseCacheKeys(ccConfiguration map[string]config.Value) map[configurationty
101101
ck.DisableHost = true
102102
case "disable_method":
103103
ck.DisableMethod = true
104+
case "disable_query":
105+
ck.DisableQuery = true
104106
case "hide":
105107
ck.Hide = true
106108
case "headers":

plugins/souin/agnostic/configuration_parser.go

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func parseCacheKeys(ccConfiguration map[string]interface{}) map[configurationtyp
6767
ck.DisableHost = true
6868
case "disable_method":
6969
ck.DisableMethod = true
70+
case "disable_query":
71+
ck.DisableQuery = true
7072
case "hide":
7173
ck.Hide = true
7274
case "headers":

0 commit comments

Comments
 (0)