Skip to content

Commit b08d1b3

Browse files
committed
Search: Set limits on the amount of search terms
Sets some reasonable limits, which are higher when logged in since that infers a little extra trust. Helps prevent against large resource consuption attacks via super heavy search queries. Thanks to Gabriel Rodrigues AKA TEXUGO for reporting.
1 parent 88d86df commit b08d1b3

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

app/Search/SearchController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ public function searchForSelector(Request $request, QueryPopular $queryPopular)
7878

7979
// Search for entities otherwise show most popular
8080
if ($searchTerm !== false) {
81-
$searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
82-
$entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20)['results'];
81+
$options = SearchOptions::fromString($searchTerm);
82+
$options->setFilter('type', implode('|', $entityTypes));
83+
$entities = $this->searchRunner->searchEntities($options, 'all', 1, 20)['results'];
8384
} else {
8485
$entities = $queryPopular->run(20, 0, $entityTypes);
8586
}

app/Search/SearchOptionSet.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,12 @@ public function nonNegated(): self
8282
$values = array_values(array_filter($this->options, fn (SearchOption $option) => !$option->negated));
8383
return new self($values);
8484
}
85+
86+
/**
87+
* @return self<T>
88+
*/
89+
public function limit(int $limit): self
90+
{
91+
return new self(array_slice(array_values($this->options), 0, $limit));
92+
}
8593
}

app/Search/SearchOptions.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static function fromString(string $search): self
3535
{
3636
$instance = new self();
3737
$instance->addOptionsFromString($search);
38+
$instance->limitOptions();
3839
return $instance;
3940
}
4041

@@ -87,6 +88,8 @@ public static function fromRequest(Request $request): self
8788
$instance->filters = $instance->filters->merge($extras->filters);
8889
}
8990

91+
$instance->limitOptions();
92+
9093
return $instance;
9194
}
9295

@@ -147,6 +150,25 @@ protected function addOptionsFromString(string $searchString): void
147150
$this->filters = $this->filters->merge(new SearchOptionSet($terms['filters']));
148151
}
149152

153+
/**
154+
* Limit the amount of search options to reasonable levels.
155+
* Provides higher limits to logged-in users since that signals a slightly
156+
* higher level of trust.
157+
*/
158+
protected function limitOptions(): void
159+
{
160+
$userLoggedIn = !user()->isGuest();
161+
$searchLimit = $userLoggedIn ? 10 : 5;
162+
$exactLimit = $userLoggedIn ? 4 : 2;
163+
$tagLimit = $userLoggedIn ? 8 : 4;
164+
$filterLimit = $userLoggedIn ? 10 : 5;
165+
166+
$this->searches = $this->searches->limit($searchLimit);
167+
$this->exacts = $this->exacts->limit($exactLimit);
168+
$this->tags = $this->tags->limit($tagLimit);
169+
$this->filters = $this->filters->limit($filterLimit);
170+
}
171+
150172
/**
151173
* Decode backslash escaping within the input string.
152174
*/

tests/Search/SearchOptionsTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,53 @@ public function test_from_request_properly_parses_out_extras_as_string()
142142
$this->assertEquals('dino', $options->exacts->all()[0]->value);
143143
$this->assertTrue($options->exacts->all()[0]->negated);
144144
}
145+
146+
public function test_from_string_results_are_count_limited_and_larger_for_logged_in_users()
147+
{
148+
$terms = [
149+
...array_fill(0, 40, 'cat'),
150+
...array_fill(0, 50, '"bees"'),
151+
...array_fill(0, 50, '{is_template}'),
152+
...array_fill(0, 50, '[a=b]'),
153+
];
154+
155+
$options = SearchOptions::fromString(implode(' ', $terms));
156+
157+
$this->assertCount(5, $options->searches->all());
158+
$this->assertCount(2, $options->exacts->all());
159+
$this->assertCount(4, $options->tags->all());
160+
$this->assertCount(5, $options->filters->all());
161+
162+
$this->asEditor();
163+
$options = SearchOptions::fromString(implode(' ', $terms));
164+
165+
$this->assertCount(10, $options->searches->all());
166+
$this->assertCount(4, $options->exacts->all());
167+
$this->assertCount(8, $options->tags->all());
168+
$this->assertCount(10, $options->filters->all());
169+
}
170+
171+
public function test_from_request_results_are_count_limited_and_larger_for_logged_in_users()
172+
{
173+
$request = new Request([
174+
'search' => str_repeat('hello ', 20),
175+
'tags' => array_fill(0, 20, 'a=b'),
176+
'extras' => str_repeat('-[b=c] -{viewed_by_me} -"dino"', 20),
177+
]);
178+
179+
$options = SearchOptions::fromRequest($request);
180+
181+
$this->assertCount(5, $options->searches->all());
182+
$this->assertCount(2, $options->exacts->all());
183+
$this->assertCount(4, $options->tags->all());
184+
$this->assertCount(5, $options->filters->all());
185+
186+
$this->asEditor();
187+
$options = SearchOptions::fromRequest($request);
188+
189+
$this->assertCount(10, $options->searches->all());
190+
$this->assertCount(4, $options->exacts->all());
191+
$this->assertCount(8, $options->tags->all());
192+
$this->assertCount(10, $options->filters->all());
193+
}
145194
}

0 commit comments

Comments
 (0)