Skip to content

Commit 975cc61

Browse files
authored
AOS-100: Update docs for sanitisation (#33)
* AOS-100: Update docs regarding the query class and the need for handling sanitisation of the string for including in the template * AOS-100: Update docs regarding the query class and the need for handling sanitisation of the string for including in the template * AOS-100 Add query sanitisation example * AOS-100 Add query sanitisation example * AOS-100 Reinstate getQuery bullet point * AOS-100 Remove additional space * AOS-100 Remove additional quote
1 parent b6afd22 commit 975cc61

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

docs/detailed-result-handling.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,29 @@ simple methods available that you can access anywhere.
2020
* `isSuccess()`: Simply states whether or not the search was a success, or error.
2121
* `getRecords()`: A `PaginatedList` of `Record` objects that were returned by the search service based on your `Query`.
2222
* `getFacets`: An `ArrayList` of `Facet` objects that were returned by the search service based on your `Query`.
23-
* `getQuery`": The `Query` object
23+
* `getQuery`: The `Query` object containing the user entered search term. See the [Query class](#query-class) section below for safe handling of user input.
2424

2525
The `Results` class is also a `ViewableData` object, so these methods can be access in your template with `$isSuccess`,
26-
`$Records`, `$Facets`, and `$Query`.
26+
`$Records`, and `$Facets`.
2727

2828
## `Query` class
29-
The `Query` class provides the original query that was used for matching results. This is available should you wish
30-
to include `Showing results for "test"` or similar text on your results page.
31-
32-
**Important:** When including `$Query` in the template it should be noted that this assumes the query string is
33-
safe and that the implementation has sanitised the user input to mitigate against cross-site scripting (xss) attacks.
29+
The `Query` class provides the original query that was used for matching results.
30+
31+
> [!IMPORTANT]
32+
> If you need to include the search term on the page (for example, `Showing results for "test"`) you will
33+
> need to handle sanitisation of this value to mitigate against cross-site scripting (xss) attacks. The simplest
34+
> way of doing this is to create a custom function that returns a DBText field, and the Silverstripe templating
35+
> system will handle this for you (see example below).
36+
37+
````php
38+
/**
39+
* Wraps the raw query string in a DBText instance for safely adding $sanitisedQuery to template.
40+
*/
41+
public function sanitisedQuery(Query $query): DBText
42+
{
43+
return DBText::create()->setValue($query->getQueryString());
44+
}
45+
````
3446

3547
## `Record` class
3648

0 commit comments

Comments
 (0)