Permalink
Browse files

Add config array for configuration of StackAttack filters etc

  • Loading branch information...
1 parent 59cc331 commit 2a0a15303efe16c30839ac28ba740a9eb8dbb463 @dongilbert dongilbert committed Nov 1, 2013
Showing with 39 additions and 25 deletions.
  1. +0 −1 composer.json
  2. +30 −22 src/League/StackAttack/Attack.php
  3. +1 −1 src/League/StackAttack/FilterCollection.php
  4. +8 −1 tests/test.php
View
@@ -10,7 +10,6 @@
"require-dev" : {
"stack/builder" : "1.0.*@dev",
"stack/callable-http-kernel": "~1.0@dev",
- "stack/inline": "~1.0@dev",
"stack/run": "~1.0@dev"
},
"autoload": {
@@ -23,42 +23,50 @@ class Attack implements HttpKernelInterface
*/
private $filters;
- public function __construct(HttpKernelInterface $app, FilterCollection $filters)
+ public function __construct(HttpKernelInterface $app, FilterCollection $filters, array $config = array())
{
$this->app = $app;
$this->filters = $filters;
+ $this->config = $config;
- $this->setBlacklistedResponse();
+ $this->setBlacklistedResponse();
}
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
- // If this is not a whitelisted request, check the blacklist.
+ // If this is not a whitelisted request, check the blacklist.
if (! $this->whitelisted($request)) {
- if ($this->blacklisted($request)) {
- return call_user_func($this->blacklistedResponse, $request);
- }
+ if ($this->blacklisted($request)) {
+ return call_user_func($this->blacklistedResponse, $request);
+ }
}
return $this->app->handle($request, $type, $catch);
}
- public function setBlacklistedResponse(\Closure $fun = null)
- {
- if ($fun instanceof \Closure) {
- $this->blacklistedResponse = $fun;
- } else {
- $this->blacklistedResponse = function (Request $request) {
- $message = 'Unauthorized';
-
- if ($request->attributes->has('stack.attack.match_message')) {
- $message = $request->attributes->get('stack.attack.match_message');
- }
-
- return new Response($message, 401);
- };
- }
- }
+ public function setBlacklistedResponse(\Closure $func = null)
+ {
+ if ($func !== null) {
+ $this->blacklistedResponse = $func;
+ } elseif (isset($this->config['blacklistedResponse']) && ($this->config['blacklistedResponse'] instanceof \Closure)) {
+ $this->blacklistedResponse = $this->config['blacklistedResponse'];
+ } else {
+ $this->defaultBlacklistedResponse();
+ }
+ }
+
+ private function defaultBlacklistedResponse()
+ {
+ $this->blacklistedResponse = function (Request $request) {
+ $message = 'Unauthorized';
+
+ if ($request->attributes->has('stack.attack.match_message')) {
+ $message = $request->attributes->get('stack.attack.match_message');
+ }
+
+ return new Response($message, 401);
+ };
+ }
private function whitelisted(Request $request)
{
@@ -1,4 +1,4 @@
-git <?php
+<?php
namespace League\StackAttack;
View
@@ -17,8 +17,15 @@
return strpos($request->getPathInfo(), '/dev') === 0;
});
+$options = array(
+ 'blacklistedResponse' => function (Request $request) {
+ // A 503 response makes some bots think they had a successful DDOS
+ return new Response('Service Unavailable', 503);
+ }
+);
+
$app = (new Builder)
- ->push('League\\StackAttack\\Attack', $filters)
+ ->push('League\\StackAttack\\Attack', $filters, $options)
->resolve($app);
run($app);

0 comments on commit 2a0a153

Please sign in to comment.