MediaWiki REL1_28
LinkFilterTest.php
Go to the documentation of this file.
1<?php
2
7
8 protected function setUp() {
9
10 parent::setUp();
11
12 $this->setMwGlobals( 'wgUrlProtocols', [
13 'http://',
14 'https://',
15 'ftp://',
16 'irc://',
17 'ircs://',
18 'gopher://',
19 'telnet://',
20 'nntp://',
21 'worldwind://',
22 'mailto:',
23 'news:',
24 'svn://',
25 'git://',
26 'mms://',
27 '//',
28 ] );
29
30 }
31
40 function createRegexFromLIKE( $like ) {
41
42 $regex = '!^';
43
44 foreach ( $like as $item ) {
45
46 if ( $item instanceof LikeMatch ) {
47 if ( $item->toString() == '%' ) {
48 $regex .= '.*';
49 } elseif ( $item->toString() == '_' ) {
50 $regex .= '.';
51 }
52 } else {
53 $regex .= preg_quote( $item, '!' );
54 }
55
56 }
57
58 $regex .= '$!';
59
60 return $regex;
61
62 }
63
69 public static function provideValidPatterns() {
70
71 return [
72 // Protocol, Search pattern, URL which matches the pattern
73 [ 'http://', '*.test.com', 'http://www.test.com' ],
74 [ 'http://', 'test.com:8080/dir/file', 'http://name:pass@test.com:8080/dir/file' ],
75 [ 'https://', '*.com', 'https://s.s.test..com:88/dir/file?a=1&b=2' ],
76 [ 'https://', '*.com', 'https://name:pass@secure.com/index.html' ],
77 [ 'http://', 'name:pass@test.com', 'http://test.com' ],
78 [ 'http://', 'test.com', 'http://name:pass@test.com' ],
79 [ 'http://', '*.test.com', 'http://a.b.c.test.com/dir/dir/file?a=6' ],
80 [ null, 'http://*.test.com', 'http://www.test.com' ],
81 [ 'mailto:', 'name@mail.test123.com', 'mailto:name@mail.test123.com' ],
82 [ '',
83 'http://name:pass@www.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg',
84 'http://name:pass@www.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg'
85 ],
86 [ '', 'http://name:pass@*.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg',
87 'http://name:pass@www.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg' ],
88 [ '', 'http://name:wrongpass@*.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]',
89 'http://name:pass@www.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg' ],
90 [ 'http://', 'name:pass@*.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg',
91 'http://name:pass@www.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg' ],
92 [ '', 'http://name:pass@www.test.com:12345',
93 'http://name:pass@www.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg' ],
94 [ 'ftp://', 'user:pass@ftp.test.com:1233/home/user/file;type=efw',
95 'ftp://user:pass@ftp.test.com:1233/home/user/file;type=efw' ],
96 [ null, 'ftp://otheruser:otherpass@ftp.test.com:1233/home/user/file;type=',
97 'ftp://user:pass@ftp.test.com:1233/home/user/file;type=efw' ],
98 [ null, 'ftp://@ftp.test.com:1233/home/user/file;type=',
99 'ftp://user:pass@ftp.test.com:1233/home/user/file;type=efw' ],
100 [ null, 'ftp://ftp.test.com/',
101 'ftp://user:pass@ftp.test.com/home/user/file;type=efw' ],
102 [ null, 'ftp://ftp.test.com/',
103 'ftp://user:pass@ftp.test.com/home/user/file;type=efw' ],
104 [ null, 'ftp://*.test.com:222/',
105 'ftp://user:pass@ftp.test.com:222/home' ],
106 [ 'irc://', '*.myserver:6667/', 'irc://test.myserver:6667/' ],
107 [ 'irc://', 'name:pass@*.myserver/', 'irc://test.myserver:6667/' ],
108 [ 'irc://', 'name:pass@*.myserver/', 'irc://other:@test.myserver:6667/' ],
109 [ '', 'irc://test/name,string,abc?msg=t', 'irc://test/name,string,abc?msg=test' ],
110 [ '', 'https://gerrit.wikimedia.org/r/#/q/status:open,n,z',
111 'https://gerrit.wikimedia.org/r/#/q/status:open,n,z' ],
112 [ '', 'https://gerrit.wikimedia.org',
113 'https://gerrit.wikimedia.org/r/#/q/status:open,n,z' ],
114 [ 'mailto:', '*.test.com', 'mailto:name@pop3.test.com' ],
115 [ 'mailto:', 'test.com', 'mailto:name@test.com' ],
116 [ 'news:', 'test.1234afc@news.test.com', 'news:test.1234afc@news.test.com' ],
117 [ 'news:', '*.test.com', 'news:test.1234afc@news.test.com' ],
118 [ '', 'news:4df8kh$iagfewewf(at)newsbf02aaa.news.aol.com',
119 'news:4df8kh$iagfewewf(at)newsbf02aaa.news.aol.com' ],
120 [ '', 'news:*.aol.com',
121 'news:4df8kh$iagfewewf(at)newsbf02aaa.news.aol.com' ],
122 [ '', 'git://github.com/prwef/abc-def.git', 'git://github.com/prwef/abc-def.git' ],
123 [ 'git://', 'github.com/', 'git://github.com/prwef/abc-def.git' ],
124 [ 'git://', '*.github.com/', 'git://a.b.c.d.e.f.github.com/prwef/abc-def.git' ],
125 [ '', 'gopher://*.test.com/', 'gopher://gopher.test.com/0/v2/vstat' ],
126 [ 'telnet://', '*.test.com', 'telnet://shell.test.com/~home/' ],
127 [ '', 'http://test.com', 'http://test.com/index?arg=1' ],
128 [ 'http://', '*.test.com', 'http://www.test.com/index?arg=1' ],
129 [ '' ,
130 'http://xx23124:__ffdfdef__@www.test.com:12345/dir' ,
131 'http://name:pass@www.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg'
132 ],
133
134 // Tests for false positives
135 [ 'http://', 'test.com', 'http://www.test.com', false ],
136 [ 'http://', 'www1.test.com', 'http://www.test.com', false ],
137 [ 'http://', '*.test.com', 'http://www.test.t.com', false ],
138 [ '', 'http://test.com:8080', 'http://www.test.com:8080', false ],
139 [ '', 'https://test.com', 'http://test.com', false ],
140 [ '', 'http://test.com', 'https://test.com', false ],
141 [ 'http://', 'http://test.com', 'http://test.com', false ],
142 [ null, 'http://www.test.com', 'http://www.test.com:80', false ],
143 [ null, 'http://www.test.com:80', 'http://www.test.com', false ],
144 [ null, 'http://*.test.com:80', 'http://www.test.com', false ],
145 [ '', 'https://gerrit.wikimedia.org/r/#/XXX/status:open,n,z',
146 'https://gerrit.wikimedia.org/r/#/q/status:open,n,z', false ],
147 [ '', 'https://*.wikimedia.org/r/#/q/status:open,n,z',
148 'https://gerrit.wikimedia.org/r/#/XXX/status:open,n,z', false ],
149 [ 'mailto:', '@test.com', '@abc.test.com', false ],
150 [ 'mailto:', 'mail@test.com', 'mail2@test.com', false ],
151 [ '', 'mailto:mail@test.com', 'mail2@test.com', false ],
152 [ '', 'mailto:@test.com', '@abc.test.com', false ],
153 [ 'ftp://', '*.co', 'ftp://www.co.uk', false ],
154 [ 'ftp://', '*.co', 'ftp://www.co.m', false ],
155 [ 'ftp://', '*.co/dir/', 'ftp://www.co/dir2/', false ],
156 [ 'ftp://', 'www.co/dir/', 'ftp://www.co/dir2/', false ],
157 [ 'ftp://', 'test.com/dir/', 'ftp://test.com/', false ],
158 [ '', 'http://test.com:8080/dir/', 'http://test.com:808/dir/', false ],
159 [ '', 'http://test.com/dir/index.html', 'http://test.com/dir/index.php', false ],
160
161 // These are false positives too and ideally shouldn't match, but that
162 // would require using regexes and RLIKE instead of LIKE
163 // [ null, 'http://*.test.com', 'http://www.test.com:80', false ],
164 // [ '', 'https://*.wikimedia.org/r/#/q/status:open,n,z',
165 // 'https://gerrit.wikimedia.org/XXX/r/#/q/status:open,n,z', false ],
166 ];
167
168 }
169
183 function testMakeLikeArrayWithValidPatterns( $protocol, $pattern, $url, $shouldBeFound = true ) {
184
185 $indexes = wfMakeUrlIndexes( $url );
186 $likeArray = LinkFilter::makeLikeArray( $pattern, $protocol );
187
188 $this->assertTrue( $likeArray !== false,
189 "LinkFilter::makeLikeArray('$pattern', '$protocol') returned false on a valid pattern"
190 );
191
192 $regex = $this->createRegexFromLIKE( $likeArray );
193 $debugmsg = "Regex: '" . $regex . "'\n";
194 $debugmsg .= count( $indexes ) . " index(es) created by wfMakeUrlIndexes():\n";
195
196 $matches = 0;
197
198 foreach ( $indexes as $index ) {
199 $matches += preg_match( $regex, $index );
200 $debugmsg .= "\t'$index'\n";
201 }
202
203 if ( $shouldBeFound ) {
204 $this->assertTrue(
205 $matches > 0,
206 "Search pattern '$protocol$pattern' does not find url '$url' \n$debugmsg"
207 );
208 } else {
209 $this->assertFalse(
210 $matches > 0,
211 "Search pattern '$protocol$pattern' should not find url '$url' \n$debugmsg"
212 );
213 }
214
215 }
216
222 public static function provideInvalidPatterns() {
223
224 return [
225 [ '' ],
226 [ '*' ],
227 [ 'http://*' ],
228 [ 'http://*/' ],
229 [ 'http://*/dir/file' ],
230 [ 'test.*.com' ],
231 [ 'http://test.*.com' ],
232 [ 'test.*.com' ],
233 [ 'http://*.test.*' ],
234 [ 'http://*test.com' ],
235 [ 'https://*' ],
236 [ '*://test.com' ],
237 [ 'mailto:name:pass@t*est.com' ],
238 [ 'http://*:888/' ],
239 [ '*http://' ],
240 [ 'test.com/*/index' ],
241 [ 'test.com/dir/index?arg=*' ],
242 ];
243
244 }
245
256
257 $this->assertFalse(
258 LinkFilter::makeLikeArray( $pattern ),
259 "'$pattern' is not a valid pattern and should be rejected"
260 );
261
262 }
263
264}
wfMakeUrlIndexes( $url)
Make URL indexes, appropriate for the el_index field of externallinks.
Used by Database::buildLike() to represent characters that have special meaning in SQL LIKE clauses a...
Definition LikeMatch.php:7
createRegexFromLIKE( $like)
createRegexFromLike($like)
static provideValidPatterns()
provideValidPatterns()
testMakeLikeArrayWithValidPatterns( $protocol, $pattern, $url, $shouldBeFound=true)
testMakeLikeArrayWithValidPatterns()
static provideInvalidPatterns()
provideInvalidPatterns()
testMakeLikeArrayWithInvalidPatterns( $pattern)
testMakeLikeArrayWithInvalidPatterns()
static makeLikeArray( $filterEntry, $protocol='http://')
Make an array to be used for calls to Database::buildLike(), which will match the specified string.
Base class that store and restore the Language objects.
setMwGlobals( $pairs, $value=null)
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
processing should stop and the error should be shown to the user * false
Definition hooks.txt:189
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37