Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9927dddefd0d9dffed25a84aa1f143312e4e0b66 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011, 2017 The Eclipse Foundation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     The Eclipse Foundation - initial API and implementation
 *     Ian Pun - factored out of RepositorySelectionPage
 *******************************************************************************/
package org.eclipse.egit.ui.internal.clone;

import java.net.URISyntaxException;

import org.eclipse.egit.ui.internal.KnownHosts;
import org.eclipse.egit.ui.internal.components.RepositorySelectionPage.Protocol;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.transport.Transport;
import org.eclipse.jgit.transport.TransportProtocol;
import org.eclipse.jgit.transport.URIish;

/**
 * Utility class for checking strings for being valid git URLs, and for
 * sanitizing arbitrary string input.
 */
public abstract class GitUrlChecker {

	private static final String GIT_CLONE_COMMAND_PREFIX = "git clone "; //$NON-NLS-1$

	/**
	 * Checks if the incoming string is a valid git URL. It is recommended to
	 * {@link #sanitizeAsGitUrl(String) sanitize} the String first if coming
	 * from an untrustworthy source.
	 *
	 * @param url
	 *            to check
	 * @return {@code true} if the {@code url} is a valid git URL, {@code false}
	 *         otherwise
	 */
	public static boolean isValidGitUrl(String url) {
		try {
			if (url != null) {
				URIish u = new URIish(url);
				if (canHandleProtocol(u)) {
					if (Protocol.GIT.handles(u) || Protocol.SSH.handles(u)
							|| (Protocol.HTTP.handles(u)
									|| Protocol.HTTPS.handles(u))
									&& KnownHosts.isKnownHost(u.getHost())
							|| url.endsWith(Constants.DOT_GIT_EXT)) {
						return true;
					}
				}
			}
		} catch (URISyntaxException e) {
			// Ignore. This is used to check arbitrary input for being a
			// possibly valid git URL; we don't want to flood the log here.
		}
		return false;
	}

	/**
	 * Sanitize a string for use as a git URL. Strips the Git Clone command if
	 * needed and reduces remaining the input to anything before the first
	 * whitespace.
	 *
	 * @param input
	 *            String to be sanitized
	 * @return sanitized string; if the input came from an untrustworthy source,
	 *         is should still be checked using {@link #isValidGitUrl(String)}
	 *         before being used for real as a git URL
	 */
	public static String sanitizeAsGitUrl(String input) {
		String sanitized = input.trim();
		if (sanitized.startsWith(GIT_CLONE_COMMAND_PREFIX)) {
			sanitized = sanitized.substring(GIT_CLONE_COMMAND_PREFIX.length())
					.trim();
		}
		// Take only the part up to the first whitespace character
		return sanitized.split("[\\h|\\v]", 2)[0]; //$NON-NLS-1$
	}

	private static boolean canHandleProtocol(URIish u) {
		for (TransportProtocol proto : Transport.getTransportProtocols()) {
			if (proto.canHandle(u)) {
				return true;
			}
		}
		return false;
	}

}

Back to the top