Exemplo n.º 1
0
 protected function doValidation(Response $response)
 {
     $data = json_decode($response->getBody());
     if ($data === null) {
         throw new ValidationFailedException("The given JSON data can not be validated (last error: '" . $this->json_errors[json_last_error()] . "').");
     } else {
         $error = false;
         $messageParts = array();
         foreach ($this->jsonSchemaFiles as $jsonSchemaFile) {
             $factory = new Factory(null, null, Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_COERCE);
             $validator = new Validator($factory);
             $jsonSchemaObject = (object) json_decode(file_get_contents($jsonSchemaFile['jsonschemafileurl']));
             $validator->check($data, $jsonSchemaObject);
             if (!$validator->isValid()) {
                 $error = true;
                 $errorMessage = '';
                 foreach ($validator->getErrors() as $error) {
                     $errorMessage = $errorMessage . sprintf("[%s] %s\n", $error['property'], $error['message']);
                 }
                 $messageParts[] = $jsonSchemaFile['jsonschemafilename'] . ' - ' . $jsonSchemaFile['jsonschemafileurl'] . '(last error: ' . $errorMessage . ').';
             }
         }
         if ($error == true) {
             $message = 'JSON file (' . (string) $response->getUri() . ')  does not validate against the following JSON Schema files: ' . implode(", ", $messageParts);
             throw new ValidationFailedException($message);
         }
     }
 }
Exemplo n.º 2
0
 public function validate(Response $response)
 {
     if ($response->getContentType() !== 'application/javascript') {
         return;
     }
     $filename = $this->tmpDir . DIRECTORY_SEPARATOR . md5($response->getBody()) . '.js';
     file_put_contents($filename, $response->getBody());
     $conf = __DIR__ . DIRECTORY_SEPARATOR . 'jsHint.conf';
     $command = $this->jsHintExecutable . ' --config ' . $conf . ' --verbose ' . $filename . ' | grep -E E[0-9]+.$';
     $validationResult = shell_exec($command);
     unlink($filename);
     if (!is_null($validationResult)) {
         $errorMsg = str_replace($filename . ':', '', $validationResult);
         throw new ValidationFailedException('JavaScript error found: ' . $errorMsg);
     }
 }
Exemplo n.º 3
0
 public function doValidation(Response $response)
 {
     $result = json_decode($response->getBody());
     if ($result === null) {
         throw new ValidationFailedException("The given JSON data can not be validated (last error: '" . $this->json_errors[json_last_error()] . "').");
     }
 }
Exemplo n.º 4
0
 protected function doValidation(Response $response)
 {
     $crawler = new Crawler($response->getBody());
     $actionNodes = $crawler->filterXPath('//form[//input[@type="password"]]');
     $url = (string) $response->getUri();
     foreach ($actionNodes as $node) {
         $action = $node->getAttribute('action');
         if (strpos($action, 'https://') === 0) {
             continue;
         }
         $fullPath = $node->tagName;
         $parent = $node->parentNode;
         while ($parent = $parent->parentNode) {
             if (property_exists($parent, 'tagName')) {
                 $fullPath = $parent->tagName . '/' . $fullPath;
             } else {
                 break;
             }
         }
         if (in_array($fullPath, $this->knownIdentifier, true)) {
             continue;
         }
         $this->knownIdentifier[] = $fullPath;
         $this->assert(strpos($url, 'https://') !== false, 'Password is transferred insecure using HTTP.');
     }
 }
Exemplo n.º 5
0
 public function doValidation(Response $response)
 {
     $body = (string) $response->getBody();
     $json = json_decode($body);
     if (!$json) {
         throw new ValidationFailedException('The given json document is empty or not valid json.');
     }
     $store = new JsonStore($json);
     $error = false;
     $noCorrectJsonPaths = array();
     foreach ($this->jsonPaths as $path) {
         $jsonValue = $store->get($path['pattern']);
         $count = count($jsonValue);
         if ($jsonValue === FALSE || is_array($jsonValue) && empty($jsonValue)) {
             $error = true;
             $noCorrectJsonPaths[] = $path['pattern'] . ' (JSON Path not found)';
         }
         if ($this->checkRelation($path['relation'], $path['value'], $count) === false) {
             $error = true;
             $noCorrectJsonPaths[] = $path['pattern'] . ' (number of JSONPaths is not correct corresponding to the given relation/value)';
         }
     }
     if ($error === true) {
         $allNoCorrectJsonPaths = implode('", "', $noCorrectJsonPaths);
         throw new ValidationFailedException('Disonances with JSON Paths "' . $allNoCorrectJsonPaths . '!');
     }
 }
Exemplo n.º 6
0
 public function validate(Response $response)
 {
     if (($response->getStatus() < 300 || $response->getStatus() >= 400) && $response->getContentType() === 'text/html') {
         if (stripos($response->getBody(), '</html>') === false) {
             throw new ValidationFailedException('Closing html tag is missing');
         }
     }
 }
Exemplo n.º 7
0
 public function doValidation(Response $response)
 {
     if (strpos($response->getContentType(), 'image') === false && strpos($response->getContentType(), 'pdf') === false && strlen((string) $response->getBody()) >= $this->minFileSize) {
         if (!$response->hasHeader('Content-Encoding') || $response->getHeader('Content-Encoding')[0] !== 'gzip') {
             throw new ValidationFailedException('gzip compression not active');
         }
     }
 }
Exemplo n.º 8
0
 protected function doValidation(Response $response)
 {
     if (strpos((string) $response->getUri(), 'favicon.ico') === false) {
         return;
     }
     $imageHash = md5($response->getBody());
     $this->assert(!array_key_exists($imageHash, $this->favicons), 'Seems like you use the standard favicon of your framework (' . $this->favicons[$imageHash] . ').');
 }
Exemplo n.º 9
0
 public function doValidation(Response $response)
 {
     $domDocument = new \DOMDocument();
     $success = @$domDocument->loadXML((string) $response->getBody());
     if (!$success) {
         $lastError = libxml_get_last_error();
         throw new ValidationFailedException('The xml file ' . $response->getUri() . ' is not well formed (last error: ' . str_replace("\n", '', $lastError->message) . ').');
     }
 }
Exemplo n.º 10
0
 protected function doValidation(Response $response)
 {
     $body = $response->getBody();
     // sitemapindex or urlset
     if (preg_match('/<sitemapindex/', $body)) {
         $this->validateBody($body, (string) $response->getUri());
     } elseif (preg_match('/<urlset/', $body)) {
         $this->validateBody($body, (string) $response->getUri(), false);
     }
 }
Exemplo n.º 11
0
 protected function doValidation(Response $response)
 {
     if ($response->getStatus() >= 300) {
         return;
     }
     $crawler = new Crawler($response->getBody());
     $metaTags = $crawler->filterXPath("//meta[@name='robots']/@content");
     foreach ($metaTags as $metaTag) {
         $this->assert(strpos($metaTag->nodeValue, 'no-index') === false, 'A meta tag "robots" with the value "no-index" was found');
     }
 }
Exemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 protected function doValidation(Response $response)
 {
     $document = new Document($response->getBody(), false);
     $urls = $document->getDependencies($response->getUri());
     $invalidUrls = array();
     foreach ($urls as $url) {
         if (!filter_var(trim((string) $url), FILTER_VALIDATE_URL)) {
             $invalidUrls[] = (string) $url;
         }
     }
     $this->assert(count($invalidUrls) === 0, 'Invalid urls found (' . implode(', ', $invalidUrls) . ').');
 }
Exemplo n.º 13
0
 public function validate(Response $response)
 {
     $request = $response->getRequest();
     if ('https' !== $request->getUri()->getScheme()) {
         return;
     }
     $htmlDocument = new Document($response->getBody());
     $resources = $htmlDocument->getDependencies($response->getRequest()->getUri(), false);
     foreach ($resources as $resource) {
         if ($resource->getScheme() && 'https' !== $resource->getScheme()) {
             throw new ValidationFailedException('At least one dependency was found on a secure url, that was transfered insecure (' . (string) $resource . ')');
         }
     }
 }
Exemplo n.º 14
0
 protected function doValidation(Response $response)
 {
     $document = new Document($response->getBody());
     $images = $document->getImages($response->getUri());
     $foreignImages = array();
     /* @var $currentUri Uri */
     $currentUri = $response->getUri();
     foreach ($images as $image) {
         /* @var $image Uri */
         if ($currentUri->getHost($this->depth) !== $image->getHost($this->depth)) {
             $foreignImages[] = (string) $image;
         }
     }
     $this->assert(count($foreignImages) === 0, 'Images from a foreign domain where found (' . implode(', ', $foreignImages) . ')');
 }
Exemplo n.º 15
0
 public function testResponse()
 {
     $testBody = 'TestBodyWith<strong>special</strong>Chäräctörs';
     $testHeader = HeadersParser::parse(['Header1' => 'Test Header']);
     $testStatus = 200;
     $testUri = new Uri('http://smoke.phmlabs.com');
     $testRequest = new Request($testUri);
     $stream = fopen('data://text/plain,' . $testBody, 'r');
     $response = new Response($stream, $testStatus, array(), ['request' => $testRequest]);
     $this->assertEquals($testBody, $response->getBody());
     $this->assertEquals([], $response->getHeader('Test Header'));
     $this->assertEquals($testStatus, $response->getStatus());
     $this->assertEmpty($response->getContentType());
     $this->assertEquals($testRequest, $response->getRequest());
 }
Exemplo n.º 16
0
 public function doValidation(Response $response)
 {
     $domDocument = new \DOMDocument();
     @$domDocument->loadHTML((string) $response->getBody());
     $domXPath = new \DOMXPath($domDocument);
     $error = false;
     $snotFoundSelectors = array();
     foreach ($this->cssSelectors as $selector) {
         $converter = new CssSelectorConverter();
         $selectorAsXPath = $converter->toXPath($selector['pattern']);
         $count = $domXPath->query($selectorAsXPath)->length;
         if ($count === 0) {
             $error = true;
             $snotFoundSelectors[] = $selector['pattern'];
         }
     }
     if ($error === true) {
         $allNotFoundSelectors = implode('", "', $snotFoundSelectors);
         throw new ValidationFailedException('CSS Selector "' . $allNotFoundSelectors . '" not found in DOM.');
     }
 }
Exemplo n.º 17
0
 protected function doValidation(Response $response)
 {
     $body = $response->getBody();
     $dom = new \DOMDocument();
     @$dom->loadXML($body);
     $filename = (string) $response->getUri();
     $error = false;
     $messageParts = array();
     foreach ($this->xsdFiles as $xsdFile) {
         $valid = @$dom->schemaValidate($xsdFile['xsdfileurl']);
         if (!$valid) {
             $error = true;
             $lastError = libxml_get_last_error();
             $messageParts[] = $xsdFile['xsdfilename'] . ' - ' . $xsdFile['xsdfileurl'] . '(last error: ' . str_replace("\n", '', $lastError->message) . ').';
         }
     }
     if ($error === true) {
         $message = 'XML file (' . $filename . ')  does not validate against the following XSD files: ' . implode(", ", $messageParts);
         throw new ValidationFailedException($message);
     }
 }
Exemplo n.º 18
0
 public function validate(Response $response)
 {
     if ($response->getContentType() !== 'text/xml') {
         return;
     }
     $body = $response->getBody();
     if (preg_match('/<rss/', $body)) {
         libxml_clear_errors();
         $dom = new \DOMDocument();
         @$dom->loadXML($body);
         $lastError = libxml_get_last_error();
         if ($lastError) {
             throw new ValidationFailedException('The given xml file is not well formed (last error: ' . str_replace("\n", '', $lastError->message) . ').');
         }
         $valid = @$dom->schemaValidate($this->getSchema());
         if (!$valid) {
             $lastError = libxml_get_last_error();
             $lastErrorMessage = str_replace("\n", '', $lastError->message);
             throw new ValidationFailedException('The given xml file did not Validate vs. ' . $this->getSchema() . ' (last error: ' . $lastErrorMessage . ').');
         }
     }
 }
Exemplo n.º 19
0
 public function validate(Response $response)
 {
     if ($response->getContentType() !== 'text/html') {
         return;
     }
     $crawler = new Crawler($response->getBody());
     $idList = $crawler->filterXPath('//*[@id!=""]');
     $foundIds = array();
     $duplicatedIds = array();
     foreach ($idList as $idElement) {
         $id = $idElement->getAttribute('id');
         if (array_key_exists($id, $foundIds)) {
             $duplicatedIds[$id] = true;
         } else {
             $foundIds[$id] = true;
         }
     }
     if (count($duplicatedIds) > 0) {
         unset($duplicatedIds['']);
         throw new ValidationFailedException('Duplicate ids found (' . implode(', ', array_keys($duplicatedIds)) . ')');
     }
 }
Exemplo n.º 20
0
 public function doValidation(Response $response)
 {
     $domDocument = new \DOMDocument();
     @$domDocument->loadHTML((string) $response->getBody());
     $domXPath = new \DOMXPath($domDocument);
     foreach ($this->xPaths as $xpath) {
         $count = $domXPath->query($xpath['pattern'])->length;
         if ($xpath['relation'] === 'equals') {
             $result = $count === (int) $xpath['value'];
             $message = 'The xpath "' . $xpath['pattern'] . '" was found ' . $count . ' times. Expected were exact ' . $xpath['value'] . ' occurencies.';
         } elseif ($xpath['relation'] === 'less than') {
             $result = $count < (int) $xpath['value'];
             $message = 'The xpath "' . $xpath['pattern'] . '" was found ' . $count . ' times. Expected were less than ' . $xpath['value'] . '.';
         } elseif ($xpath['relation'] === 'greater than') {
             $result = $count > (int) $xpath['value'];
             $message = 'The xpath "' . $xpath['pattern'] . '" was found ' . $count . ' times. Expected were more than ' . $xpath['value'] . '.';
         } else {
             throw new \RuntimeException('Relation not defined. Given "' . $xpath['relation'] . '" expected [equals, greater than, less than]');
         }
         $this->assert($result, $message);
     }
 }
Exemplo n.º 21
0
 protected function doValidation(Response $response)
 {
     foreach ($this->regExs as $regEx) {
         $this->assert(preg_match('^' . $regEx . '^', (string) $response->getBody()) > 0, 'The given regular expression (' . $regEx . ') was not found in this document.');
     }
 }
Exemplo n.º 22
0
 protected function doValidation(Response $response)
 {
     $document = new Document($response->getBody());
     $files = $this->getFilesToCount($document, $response);
     $this->assert(count($files) <= $this->maxCount, sprintf($this->errorMessage, count($files)));
 }
Exemplo n.º 23
0
 protected function doValidation(Response $response)
 {
     $this->assert(strpos($response->getBody(), $this->string) !== false, 'The given string (' . $this->string . ') was not found in this document.');
 }
Exemplo n.º 24
0
 protected function doValidation(Response $response)
 {
     $size = strlen($response->getBody()) / 1000;
     $this->assert($size <= $this->maxSize, 'The size of this html file is too big (' . $size . ' KB)');
 }