Skip to content

Commit 1e987e8

Browse files
Relax the NLQ content‑type check to properly parse media types
- Adding an isNLQContentType helper that uses mime.ParseMediaType + strings.EqualFold - Updating SelectAndRewrite to use http.MethodPost + isNLQContentType(...) instead of an exact string match - Covering the new helper with a small table‑driven unit test Signed-off-by: Julien Barbot <jubarbot@cisco.com>
1 parent fb0bf6a commit 1e987e8

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

plugins/agent_bridge.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"io"
11+
"mime"
1112
"net/http"
1213
"strings"
1314

@@ -41,6 +42,15 @@ const (
4142

4243
var logger = log.Get()
4344

45+
// isNLQContentType parses a Content-Type header and returns true if it denotes application/nlq
46+
func isNLQContentType(contentType string) bool {
47+
mediaType, _, err := mime.ParseMediaType(contentType)
48+
if err != nil {
49+
return false
50+
}
51+
return strings.EqualFold(mediaType, CONTENT_TYPE_NLQ)
52+
}
53+
4454
func SelectAndRewrite(rw http.ResponseWriter, r *http.Request) {
4555
logger.Debugf("[+] Inside SelectAndRewrite ...")
4656
apiConfig, err := getPluginFromRequest(r)
@@ -71,8 +81,8 @@ func SelectAndRewrite(rw http.ResponseWriter, r *http.Request) {
7181
return
7282
}
7383

74-
// POST and Content-Type: application/nlq are expected
75-
if !(r.Method == "POST" && r.Header.Get("Content-Type") == CONTENT_TYPE_NLQ) {
84+
// Only proceed for POST with Content-Type: application/nlq (parameters are allowed)
85+
if r.Method != http.MethodPost || !isNLQContentType(r.Header.Get("Content-Type")) {
7686
logger.Debugf("[+] Query is not POST or Content-Type is not %s, ignoring ...", CONTENT_TYPE_NLQ)
7787
return
7888
}

plugins/agent_bridge_acp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func ProcessACPQuery(rw http.ResponseWriter, r *http.Request) {
5858
}
5959
}
6060

61-
// POST and Content-Type: application/nlq are expected
62-
if !(r.Method == "POST" && r.Header.Get("Content-Type") == CONTENT_TYPE_NLQ) {
61+
// Only proceed for POST with Content-Type: application/nlq (parameters are allowed)
62+
if r.Method != http.MethodPost || !isNLQContentType(r.Header.Get("Content-Type")) {
6363
logger.Debugf("[+] Query is not POST or Content-Type is not %s, ignoring ...", CONTENT_TYPE_NLQ)
6464
return
6565
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "testing"
4+
5+
// TestIsNLQContentType verifies that various Content-Type header values
6+
// are correctly recognized (or not) as application/nlq.
7+
func TestIsNLQContentType(t *testing.T) {
8+
tests := []struct {
9+
ct string
10+
want bool
11+
}{
12+
{"application/nlq", true},
13+
{"application/nlq; charset=utf-8", true},
14+
{"APPLICATION/NLQ", true},
15+
{"application/NLQ; param=val", true},
16+
{"application/json", false},
17+
{"application/nlq+xml", false},
18+
{"", false},
19+
{"invalid/md", false},
20+
}
21+
for _, tc := range tests {
22+
got := isNLQContentType(tc.ct)
23+
if got != tc.want {
24+
t.Errorf("isNLQContentType(%q) = %v; want %v", tc.ct, got, tc.want)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)