@@ -1987,44 +1987,32 @@ Path createTempFile(String prefix, String suffix) throws IOException {
19871987 return createTempFileInSystemDir (prefix , suffix );
19881988 }
19891989 }
1990- Path tmpPath = Path .of (workspaceTmp .getAbsolutePath ());
1990+ String absolutePath = workspaceTmp .getAbsolutePath ();
1991+ Path tmpPath = Path .of (absolutePath );
1992+ if (absolutePath .contains ("%" )) {
1993+ // Avoid ssh token expansion on all platforms
1994+ return createTempFileInSystemDir (prefix , suffix );
1995+ }
19911996 if (isWindows ()) {
1997+ /* Windows git fails its call to GIT_SSH if its absolute
1998+ * path contains a space or parenthesis or pipe or question mark or asterisk.
1999+ * Use system temp dir instead of workspace temp dir.
2000+ */
2001+ if (absolutePath .matches (".*[ ()|?*].*" )) {
2002+ return createTempFileInSystemDir (prefix , suffix );
2003+ }
19922004 return Files .createTempFile (tmpPath , prefix , suffix );
19932005 }
2006+ // Unix specific
2007+ if (absolutePath .contains ("`" )) {
2008+ // Avoid backquote shell expansion
2009+ return createTempFileInSystemDir (prefix , suffix );
2010+ }
19942011 Set <PosixFilePermission > ownerOnly = PosixFilePermissions .fromString ("rw-------" );
19952012 FileAttribute <Set <PosixFilePermission >> fileAttribute = PosixFilePermissions .asFileAttribute (ownerOnly );
19962013 return Files .createTempFile (tmpPath , prefix , suffix , fileAttribute );
19972014 }
19982015
1999- /**
2000- * Create temporary file for SSH/askpass wrapper scripts.
2001- *
2002- * Wrapper scripts are passed to git via GIT_SSH environment variable, and git
2003- * must be able to execute them. Unlike SSH keys and passwords which contain
2004- * actual secrets, wrapper scripts only contain references to environment variables.
2005- *
2006- * System temp is used for reliability:
2007- * - Workspace paths can contain special characters that break git execution
2008- * - No secrets are exposed (wrappers only reference environment variables)
2009- * - Consistent, predictable paths across all platforms and workspace configurations
2010- * - Credentials (SSH keys, passwords) remain isolated in workspace temp
2011- *
2012- * @param prefix file name prefix for the generated temporary file (will be preceeded by "jenkins-gitclient-")
2013- * @param suffix file name suffix for the generated temporary file
2014- * @return temporary file for wrapper script in system temp directory
2015- * @throws IOException on error
2016- */
2017- private Path createTempFileForWrapper (String prefix , String suffix ) throws IOException {
2018- String common_prefix = "jenkins-gitclient-" ;
2019- if (prefix == null ) {
2020- prefix = common_prefix ;
2021- } else {
2022- prefix = common_prefix + prefix ;
2023- }
2024-
2025- return createTempFileInSystemDir (prefix , suffix );
2026- }
2027-
20282016 private void deleteTempFile (Path tempFile ) {
20292017 if (tempFile != null ) {
20302018 try {
@@ -2130,10 +2118,7 @@ private String launchCommandWithCredentials(
21302118 userName = sshUser .getUsername ();
21312119 }
21322120 passphrase = createPassphraseFile (sshUser );
2133- /* ssh.exe 9.5 on Windows does not accept spaces in path to known_hosts.
2134- * Use temp file wrapper location because known_hosts is not sensitive info.
2135- */
2136- knownHostsTemp = createTempFileForWrapper ("known_hosts" , "" );
2121+ knownHostsTemp = createTempFile ("known_hosts" , "" );
21372122 if (launcher .isUnix ()) {
21382123 ssh = createUnixGitSSH (key , userName , knownHostsTemp );
21392124 askpass = createUnixSshAskpass (sshUser , passphrase );
@@ -2715,7 +2700,7 @@ public File getSSHExecutable() {
27152700
27162701 /* Package protected for security testing */
27172702 Path createWindowsGitSSH (Path key , String user , Path knownHosts ) throws IOException {
2718- Path ssh = createTempFileForWrapper ("ssh" , ".bat" );
2703+ Path ssh = createTempFile ("ssh" , ".bat" );
27192704
27202705 File sshexe = getSSHExecutable ();
27212706
@@ -2735,7 +2720,7 @@ Path createWindowsGitSSH(Path key, String user, Path knownHosts) throws IOExcept
27352720
27362721 /* Package protected for security testing */
27372722 Path createUnixGitSSH (Path key , String user , Path knownHosts ) throws IOException {
2738- Path ssh = createTempFileForWrapper ("ssh" , ".sh" );
2723+ Path ssh = createTempFile ("ssh" , ".sh" );
27392724 try (BufferedWriter w = Files .newBufferedWriter (ssh , Charset .forName (encoding ))) {
27402725 w .write ("#!/bin/sh" );
27412726 w .newLine ();
0 commit comments