|
144 | 144 | import org.slf4j.LoggerFactory; |
145 | 145 |
|
146 | 146 | import javax.inject.Inject; |
| 147 | +import javax.net.ssl.SSLHandshakeException; |
147 | 148 | import java.io.File; |
148 | 149 | import java.io.FileOutputStream; |
149 | 150 | import java.io.FilenameFilter; |
@@ -230,6 +231,8 @@ class JGitConnection implements GitConnection { |
230 | 231 | private static final String MESSAGE_COMMIT_NOT_POSSIBLE = "Commit is not possible because repository state is '%s'"; |
231 | 232 | private static final String MESSAGE_COMMIT_AMEND_NOT_POSSIBLE = "Amend is not possible because repository state is '%s'"; |
232 | 233 |
|
| 234 | + private static final String FILE_NAME_TOO_LONG_ERROR_PREFIX = "File name too long"; |
| 235 | + |
233 | 236 | private static final Logger LOG = LoggerFactory.getLogger(JGitConnection.class); |
234 | 237 |
|
235 | 238 | private Git git; |
@@ -521,7 +524,8 @@ protected void onEndTask(String taskName, int workCurr, int workTotal, int perce |
521 | 524 | } |
522 | 525 | return; |
523 | 526 | } |
524 | | - throw new GitException(exception.getMessage(), exception); |
| 527 | + String message = generateExceptionMessage(exception); |
| 528 | + throw new GitException(message, exception); |
525 | 529 | } |
526 | 530 | } |
527 | 531 |
|
@@ -684,7 +688,7 @@ public void fetch(FetchRequest request) throws GitException, UnauthorizedExcepti |
684 | 688 | } else if ("Nothing to fetch.".equals(exception.getMessage())) { |
685 | 689 | return; |
686 | 690 | } else { |
687 | | - errorMessage = exception.getMessage(); |
| 691 | + errorMessage = generateExceptionMessage(exception); |
688 | 692 | } |
689 | 693 | throw new GitException(errorMessage, exception); |
690 | 694 | } |
@@ -1030,7 +1034,7 @@ public PullResponse pull(PullRequest request) throws GitException, UnauthorizedE |
1030 | 1034 | if (exception.getMessage().equals("Invalid remote: " + remoteName)) { |
1031 | 1035 | errorMessage = ERROR_NO_REMOTE_REPOSITORY; |
1032 | 1036 | } else { |
1033 | | - errorMessage = exception.getMessage(); |
| 1037 | + errorMessage = generateExceptionMessage(exception); |
1034 | 1038 | } |
1035 | 1039 | throw new GitException(errorMessage, exception); |
1036 | 1040 | } |
@@ -1111,7 +1115,8 @@ public PushResponse push(PushRequest request) throws GitException, UnauthorizedE |
1111 | 1115 | if ("origin: not found.".equals(exception.getMessage())) { |
1112 | 1116 | throw new GitException(ERROR_NO_REMOTE_REPOSITORY, exception); |
1113 | 1117 | } else { |
1114 | | - throw new GitException(exception.getMessage(), exception); |
| 1118 | + String message = generateExceptionMessage(exception); |
| 1119 | + throw new GitException(message, exception); |
1115 | 1120 | } |
1116 | 1121 | } |
1117 | 1122 | } |
@@ -1550,7 +1555,8 @@ public boolean accept(File dir) { |
1550 | 1555 | } |
1551 | 1556 | } |
1552 | 1557 | } catch (IOException exception) { |
1553 | | - throw new GitException(exception.getMessage(), exception); |
| 1558 | + String message = generateExceptionMessage(exception); |
| 1559 | + throw new GitException(message, exception); |
1554 | 1560 | } |
1555 | 1561 | } |
1556 | 1562 |
|
@@ -1708,4 +1714,42 @@ private String cleanRemoteName(String branchName) throws GitException { |
1708 | 1714 | } |
1709 | 1715 | return returnName; |
1710 | 1716 | } |
| 1717 | + |
| 1718 | + /** |
| 1719 | + * Method for generate exception message. The default logic return message from the error. |
| 1720 | + * It also check if the type of the message is for SSL or in case that the error |
| 1721 | + * start with "file name to long" then it raise the relevant message |
| 1722 | + * |
| 1723 | + * @param error |
| 1724 | + * throwable error |
| 1725 | + * @return exception message |
| 1726 | + */ |
| 1727 | + private String generateExceptionMessage(Throwable error) { |
| 1728 | + String message = error.getMessage(); |
| 1729 | + while (error.getCause() != null) { |
| 1730 | + //if e caused by an SSLHandshakeException - replace thrown message with a hardcoded message |
| 1731 | + if (error.getCause() instanceof SSLHandshakeException) { |
| 1732 | + message = "The system is not configured to trust the security certificate provided by the Git server"; |
| 1733 | + break; |
| 1734 | + } else if (error.getCause() instanceof IOException) { |
| 1735 | + // Security fix - error message should not include complete local file path on the target system |
| 1736 | + // Error message for example - File name too long (path /xx/xx/xx/xx/xx/xx/xx/xx /, working dir /xx/xx/xx) |
| 1737 | + if (message != null && message.startsWith(FILE_NAME_TOO_LONG_ERROR_PREFIX)) { |
| 1738 | + try { |
| 1739 | + String repoPath = repository.getWorkTree().getCanonicalPath(); |
| 1740 | + int startIndex = message.indexOf(repoPath); |
| 1741 | + int endIndex = message.indexOf(","); |
| 1742 | + if (startIndex > -1 && endIndex > -1) { |
| 1743 | + message = FILE_NAME_TOO_LONG_ERROR_PREFIX + " " + message.substring(startIndex + repoPath.length(), endIndex); |
| 1744 | + } |
| 1745 | + break; |
| 1746 | + } catch (IOException e) { |
| 1747 | + //Hide exception as it is only needed for this message generation |
| 1748 | + } |
| 1749 | + } |
| 1750 | + } |
| 1751 | + error = error.getCause(); |
| 1752 | + } |
| 1753 | + return message; |
| 1754 | + } |
1711 | 1755 | } |
0 commit comments