MediaWiki  1.23.2
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', array(
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 array(
72  // Protocol, Search pattern, URL which matches the pattern
73  array( 'http://', '*.test.com', 'http://www.test.com' ),
74  array( 'http://', 'test.com:8080/dir/file', 'http://name:pass@test.com:8080/dir/file' ),
75  array( 'https://', '*.com', 'https://s.s.test..com:88/dir/file?a=1&b=2' ),
76  array( 'https://', '*.com', 'https://name:pass@secure.com/index.html' ),
77  array( 'http://', 'name:pass@test.com', 'http://test.com' ),
78  array( 'http://', 'test.com', 'http://name:pass@test.com' ),
79  array( 'http://', '*.test.com', 'http://a.b.c.test.com/dir/dir/file?a=6'),
80  array( null, 'http://*.test.com', 'http://www.test.com' ),
81  array( 'mailto:', 'name@mail.test123.com', 'mailto:name@mail.test123.com' ),
82  array( '',
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  array( '', '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  array( '', '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  array( '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  array( '', '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  array( '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  array( 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  array( null, 'ftp://@ftp.test.com:1233/home/user/file;type=',
99  'ftp://user:pass@ftp.test.com:1233/home/user/file;type=efw' ),
100  array( null, 'ftp://ftp.test.com/',
101  'ftp://user:pass@ftp.test.com/home/user/file;type=efw' ),
102  array( null, 'ftp://ftp.test.com/',
103  'ftp://user:pass@ftp.test.com/home/user/file;type=efw' ),
104  array( null, 'ftp://*.test.com:222/',
105  'ftp://user:pass@ftp.test.com:222/home' ),
106  array( 'irc://', '*.myserver:6667/', 'irc://test.myserver:6667/' ),
107  array( 'irc://', 'name:pass@*.myserver/', 'irc://test.myserver:6667/' ),
108  array( 'irc://', 'name:pass@*.myserver/', 'irc://other:@test.myserver:6667/' ),
109  array( '', 'irc://test/name,string,abc?msg=t', 'irc://test/name,string,abc?msg=test' ),
110  array( '', 'https://gerrit.wikimedia.org/r/#/q/status:open,n,z',
111  'https://gerrit.wikimedia.org/r/#/q/status:open,n,z' ),
112  array( '', 'https://gerrit.wikimedia.org',
113  'https://gerrit.wikimedia.org/r/#/q/status:open,n,z' ),
114  array( 'mailto:', '*.test.com', 'mailto:name@pop3.test.com' ),
115  array( 'mailto:', 'test.com', 'mailto:name@test.com' ),
116  array( 'news:', 'test.1234afc@news.test.com', 'news:test.1234afc@news.test.com' ),
117  array( 'news:', '*.test.com', 'news:test.1234afc@news.test.com' ),
118  array( '', 'news:4df8kh$iagfewewf(at)newsbf02aaa.news.aol.com',
119  'news:4df8kh$iagfewewf(at)newsbf02aaa.news.aol.com' ),
120  array( '', 'news:*.aol.com',
121  'news:4df8kh$iagfewewf(at)newsbf02aaa.news.aol.com' ),
122  array( '', 'git://github.com/prwef/abc-def.git', 'git://github.com/prwef/abc-def.git' ),
123  array( 'git://', 'github.com/', 'git://github.com/prwef/abc-def.git' ),
124  array( 'git://', '*.github.com/', 'git://a.b.c.d.e.f.github.com/prwef/abc-def.git' ),
125  array( '', 'gopher://*.test.com/', 'gopher://gopher.test.com/0/v2/vstat'),
126  array( 'telnet://', '*.test.com', 'telnet://shell.test.com/~home/'),
127 
128  //
129  // The following only work in PHP >= 5.3.7, due to a bug in parse_url which eats
130  // the path from the url (https://bugs.php.net/bug.php?id=54180)
131  //
132  // array( '', 'http://test.com', 'http://test.com/index?arg=1' ),
133  // array( 'http://', '*.test.com', 'http://www.test.com/index?arg=1' ),
134  // array( '' ,
135  // 'http://xx23124:__ffdfdef__@www.test.com:12345/dir' ,
136  // 'http://name:pass@www.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg'
137  // ),
138  //
139 
140  //
141  // Tests for false positives
142  //
143  array( 'http://', 'test.com', 'http://www.test.com', false ),
144  array( 'http://', 'www1.test.com', 'http://www.test.com', false ),
145  array( 'http://', '*.test.com', 'http://www.test.t.com', false ),
146  array( '', 'http://test.com:8080', 'http://www.test.com:8080', false ),
147  array( '', 'https://test.com', 'http://test.com', false ),
148  array( '', 'http://test.com', 'https://test.com', false ),
149  array( 'http://', 'http://test.com', 'http://test.com', false ),
150  array( null, 'http://www.test.com', 'http://www.test.com:80', false ),
151  array( null, 'http://www.test.com:80', 'http://www.test.com', false ),
152  array( null, 'http://*.test.com:80', 'http://www.test.com', false ),
153  array( '', 'https://gerrit.wikimedia.org/r/#/XXX/status:open,n,z',
154  'https://gerrit.wikimedia.org/r/#/q/status:open,n,z', false ),
155  array( '', 'https://*.wikimedia.org/r/#/q/status:open,n,z',
156  'https://gerrit.wikimedia.org/r/#/XXX/status:open,n,z', false ),
157  array( 'mailto:', '@test.com', '@abc.test.com', false ),
158  array( 'mailto:', 'mail@test.com', 'mail2@test.com', false ),
159  array( '', 'mailto:mail@test.com', 'mail2@test.com', false ),
160  array( '', 'mailto:@test.com', '@abc.test.com', false ),
161  array( 'ftp://', '*.co', 'ftp://www.co.uk', false ),
162  array( 'ftp://', '*.co', 'ftp://www.co.m', false ),
163  array( 'ftp://', '*.co/dir/', 'ftp://www.co/dir2/', false ),
164  array( 'ftp://', 'www.co/dir/', 'ftp://www.co/dir2/', false ),
165  array( 'ftp://', 'test.com/dir/', 'ftp://test.com/', false ),
166  array( '', 'http://test.com:8080/dir/', 'http://test.com:808/dir/', false ),
167  array( '', 'http://test.com/dir/index.html', 'http://test.com/dir/index.php', false ),
168 
169  //
170  // These are false positives too and ideally shouldn't match, but that
171  // would require using regexes and RLIKE instead of LIKE
172  //
173  // array( null, 'http://*.test.com', 'http://www.test.com:80', false ),
174  // array( '', 'https://*.wikimedia.org/r/#/q/status:open,n,z',
175  // 'https://gerrit.wikimedia.org/XXX/r/#/q/status:open,n,z', false ),
176  );
177 
178  }
179 
193  function testMakeLikeArrayWithValidPatterns( $protocol, $pattern, $url, $shouldBeFound = true ) {
194 
195  $indexes = wfMakeUrlIndexes( $url );
196  $likeArray = LinkFilter::makeLikeArray( $pattern, $protocol );
197 
198  $this->assertTrue( $likeArray !== false,
199  "LinkFilter::makeLikeArray('$pattern', '$protocol') returned false on a valid pattern"
200  );
201 
202  $regex = $this->createRegexFromLIKE( $likeArray );
203  $debugmsg = "Regex: '" . $regex . "'\n";
204  $debugmsg .= count( $indexes ) . " index(es) created by wfMakeUrlIndexes():\n";
205 
206  $matches = 0;
207 
208  foreach ( $indexes as $index ) {
209  $matches += preg_match( $regex, $index );
210  $debugmsg .= "\t'$index'\n";
211  }
212 
213  if ( $shouldBeFound ) {
214  $this->assertTrue(
215  $matches > 0,
216  "Search pattern '$protocol$pattern' does not find url '$url' \n$debugmsg"
217  );
218  } else {
219  $this->assertFalse(
220  $matches > 0,
221  "Search pattern '$protocol$pattern' should not find url '$url' \n$debugmsg"
222  );
223  }
224 
225  }
226 
232  public static function provideInvalidPatterns() {
233 
234  return array(
235  array( '' ),
236  array( '*' ),
237  array( 'http://*' ),
238  array( 'http://*/' ),
239  array( 'http://*/dir/file' ),
240  array( 'test.*.com' ),
241  array( 'http://test.*.com' ),
242  array( 'test.*.com' ),
243  array( 'http://*.test.*' ),
244  array( 'http://*test.com' ),
245  array( 'https://*' ),
246  array( '*://test.com'),
247  array( 'mailto:name:pass@t*est.com' ),
248  array( 'http://*:888/'),
249  array( '*http://'),
250  array( 'test.com/*/index' ),
251  array( 'test.com/dir/index?arg=*' ),
252  );
253 
254  }
255 
266 
267  $this->assertFalse(
268  LinkFilter::makeLikeArray( $pattern ),
269  "'$pattern' is not a valid pattern and should be rejected"
270  );
271 
272  }
273 
274 }
LinkFilterTest\provideValidPatterns
static provideValidPatterns()
provideValidPatterns()
Definition: LinkFilterTest.php:69
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfMakeUrlIndexes
wfMakeUrlIndexes( $url)
Make URL indexes, appropriate for the el_index field of externallinks.
Definition: GlobalFunctions.php:848
LinkFilterTest\testMakeLikeArrayWithInvalidPatterns
testMakeLikeArrayWithInvalidPatterns( $pattern)
testMakeLikeArrayWithInvalidPatterns()
Definition: LinkFilterTest.php:265
LikeMatch
Used by DatabaseBase::buildLike() to represent characters that have special meaning in SQL LIKE claus...
Definition: DatabaseUtility.php:296
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
LinkFilter\makeLikeArray
static makeLikeArray( $filterEntry, $protocol='http://')
Make an array to be used for calls to DatabaseBase::buildLike(), which will match the specified strin...
Definition: LinkFilter.php:94
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
LinkFilterTest\setUp
setUp()
Definition: LinkFilterTest.php:8
$matches
if(!defined( 'MEDIAWIKI')) if(!isset( $wgVersion)) $matches
Definition: NoLocalSettings.php:33
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
LinkFilterTest\testMakeLikeArrayWithValidPatterns
testMakeLikeArrayWithValidPatterns( $protocol, $pattern, $url, $shouldBeFound=true)
testMakeLikeArrayWithValidPatterns()
Definition: LinkFilterTest.php:193
LinkFilterTest\createRegexFromLIKE
createRegexFromLIKE( $like)
createRegexFromLike($like)
Definition: LinkFilterTest.php:40
as
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
Definition: distributors.txt:9
LinkFilterTest\provideInvalidPatterns
static provideInvalidPatterns()
provideInvalidPatterns()
Definition: LinkFilterTest.php:232
LinkFilterTest
@group Database
Definition: LinkFilterTest.php:6