|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test that recommendation titles created via the REST API are sanitized. |
| 4 | + * |
| 5 | + * Regression test for the stored XSS reported by Patchstack: an Editor (or |
| 6 | + * higher) could POST /wp/v2/prpl_recommendations with a `title` containing an |
| 7 | + * HTML payload (e.g. `<img src=x onerror=alert(1)>`) which was later rendered |
| 8 | + * unescaped in the admin dashboard. |
| 9 | + * |
| 10 | + * @package Progress_Planner\Tests |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Progress_Planner\Tests; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class Rest_Recommendations_Xss_Test |
| 17 | + */ |
| 18 | +class Rest_Recommendations_Xss_Test extends \WP_UnitTestCase { |
| 19 | + |
| 20 | + /** |
| 21 | + * Set up the REST server before each test. |
| 22 | + * |
| 23 | + * @return void |
| 24 | + */ |
| 25 | + public function setUp(): void { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + global $wp_rest_server; |
| 29 | + $wp_rest_server = new \WP_REST_Server(); |
| 30 | + \do_action( 'rest_api_init' ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Tear down the REST server after each test. |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function tearDown(): void { |
| 39 | + global $wp_rest_server; |
| 40 | + $wp_rest_server = null; |
| 41 | + parent::tearDown(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Submit a recommendation via the REST API and return the raw response. |
| 46 | + * |
| 47 | + * @param string $title The title to submit. |
| 48 | + * @return \WP_REST_Response The REST response. |
| 49 | + */ |
| 50 | + private function submit_recommendation_via_rest( $title ) { |
| 51 | + $request = new \WP_REST_Request( 'POST', '/wp/v2/prpl_recommendations' ); |
| 52 | + $request->set_header( 'Content-Type', 'application/json' ); |
| 53 | + $request->set_body( |
| 54 | + (string) \wp_json_encode( |
| 55 | + [ |
| 56 | + 'title' => $title, |
| 57 | + 'status' => 'publish', |
| 58 | + ] |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + return \rest_get_server()->dispatch( $request ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create a recommendation via the REST API and return the created WP_Post. |
| 67 | + * |
| 68 | + * @param string $title The title to submit. |
| 69 | + * @return \WP_Post The created post. |
| 70 | + */ |
| 71 | + private function create_recommendation_via_rest( $title ) { |
| 72 | + $response = $this->submit_recommendation_via_rest( $title ); |
| 73 | + $this->assertSame( 201, $response->get_status(), 'Recommendation should be created.' ); |
| 74 | + |
| 75 | + $data = $response->get_data(); |
| 76 | + return \get_post( $data['id'] ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * An Editor submitting a title that is purely an HTML payload must not result |
| 81 | + * in a stored post that contains the markup. |
| 82 | + * |
| 83 | + * Stripping the tags leaves an empty title, so WordPress rejects the request |
| 84 | + * outright (the malicious post is never created) - an acceptable, even |
| 85 | + * preferable, outcome. We assert that either nothing was stored, or if it was, |
| 86 | + * the markup is gone. |
| 87 | + * |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + public function test_editor_xss_title_is_stripped() { |
| 91 | + $editor_id = self::factory()->user->create( [ 'role' => 'editor' ] ); |
| 92 | + \wp_set_current_user( $editor_id ); |
| 93 | + |
| 94 | + $response = $this->submit_recommendation_via_rest( '<img src=x onerror=alert(1)>' ); |
| 95 | + |
| 96 | + if ( 201 === $response->get_status() ) { |
| 97 | + $post = \get_post( $response->get_data()['id'] ); |
| 98 | + $this->assertStringNotContainsString( '<img', $post->post_title ); |
| 99 | + $this->assertStringNotContainsString( 'onerror', $post->post_title ); |
| 100 | + $this->assertStringNotContainsString( '<', $post->post_title ); |
| 101 | + } else { |
| 102 | + // An empty title (after stripping) is rejected; no post is created. |
| 103 | + $this->assertSame( 400, $response->get_status() ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * A <script> payload from an Editor should be stripped from the stored title. |
| 109 | + * |
| 110 | + * @return void |
| 111 | + */ |
| 112 | + public function test_editor_script_title_is_stripped() { |
| 113 | + $editor_id = self::factory()->user->create( [ 'role' => 'editor' ] ); |
| 114 | + \wp_set_current_user( $editor_id ); |
| 115 | + |
| 116 | + $post = $this->create_recommendation_via_rest( '<script>alert(document.cookie)</script>Hello' ); |
| 117 | + |
| 118 | + $this->assertStringNotContainsString( '<script', $post->post_title ); |
| 119 | + $this->assertStringNotContainsString( '</script', $post->post_title ); |
| 120 | + // The plain-text remainder is preserved. |
| 121 | + $this->assertStringContainsString( 'Hello', $post->post_title ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Even an Administrator (who has the `unfiltered_html` capability) should |
| 126 | + * have HTML stripped from recommendation titles, since they are rendered as |
| 127 | + * plain text in JS templates. |
| 128 | + * |
| 129 | + * @return void |
| 130 | + */ |
| 131 | + public function test_admin_with_unfiltered_html_title_is_stripped() { |
| 132 | + $admin_id = self::factory()->user->create( [ 'role' => 'administrator' ] ); |
| 133 | + \wp_set_current_user( $admin_id ); |
| 134 | + |
| 135 | + $post = $this->create_recommendation_via_rest( '<img src=x onerror=alert(1)>Title' ); |
| 136 | + |
| 137 | + $this->assertStringNotContainsString( '<img', $post->post_title ); |
| 138 | + $this->assertStringNotContainsString( 'onerror', $post->post_title ); |
| 139 | + $this->assertStringContainsString( 'Title', $post->post_title ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Legitimate plain-text titles (including ampersands) must be preserved. |
| 144 | + * |
| 145 | + * @return void |
| 146 | + */ |
| 147 | + public function test_plain_text_title_is_preserved() { |
| 148 | + $editor_id = self::factory()->user->create( [ 'role' => 'editor' ] ); |
| 149 | + \wp_set_current_user( $editor_id ); |
| 150 | + |
| 151 | + $post = $this->create_recommendation_via_rest( 'Buy milk & eggs' ); |
| 152 | + |
| 153 | + $this->assertSame( 'Buy milk & eggs', $post->post_title ); |
| 154 | + } |
| 155 | +} |
0 commit comments