Bug 266140 - HttpFileRetrieveTransfer does not detect malformed URL
Summary: HttpFileRetrieveTransfer does not detect malformed URL
Status: RESOLVED FIXED
Alias: None
Product: ECF
Classification: RT
Component: ecf.filetransfer (show other bugs)
Version: unspecified   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: ecf.core-inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 248604
  Show dependency tree
 
Reported: 2009-02-25 10:18 EST by Henrik Lindberg CLA
Modified: 2009-02-28 01:41 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Henrik Lindberg CLA 2009-02-25 10:18:41 EST
This method in HttpFileRetrieveTransfer:
	protected static int getPortFromURL(String url) {
		final int colonSlashSlash = url.indexOf("://"); //$NON-NLS-1$
		final int colonPort = url.indexOf(':', colonSlashSlash + 1);
		if (colonPort < 0)
			return urlUsesHttps(url) ? HTTPS_PORT : HTTP_PORT;

		final int requestPath = url.indexOf('/', colonPort + 1);

		int end;
		if (requestPath < 0)
			end = url.length();
		else
			end = requestPath;

		return Integer.parseInt(url.substring(colonPort + 1, end));
	}

Does not do a good job when the url is malformed. As an example - try feeding it "http:bogus". The result is that it tries to transform "bogus" to a port number - with a NumberFormatException as the result.

Insert this after the first line in the method to correct it;
if(colonSlashSlash < 0)
   return urlUsesHttps(url) ? HTTPS_PORT : HTTP_PORT;

Since it is not meaningful to try to look for a port if there is no :// in the URL.

Have not searched if there are similar methods in other transfer protocols.

The methods getHostFromUrl, and getPathFromUrl have similar problems on malformed URLs as they start searching for "/" from a position -1 + 3.
Comment 1 Scott Lewis CLA 2009-02-25 11:21:50 EST
Hi Hendrik,

I'm not opposed to making things more robust but the String url is actually retrieved from the URL instance (e.g. line 441 in HttpClientRetrieveFileTransfer:

	final String urlString = getRemoteFileURL().toString();

the URL instance returned by getRemoteFileURL() is provided by the IFileID in the initial request...so it's been through the URL parser...which I'm assuming will/should prevent any malformed urls.

So, in any event...were you able to trigger an exception in the ECF code somehow by passing in a valid IFileID?

Comment 2 Henrik Lindberg CLA 2009-02-25 20:02:30 EST
In p2 the ECFFileTransport.download receives a String that it passes on. If this String lacks "://" it is still a valid URL, but the getPortForURL() method does not handle this case. 

So, I was wrong in saying it was an invalid URL, just not a URL that getPortForUrl() does a good job in translating - it will trigger a NumberFormatException. 

Try it with "http:bogus" for instance - it thinks that ":bogus" is a port number.
Comment 3 Scott Lewis CLA 2009-02-25 20:14:34 EST
(In reply to comment #2)
> In p2 the ECFFileTransport.download receives a String that it passes on. If
> this String lacks "://" it is still a valid URL, but the getPortForURL() method
> does not handle this case. 
> 
> So, I was wrong in saying it was an invalid URL, just not a URL that
> getPortForUrl() does a good job in translating - it will trigger a
> NumberFormatException. 
> 
> Try it with "http:bogus" for instance - it thinks that ":bogus" is a port
> number.
> 

I didn't realize http:bogus was a valid URL...learn something new everyday.

Thanks.

Comment 4 Scott Lewis CLA 2009-02-27 15:46:05 EST
(In reply to comment #3)

Henrik...were you thinking of creating a patch for these specific cases? 

Or should I assign to myself and do it?



Comment 5 Henrik Lindberg CLA 2009-02-27 20:28:18 EST
(In reply to comment #4)
> (In reply to comment #3)
> 
> Henrik...were you thinking of creating a patch for these specific cases? 
> 
> Or should I assign to myself and do it?
> 
Well, the fix to the immediate problem is to insert two lines... as described in the first post.
I can produce a patch if that helps you.

Comment 6 Scott Lewis CLA 2009-02-28 01:41:53 EST
Applied suggested checks to HttpclientRetrieveFileTransfer.getPortFromURL, getHostFromURL, getPathFromURL.

Thanks Hendrik.

Resolving as fixed.