Skip to content

Commit 4ba0281

Browse files
committed
AOS-100 Add query sanitisation example
1 parent 3eed8db commit 4ba0281

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

docs/detailed-result-handling.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,21 @@ The `Results` class is also a `ViewableData` object, so these methods can be acc
2727
## `Query` class
2828
The `Query` class provides the original query that was used for matching results.
2929

30-
**Important:** If you need to include the search term on the page (for example, `Showing results for "test"`) you will
31-
need to handle sanitisation of this value to mitigate against cross-site scripting (xss) attacks.
30+
> [!IMPORTANT]
31+
> If you need to include the search term on the page (for example, `Showing results for "test"`) you will
32+
> need to handle sanitisation of this value to mitigate against cross-site scripting (xss) attacks. The simplest
33+
> way of doing this is to create a custom function that returns a DBText field, and the Silverstripe templating
34+
> system will handle this for you (see example below).
35+
36+
````php
37+
/**
38+
* Wraps the raw query string in a DBText instance for safely adding $sanitisedQuery to template.
39+
*/
40+
public function sanitisedQuery(Query $query): DBText
41+
{
42+
return DBText::create()->setValue($query->getQueryString());
43+
}
44+
````
3245

3346
## `Record` class
3447

0 commit comments

Comments
 (0)