KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kigit_remote_url.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "kigit_remote_url.h"
21
22#include <wx/regex.h>
23#include <wx/utils.h>
24
25#include <cctype>
26#include <string>
27
28namespace
29{
30
31// Index of the '@' ending the userinfo of proto://user:password@host/path, or npos. The last
32// one, since the first would split inside a password that contains an '@'
33size_t findUserinfoAt( const wxString& aUrl )
34{
35 size_t protoSep = aUrl.find( "://" );
36
37 if( protoSep == wxString::npos )
38 return wxString::npos;
39
40 size_t hostStart = protoSep + 3;
41 size_t pathStart = aUrl.find_first_of( "/?#", hostStart );
42 size_t searchEnd = ( pathStart == wxString::npos ) ? aUrl.length() : pathStart;
43
44 if( searchEnd <= hostStart )
45 return wxString::npos;
46
47 size_t at = aUrl.rfind( '@', searchEnd - 1 );
48
49 return ( at != wxString::npos && at >= hostStart ) ? at : wxString::npos;
50}
51
52
53// libgit2's git_str_decode_percent when the whole result is UTF-8; otherwise the component stays as written
54wxString decodePercent( const wxString& aText )
55{
56 const std::string text = aText.utf8_string();
57 std::string decoded;
58
59 for( size_t ii = 0; ii < text.size(); ++ii )
60 {
61 if( text[ii] == '%' && ii + 2 < text.size() && isxdigit( (unsigned char) text[ii + 1] )
62 && isxdigit( (unsigned char) text[ii + 2] ) )
63 {
64 decoded += static_cast<char>( std::stoi( text.substr( ii + 1, 2 ), nullptr, 16 ) );
65 ii += 2;
66 }
67 else
68 {
69 decoded += text[ii];
70 }
71 }
72
73 wxString result = wxString::FromUTF8( decoded );
74
75 // wxString cannot hold bytes that are not UTF-8, so such text stays as written
76 return result.IsEmpty() && !decoded.empty() ? aText : result;
77}
78
79
80// 0 when the address names no port, -1 when it names one that cannot be used
81int parsePort( const wxString& aPort )
82{
83 long port = 0;
84
85 if( aPort.IsEmpty() )
86 return 0;
87
88 if( !aPort.ToLong( &port ) || port <= 0 || port > 65535 )
89 return -1;
90
91 return static_cast<int>( port );
92}
93
94
95bool isScpIpv6( const wxString& aText, size_t aBracket )
96{
97 size_t colons = 0;
98
99 for( size_t ii = aBracket + 1; ii < aText.length(); ++ii )
100 {
101 wxUniChar c = aText[ii];
102
103 if( c == ':' )
104 colons++;
105
106 if( c == ']' )
107 return colons > 1;
108
109 if( c != ':' && !wxIsxdigit( c ) )
110 return false;
111 }
112
113 return false;
114}
115
116
117bool scpHasUser( const wxString& aText, size_t aStart )
118{
119 for( size_t ii = aStart; ii < aText.length(); ++ii )
120 {
121 if( aText[ii] == '@' )
122 return true;
123
124 if( aText[ii] == ':' )
125 return false;
126 }
127
128 return false;
129}
130
131
132// libgit2's git_net_url_parse_scp, state for state, so the host key check binds the host and port it dials
133bool parseScpAddress( const wxString& aURL, wxString& aUser, wxString& aHost, wxString& aPort )
134{
135 enum
136 {
137 NONE,
138 USER,
139 HOST_START,
140 HOST,
141 HOST_END,
142 IPV6,
143 IPV6_END,
144 PORT_START,
145 PORT,
146 PORT_END,
147 PATH_START
148 } state = NONE;
149
150 size_t user = 0, userLen = 0, host = 0, hostLen = 0, port = 0, portLen = 0;
151 int bracket = 0;
152 bool havePath = false;
153
154 for( size_t ii = 0; ii < aURL.length() && !havePath; ++ii )
155 {
156 wxUniChar c = aURL[ii];
157
158 switch( state )
159 {
160 case NONE:
161 if( c == '@' || c == ':' )
162 return false;
163
164 if( c == '[' )
165 {
166 if( isScpIpv6( aURL, ii ) )
167 {
168 state = IPV6;
169 host = ii;
170 }
171 else if( bracket++ > 1 )
172 {
173 return false;
174 }
175 }
176 else if( scpHasUser( aURL, ii ) )
177 {
178 state = USER;
179 user = ii;
180 }
181 else
182 {
183 state = HOST;
184 host = ii;
185 }
186
187 break;
188
189 case USER:
190 if( c == '@' )
191 {
192 userLen = ii - user;
193 state = HOST_START;
194 }
195
196 break;
197
198 case HOST_START:
199 state = c == '[' ? IPV6 : HOST;
200 host = ii;
201 break;
202
203 case HOST:
204 if( c == ':' )
205 {
206 hostLen = ii - host;
207 state = bracket ? PORT_START : PATH_START;
208 }
209 else if( c == ']' )
210 {
211 if( bracket-- == 0 )
212 return false;
213
214 hostLen = ii - host;
215 state = HOST_END;
216 }
217
218 break;
219
220 case HOST_END:
221 if( c != ':' )
222 return false;
223
224 state = PATH_START;
225 break;
226
227 case IPV6:
228 if( c == ']' )
229 state = IPV6_END;
230
231 break;
232
233 case IPV6_END:
234 if( c != ':' )
235 return false;
236
237 hostLen = ii - host;
238 state = bracket ? PORT_START : PATH_START;
239 break;
240
241 case PORT_START:
242 port = ii;
243 state = PORT;
244 break;
245
246 case PORT:
247 if( c == ']' )
248 {
249 if( bracket-- == 0 )
250 return false;
251
252 portLen = ii - port;
253 state = PORT_END;
254 }
255
256 break;
257
258 case PORT_END:
259 if( c != ':' )
260 return false;
261
262 state = PATH_START;
263 break;
264
265 case PATH_START: havePath = true; break;
266 }
267 }
268
269 if( !havePath || hostLen == 0 )
270 return false;
271
272 aUser = aURL.Mid( user, userLen );
273 aHost = aURL.Mid( host, hostLen );
274 aPort = aURL.Mid( port, portLen );
275
276 // libgit2 keeps an IPv6 host in its brackets here, but hands it out without them for URLs
277 if( aHost.StartsWith( "[" ) && aHost.EndsWith( "]" ) )
278 aHost = aHost.Mid( 1, aHost.length() - 2 );
279
280 return true;
281}
282
283
284} // namespace
285
286
287namespace KIGIT
288{
289
290REMOTE_URL ParseRemoteURL( const wxString& aURL )
291{
293 result.url = aURL;
294
295 size_t schemeEnd = aURL.find( "://" );
296 size_t hostStart = 0;
297 size_t hostEnd;
298
299 if( schemeEnd != wxString::npos )
300 {
301 wxString scheme = aURL.Left( schemeEnd ).Lower();
302
303 if( scheme == "https" || scheme == "http" )
305 else if( scheme == "ssh" || scheme == "ssh+git" || scheme == "git+ssh" )
307 else
308 return result;
309
310 hostStart = schemeEnd + 3;
311 hostEnd = aURL.find_first_of( "/?#", hostStart );
312 size_t at = findUserinfoAt( aURL );
313
314 if( at != wxString::npos )
315 {
316 wxString credentials = aURL.Mid( hostStart, at - hostStart );
317 size_t colon = credentials.find( ':' );
318 wxString user = credentials.Left( colon );
319 result.username = decodePercent( user );
320
321 if( colon != wxString::npos )
322 result.password = decodePercent( credentials.Mid( colon + 1 ) );
323
324 wxString userPrefix;
325
326 if( result.connType == GIT_CONN_TYPE::GIT_CONN_SSH && !user.IsEmpty() )
327 userPrefix = user + "@";
328
329 result.url = aURL.Left( hostStart ) + userPrefix + aURL.Mid( at + 1 );
330 hostStart = at + 1;
331 }
332 }
333 else
334 {
335 // A colon before the path marks an SCP address, except for Windows drive paths
336 if( aURL.length() >= 2 && wxIsalpha( aURL[0] ) && aURL[1] == ':' )
337 return result;
338
339 hostEnd = aURL.find( ':' );
340 size_t bracket = aURL.find( '[' );
341
342 if( bracket != wxString::npos && bracket < hostEnd )
343 {
344 size_t endBracket = aURL.find( ']', bracket );
345
346 if( endBracket != wxString::npos )
347 hostEnd = aURL.find( ':', endBracket + 1 );
348 }
349
350 size_t slash = aURL.find_first_of( "/\\" );
351
352 if( hostEnd == wxString::npos || hostEnd == 0 || ( slash != wxString::npos && slash < hostEnd ) )
353 {
354 return result;
355 }
356
358 wxString port;
359
360 // An address libgit2 cannot read leaves no host, which the host key check refuses
361 if( parseScpAddress( aURL, result.username, result.hostname, port ) )
362 result.port = parsePort( port );
363
364 return result;
365 }
366
367 wxString host = aURL.Mid( hostStart, hostEnd == wxString::npos ? wxString::npos : hostEnd - hostStart );
368 wxString port;
369
370 if( host.StartsWith( "[" ) )
371 {
372 result.hostname = host.AfterFirst( '[' ).BeforeFirst( ']' );
373 port = host.AfterFirst( ']' ).AfterFirst( ':' );
374 }
375 else
376 {
377 result.hostname = host.BeforeFirst( ':' );
378 port = host.AfterFirst( ':' );
379 }
380
381 // libgit2 connects to the decoded name, so %67he.example is ghe.example
382 result.hostname = decodePercent( result.hostname );
383 result.port = parsePort( port );
384
385 return result;
386}
387
388
389wxString CredentialServer( const wxString& aUrl )
390{
391 REMOTE_URL parsed = ParseRemoteURL( aUrl );
392
393 if( parsed.connType == GIT_CONN_TYPE::GIT_CONN_LOCAL || parsed.hostname.IsEmpty() || parsed.port < 0 )
394 return wxEmptyString;
395
396 const wxString scheme = aUrl.Contains( wxS( "://" ) ) ? aUrl.BeforeFirst( ':' ).Lower() : wxString( wxS( "ssh" ) );
397 const int defaultPort = scheme == wxS( "https" ) ? 443 : scheme == wxS( "http" ) ? 80 : 22;
398 const wxString host = parsed.hostname.Lower();
399 wxString server = host.Contains( wxS( ":" ) ) ? wxS( "[" ) + host + wxS( "]" ) : host;
400
401 if( parsed.port > 0 && parsed.port != defaultPort )
402 server << wxS( ":" ) << parsed.port;
403
404 return server;
405}
406
407
408wxString CredentialRealm( const wxString& aUrl )
409{
410 const wxString server = CredentialServer( aUrl );
411
412 if( server.IsEmpty() || ParseRemoteURL( aUrl ).connType != GIT_CONN_TYPE::GIT_CONN_HTTPS )
413 return wxEmptyString;
414
415 return aUrl.BeforeFirst( ':' ).Lower() + wxS( "://" ) + server;
416}
417
418
419wxString StripEmbeddedUserinfo( const wxString& aText )
420{
421 wxString result;
422 size_t lineStart = 0;
423
424 result.reserve( aText.length() );
425
426 while( true )
427 {
428 size_t lineEnd = aText.find_first_of( wxS( "\r\n" ), lineStart );
429
430 if( lineEnd == wxString::npos )
431 lineEnd = aText.length();
432
433 // A password can hold anything but a line break, even what looks like another URL, so the whole line from
434 // its first "://" to its last "@" goes
435 wxString line = aText.Mid( lineStart, lineEnd - lineStart );
436 const size_t sep = line.find( wxS( "://" ) );
437 const size_t at = line.rfind( '@' );
438
439 if( sep != wxString::npos && at != wxString::npos && at >= sep + 3 )
440 line.erase( sep + 3, at - sep - 2 );
441
442 result << line;
443
444 if( lineEnd == aText.length() )
445 return result;
446
447 result << aText[lineEnd];
448 lineStart = lineEnd + 1;
449 }
450}
451
452
453wxString DecodeServerText( const char* aData, size_t aLength )
454{
455 wxString text = wxString::FromUTF8( aData, aLength );
456
457 // An empty result would drop the line, and with it the scheme the scrub looks for
458 if( text.IsEmpty() && aLength > 0 )
459 text = wxString::From8BitData( aData, aLength );
460
461 return text;
462}
463
464
465wxString PrintableServerText( const wxString& aText )
466{
467 static thread_local const wxRegEx escapes( wxS( "\x1B\\[[0-?]*[ -/]*[@-~]" ), wxRE_ADVANCED );
468
469 wxString text = aText;
470
471 // wxRegEx stops replacing at a NUL, which would leave the rest of a password in place
472 text.Replace( wxString( wxUniChar( 0 ) ), wxEmptyString );
473
474 // An escape can end in the "@" that closes a userinfo, so the raw text is scrubbed first
476 escapes.ReplaceAll( &text, wxEmptyString );
477
478 wxString printable;
479
480 for( wxUniChar c : text )
481 {
482 const wxUint32 code = c.GetValue();
483 const bool control = code < 0x20 || code == 0x7F || ( code >= 0x80 && code <= 0x9F );
484 const bool bidi = ( code >= 0x202A && code <= 0x202E ) || ( code >= 0x2066 && code <= 0x2069 );
485
486 if( code == '\t' )
487 printable << wxS( " " );
488 else if( ( !control && !bidi ) || code == '\n' || code == '\r' )
489 printable << c;
490 }
491
492 // Removing escapes and controls can join a "://" the raw text split
493 return StripEmbeddedUserinfo( printable );
494}
495
496} // namespace KIGIT
@ NONE
Definition eda_fill.h:42
@ USER
The main config directory (e.g. ~/.config/kicad/)
wxString PrintableServerText(const wxString &aText)
Text a server sent, made safe to show by removing terminal escapes, control and bidi characters,...
wxString DecodeServerText(const char *aData, size_t aLength)
Bytes a server sent, as UTF-8 or, when they are not valid UTF-8, one character for each byte.
wxString CredentialServer(const wxString &aUrl)
The host and port aUrl reaches, as host[:port] without the scheme's default port.
wxString CredentialRealm(const wxString &aUrl)
The server an HTTP(S) credential for aUrl serves, as scheme://host[:port] with no user,...
REMOTE_URL ParseRemoteURL(const wxString &aURL)
Split a remote address the way libgit2 reads it, so a host key is checked against the host it dials.
wxString StripEmbeddedUserinfo(const wxString &aText)
Remove the user and password from every URL quoted in a message, since the remote's can carry a passw...
A transport address with any embedded credentials extracted.
int port
0 when the address names none.
GIT_CONN_TYPE connType
static std::string decoded(const std::string &aBase64)
wxString result
Test unit parsing edge cases and error handling.