Skip to content

Commit 26d264a

Browse files
Vitexusclaude
andcommitted
fix: harden Jenkinsfile against injection and permission issues
- Scope copyArtifactPermission to Foregin/* (was wildcard *) - Use withEnv+single-quoted sh for dch to prevent BUILD_TAG injection - Replace for/cat/awk mv loop with safe while IFS= read loop with -- and option-injection guard - Validate pkgName with regex before apt-get install - Use withEnv+single-quoted sh for apt-get to prevent pkgName injection - Remove trusted local.list after install block Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent aeccf33 commit 26d264a

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

Test/Jenkinsfile

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ String vendor = 'vitexsoftware'
1717
//String distroFamily = ''
1818

1919
properties([
20-
copyArtifactPermission('*'),
20+
copyArtifactPermission('Foregin/*'),
2121
buildBlocker(
2222
useBuildBlocker: true,
2323
blockLevel: 'GLOBAL',
@@ -70,7 +70,9 @@ distributions.each { distro ->
7070
export TMPDIR="/tmp/build-''' + uniqueBuildId + '''"
7171
mkdir -p "$DH_INTERNAL_BUILDDIR" "$TMPDIR"
7272
'''
73-
sh 'dch -b -v ' + buildVer + ' "' + env.BUILD_TAG + '"'
73+
withEnv(["BUILDVER=${buildVer}"]) {
74+
sh 'dch -b -v "$BUILDVER" "$BUILD_TAG"'
75+
}
7476
sh 'sudo apt-get update --allow-releaseinfo-change'
7577
sh 'sudo chown jenkins:jenkins ..'
7678
sh 'sudo rm -rf debian/$(dpkg-parsechangelog --show-field Source)/ debian/.debhelper/ debian/tmp/'
@@ -79,7 +81,15 @@ distributions.each { distro ->
7981
export TMPDIR="/tmp/build-''' + uniqueBuildId + '''"
8082
debuild-pbuilder -r"sudo -E" -i -us -uc -b
8183
'''
82-
sh 'mkdir -p $WORKSPACE/dist/debian/ ; rm -rf $WORKSPACE/dist/debian/* ; for deb in $(cat debian/files | awk \'{print $1}\'); do mv "../$deb" $WORKSPACE/dist/debian/; done'
84+
sh '''
85+
mkdir -p $WORKSPACE/dist/debian/
86+
rm -rf $WORKSPACE/dist/debian/*
87+
while IFS= read -r deb _; do
88+
case "$deb" in -*) continue;; esac
89+
[ -n "$deb" ] || continue
90+
mv -- "../$deb" "$WORKSPACE/dist/debian/"
91+
done < debian/files
92+
'''
8393
artifacts = sh (
8494
script: "cat debian/files | awk '{print \$1}'",
8595
returnStdout: true
@@ -106,8 +116,11 @@ distributions.each { distro ->
106116
sorted_artifacts.each { deb_file ->
107117
if (deb_file.endsWith('.deb')) {
108118
def pkgName = deb_file.tokenize('/')[-1].replaceFirst(/_.*/, '')
109-
sh 'echo -e "${GREEN} installing ' + pkgName + ' on `lsb_release -sc` ${ENDCOLOR} "'
110-
sh 'sudo DEBIAN_FRONTEND=noninteractive DEBCONF_DEBUG=' + debconf_debug + ' apt-get -y install ' + pkgName
119+
if (!(pkgName ==~ /^[a-z0-9][a-z0-9+.\-]+$/)) { error("Invalid package name: ${pkgName}") }
120+
withEnv(["PKG=${pkgName}"]) {
121+
sh 'echo "Installing $PKG on $(lsb_release -sc)"'
122+
sh 'sudo DEBIAN_FRONTEND=noninteractive apt-get -y install "$PKG"'
123+
}
111124
}
112125
}
113126
} else {
@@ -123,11 +136,15 @@ distributions.each { distro ->
123136
}
124137
if (debFile) {
125138
def pkgName = debFile.tokenize('/')[-1].replaceFirst(/_.*/, '')
126-
sh 'echo -e "${GREEN} installing ' + pkgName + ' on `lsb_release -sc` ${ENDCOLOR} "'
127-
sh 'sudo DEBIAN_FRONTEND=noninteractive DEBCONF_DEBUG=' + debconf_debug + ' apt-get -y install ' + pkgName
139+
if (!(pkgName ==~ /^[a-z0-9][a-z0-9+.\-]+$/)) { error("Invalid package name: ${pkgName}") }
140+
withEnv(["PKG=${pkgName}"]) {
141+
sh 'echo "Installing $PKG on $(lsb_release -sc)"'
142+
sh 'sudo DEBIAN_FRONTEND=noninteractive apt-get -y install "$PKG"'
143+
}
128144
}
129145
}
130146
}
147+
sh 'sudo rm -f /etc/apt/sources.list.d/local.list'
131148

132149
}
133150
stage('Archive artifacts ' + distroName ) {

0 commit comments

Comments
 (0)