-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathOffloadingSniff.php
More file actions
166 lines (146 loc) · 5.34 KB
/
OffloadingSniff.php
File metadata and controls
166 lines (146 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* OffloadingSniff
*
* Based on code from {@link https://github.com/WordPress/WordPress-Coding-Standards}
* which is licensed under {@link https://opensource.org/licenses/MIT}.
*
* @package PluginCheck
*/
namespace PluginCheckCS\PluginCheck\Sniffs\CodeAnalysis;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Tokens\Collections;
use PHPCSUtils\Utils\TextStrings;
use PluginCheckCS\PluginCheck\Helpers\OffloadingServicesTrait;
use WordPressCS\WordPress\Sniff;
/**
* Verifies any images/styles/scripts are not loaded from external sources.
*
* @link https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/
*
* @since 1.1.0
*/
final class OffloadingSniff extends Sniff {
use OffloadingServicesTrait;
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register() {
$targets = Collections::textStringStartTokens();
$targets[] = \T_INLINE_HTML;
return $targets;
}
/**
* Processes this test, when one of its tokens is encountered.
*
* @param int $stackPtr The position of the current token in the stack.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process_token( $stackPtr ) {
$end_ptr = $stackPtr;
$content = $this->tokens[ $stackPtr ]['content'];
if ( \T_INLINE_HTML !== $this->tokens[ $stackPtr ]['code'] ) {
try {
$end_ptr = TextStrings::getEndOfCompleteTextString( $this->phpcsFile, $stackPtr );
$content = TextStrings::getCompleteTextString( $this->phpcsFile, $stackPtr );
} catch ( RuntimeException $e ) {
// Parse error/live coding.
return;
}
}
if ( empty( trim( $content ) ) ) {
return;
}
// Skip check if the string is within wp_enqueue_script/etc function calls,
// as EnqueuedResourceOffloadingSniff already handles those and we want to avoid double reporting.
if ( isset( $this->tokens[ $stackPtr ]['nested_parenthesis'] ) ) {
foreach ( $this->tokens[ $stackPtr ]['nested_parenthesis'] as $opener => $closer ) {
$prev = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $opener - 1 ), null, true );
if ( false !== $prev && \T_STRING === $this->tokens[ $prev ]['code'] ) {
$function_name = $this->tokens[ $prev ]['content'];
if ( in_array( $function_name, array( 'wp_enqueue_script', 'wp_enqueue_style', 'wp_register_script', 'wp_register_style' ), true ) ) {
return ( $end_ptr + 1 );
}
}
}
}
// First check: Always check against known offloading services pattern for all strings.
$pattern = $this->get_offloading_services_pattern();
$matches = array();
if ( preg_match_all( $pattern, $content, $matches, PREG_OFFSET_CAPTURE ) > 0 ) {
foreach ( $matches[0] as $match ) {
$this->phpcsFile->addError(
'Offloading images, js, css, and other scripts to your servers or any remote service is disallowed.',
$this->find_token_in_multiline_string( $stackPtr, $content, $match[1] ),
'OffloadedContent'
);
}
return ( $end_ptr + 1 );
}
// Second check: For HTML markup strings only, also check file extension-based patterns.
// This is limited to HTML context to avoid false positives on arbitrary URLs.
if (
false === strpos( $content, '<img' ) &&
false === strpos( $content, '<video' ) &&
false === strpos( $content, '<audio' ) &&
false === strpos( $content, '<source' ) &&
false === strpos( $content, '<link' ) &&
false === strpos( $content, '<script' )
) {
return ( $end_ptr + 1 );
}
// Known offloading extensions.
$look_known_offloading_ext = array(
'css',
'svg',
'jpg',
'jpeg',
'gif',
'png',
'webm',
'mp4',
'mpg',
'mpeg',
'mp3',
'json',
);
$offloading_ext = '\.' . implode( '|\.', $look_known_offloading_ext );
$pattern = '/(https?:\/\/[www\.]?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*(' . $offloading_ext . '){1})[\/]?([\?|#]{1}[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)?[\s|\'|"]/';
$matches = array();
if ( preg_match_all( $pattern, $content, $matches, PREG_OFFSET_CAPTURE ) > 0 ) {
foreach ( $matches[0] as $match ) {
$this->phpcsFile->addError(
'Offloading images, js, css, and other scripts to your servers or any remote service is disallowed.',
$this->find_token_in_multiline_string( $stackPtr, $content, $match[1] ),
'OffloadedContent'
);
}
}
return ( $end_ptr + 1 );
}
/**
* Find the exact token on which the error should be reported for multi-line strings.
*
* @param int $stackPtr The position of the current token in the stack.
* @param string $content The complete, potentially multi-line, text string.
* @param int $match_offset The offset within the content at which the match was found.
*
* @return int The stack pointer to the token containing the start of the match.
*/
private function find_token_in_multiline_string( $stackPtr, $content, $match_offset ) {
$newline_count = 0;
if ( $match_offset > 0 ) {
$newline_count = substr_count( $content, "\n", 0, $match_offset );
}
// Account for heredoc/nowdoc text starting at the token *after* the opener.
if ( isset( Tokens::$heredocTokens[ $this->tokens[ $stackPtr ]['code'] ] ) === true ) {
++$newline_count;
}
return ( $stackPtr + $newline_count );
}
}