jenkinsci / jenkinsci/workflow-basic-steps-plugin

[JENKINS-53901] Using readFile does not handle UTF-8 with BOM files

Open
#471 7 comments 0 reactions 0 assignees View on GitHub
component:workflow-basic-steps-plugin imported-jira-issue priority:blocker resolution:unresolved
Dominant language
Java
Stars
73
Forks
129
PR merge metrics
No merged PRs in 30d

Description

I'm extracting xml file (nuspec) from some nuget packages and trying to parse it. In most cases it works fine, but in some the xml was written using UTF-8 with BOM encoding, and then parser gets upset and reports:

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.


The way I'm parsing xml is:

@​NonCPS

def parsePackage(packageName, packageVersion) {
def packageFullName = "${packageName}.${packageVersion}"
bat """curl -L https://www.nuget.org/api/v2/package/${packageName}/${packageVersion} -o ${packageFullName}.nupkg"""
bat """unzip ${packageFullName}.nupkg -d ${packageFullName}"""

def nuspecPath = """${packageFullName}\\${packageName}.nuspec"""
def nuspecContent = readFile file:nuspecPath
def nuspecXML = new XmlSlurper( false, false ).parseText(nuspecContent)
println nuspecXML.metadata.version

def newXml = XmlUtil.serialize(nuspecXML)
return newXml
}


It looks like readFile is not supporting UTF-8 with BOM as it is passing leading BOM characters into returned string.

 

I tried to replicate it directly in groovy doing 

def xmldata = new File("Newtonsoft.Json.nuspec").text

def pkg = new XmlSlurper().parseText(xmldata)
println pkg.metadata.version.text()

But here the leading BOM characters are not passed into xmldata variable

 

Attached example nuspec with BOM in it.

 

 

---
Originally reported by quas, imported from: Using readFile does not handle UTF-8 with BOM files


  • status: Reopened
  • priority: Blocker
  • component(s): workflow-basic-steps-plugin
  • resolution: Unresolved
  • votes: 1
  • watchers: 5
  • imported: 20251215-220547

Raw content of original issue

I'm extracting xml file (nuspec) from some nuget packages and trying to parse it. In most cases it works fine, but in some the xml was written using UTF-8 with BOM encoding, and then parser gets upset and reports:



org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.



The way I'm parsing xml is:



@NonCPS

def parsePackage(packageName, packageVersion) {
def packageFullName = "${packageName}.${packageVersion}"
bat """curl -L https://www.nuget.org/api/v2/package/${packageName}/${packageVersion} -o ${packageFullName}.nupkg"""
bat """unzip ${packageFullName}.nupkg -d ${packageFullName}"""

def nuspecPath = """${packageFullName}\\${packageName}.nuspec"""
def nuspecContent = readFile file:nuspecPath
def nuspecXML = new XmlSlurper( false, false ).parseText(nuspecContent)
println nuspecXML.metadata.version

def newXml = XmlUtil.serialize(nuspecXML)
return newXml
}



It looks like readFile is not supporting UTF-8 with BOM as it is passing leading BOM characters into returned string.

 

I tried to replicate it directly in groovy doing 



def xmldata = new File("Newtonsoft.Json.nuspec").text

def pkg = new XmlSlurper().parseText(xmldata)
println pkg.metadata.version.text()


But here the leading BOM characters are not passed into xmldata variable

 

Attached example nuspec with BOM in it.

 

 

  • environment: Jenkins 2.121.2 and Jenkins 2.81 Pipeline Groovy Plugin 2.54

1 attachment

- [Newtonsoft.Json.nuspec](https://issues.jenkins.io/secure/attachment/44656/Newtonsoft.Json.nuspec)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.