Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feature/add known characters to whitelist #297

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion api/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ var defaultContentTypes = []string{
"timeseries_dataset",
}

// contains the special characters that are allowed in query validation
const AllowedSpecialCharacters = "–‘’"

type URIsRequest struct {
URIs []string `json:"uris"`
Limit int `json:"limit,omitempty"` // Limit is optional
Expand Down Expand Up @@ -835,7 +838,7 @@ func sanitiseDoubleQuotes(str string) string {
}

func checkForSpecialCharacters(str string) bool {
re := regexp.MustCompile("[[:^ascii:]]")
re := regexp.MustCompile(fmt.Sprintf("[^[:ascii:]%s]", regexp.QuoteMeta(AllowedSpecialCharacters)))
return re.MatchString(str)
}

Expand Down
6 changes: 6 additions & 0 deletions api/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func TestCheckForSpecialCharacters(t *testing.T) {
c.So(actual, c.ShouldEqual, expected)
})

c.Convey("A string containing whitelisted special characters should return false", t, func() {
expected := false
actual := checkForSpecialCharacters("Test string –‘’")
c.So(actual, c.ShouldEqual, expected)
})

c.Convey("A string containing special characters should return true", t, func() {
expected := true
actual := checkForSpecialCharacters("Test 怎么开 string")
Expand Down
8 changes: 8 additions & 0 deletions features/search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ Feature: Search endpoint should return data for requested search parameter
"""
invalid URI prefix parameter
"""

Scenario: When Searching with whitelisted special characters I get the expected results
Given elasticsearch is healthy
And elasticsearch returns one item in search response
When I GET "/search?q=CPI–‘’"
Then the HTTP status code should be "200"
And the response header "Content-Type" should be "application/json;charset=utf-8"
And the response body is the same as the json in "./features/testdata/expected_single_search_result.json"