|
| 1 | +<?php |
| 2 | + |
| 3 | +test('opengraph methods properly sanitize input', function (string $method, string $property) { |
| 4 | + $unsanitizedContent = 'Testing string " with several \' XSS characters </title> " . \' .'; |
| 5 | + |
| 6 | + seo()->{$method}($unsanitizedContent); |
| 7 | + |
| 8 | + $meta = meta(); |
| 9 | + |
| 10 | + $sanitizedContent = e($unsanitizedContent); |
| 11 | + |
| 12 | + // These assertions are equivalent, but included for clarity |
| 13 | + expect($meta)->not()->toContain('content="Testing string " with several \' XSS characters </title> " . \' ."'); |
| 14 | + expect($meta)->not()->toContain("content=\"{$unsanitizedContent}\""); |
| 15 | + |
| 16 | + expect($meta)->toContain("<meta property=\"$property\" content=\"{$sanitizedContent}\" />"); |
| 17 | + expect($meta)->toContain("<meta property=\"$property\" content=\"Testing string " with several ' XSS characters </title> " . ' .\" />"); |
| 18 | +})->with([ |
| 19 | + ['site', 'og:site_name'], |
| 20 | + ['url', 'og:url'], |
| 21 | + ['image', 'og:image'], |
| 22 | + ['type', 'og:type'], |
| 23 | + ['locale', 'og:locale'], |
| 24 | +]); |
| 25 | + |
| 26 | +// The Twitter integration is tested separately as it uses `meta name=""` instead of `meta property=""` |
| 27 | +test('the twitter extension properly sanitizes input', function (string $method, $property) { |
| 28 | + $unsanitizedContent = 'Testing string " with several \' XSS characters </title> " . \' .'; |
| 29 | + |
| 30 | + seo()->{$method}($unsanitizedContent); |
| 31 | + |
| 32 | + $meta = meta(); |
| 33 | + |
| 34 | + $sanitizedContent = e($unsanitizedContent); |
| 35 | + |
| 36 | + // These assertions are equivalent, but included for clarity |
| 37 | + expect($meta)->not()->toContain('content="Testing string " with several \' XSS characters </title> " . \' ."'); |
| 38 | + expect($meta)->not()->toContain("content=\"{$unsanitizedContent}\""); |
| 39 | + |
| 40 | + expect($meta)->toContain("<meta name=\"$property\" content=\"{$sanitizedContent}\" />"); |
| 41 | + expect($meta)->toContain("<meta name=\"$property\" content=\"Testing string " with several ' XSS characters </title> " . ' .\" />"); |
| 42 | +})->with([ |
| 43 | + ['twitterCreator', 'twitter:creator'], |
| 44 | + ['twitterSite', 'twitter:site'], |
| 45 | + ['twitterTitle', 'twitter:title'], |
| 46 | + ['twitterDescription', 'twitter:description'], |
| 47 | + ['twitterImage', 'twitter:image'], |
| 48 | +]); |
| 49 | + |
| 50 | +// This method is tested separately as it adds an extra (<title>) tag |
| 51 | +test('the title method properly sanitizes both tags', function () { |
| 52 | + $unsanitizedContent = 'Testing string " with several \' XSS characters </title> " . \' .'; |
| 53 | + |
| 54 | + seo()->title($unsanitizedContent); |
| 55 | + |
| 56 | + $meta = meta(); |
| 57 | + |
| 58 | + $sanitizedContent = e($unsanitizedContent); |
| 59 | + |
| 60 | + // These assertions are equivalent, but included for clarity |
| 61 | + expect($meta)->not()->toContain('meta property="og:title" content="Testing string " with several \' XSS characters </title> " . \' ."'); |
| 62 | + expect($meta)->not()->toContain('<title>Testing string " with several \' XSS characters </title> " . \' ."</title>'); |
| 63 | + expect($meta)->not()->toContain("meta property=\"og:title\" content=\"{$unsanitizedContent}\""); |
| 64 | + expect($meta)->not()->toContain("<title>{$unsanitizedContent}</title>"); |
| 65 | + |
| 66 | + expect($meta)->toContain("<title>{$sanitizedContent}</title>"); |
| 67 | + expect($meta)->toContain("<title>Testing string " with several ' XSS characters </title> " . ' .</title>"); |
| 68 | + expect($meta)->toContain("<meta property=\"og:title\" content=\"{$sanitizedContent}\" />"); |
| 69 | + expect($meta)->toContain("<meta property=\"og:title\" content=\"Testing string " with several ' XSS characters </title> " . ' .\" />"); |
| 70 | +}); |
| 71 | + |
| 72 | +test('seo blade directive calls are sanitized', function () { |
| 73 | + seo(['image' => $string = 'Testing string " with several \' XSS characters </title> " . \' .']); |
| 74 | + |
| 75 | + $escaped = e($string); |
| 76 | + |
| 77 | + // Using @seo() to get a value |
| 78 | + expect(blade('<img src="@seo(\'image\')">')) |
| 79 | + ->toBe("<img src=\"{$escaped}\">") |
| 80 | + ->not()->toBe('<img src="Testing string " with several \' XSS characters </title> " . \' ."'); |
| 81 | + |
| 82 | + // Using @seo() to set a value |
| 83 | + expect(blade("@seo('description', 'abc \' def &')"))->toBe('abc ' def &'); |
| 84 | +}); |
0 commit comments