Skip to content

(PUP-8243) Strip leading BOM from ERB templates #8639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2021

Conversation

joshcooper
Copy link
Contributor

@joshcooper joshcooper commented Jun 15, 2021

Previously, if a template contained a BOM, then it was preserved by the template function, and would end up in the resulting file or powershell command. Now we pass the bom option when reading the file, which strips the BOM as it is read.

For example, given a template with a leading BOM:

$ cat test.ps1.erb
<%= 42 %>
$ od -t x1 test.ps1.erb
0000000 ef bb bf 3c 25 3d 20 34 32 20 25 3e

Puppet used to include the BOM in the rendered output:

C:\Users\josh\projects\puppet>bundle exec puppet apply -e "notify { 'thing': message => template('C:\Users\josh\projects\puppet\test.ps1.erb') }"
...
Notice: /Stage[main]/Main/Notify[thing]/message: defined 'message' as ' 42'

Note the unprintable character before 42 above:

With this change:

C:\Users\josh\projects\puppet>bundle exec puppet apply -e "notify { 'thing': message => template('C:\Users\josh\projects\puppet\test.ps1.erb') }"
...
Notice: /Stage[main]/Main/Notify[thing]/message: defined 'message' as '42'

Create temp files when testing template wrapper file access. Although slightly
slower, it's the only way to test ruby across different OS's and external
encodings.
Specify the 'bom' flag to `read`. On Windows, we continue trying UTF-8, then the
default external encoding. On POSIX, we continue using the default external
encoding, which is what ruby defaults to.
@joshcooper joshcooper requested review from a team June 15, 2021 17:09
@joshcooper
Copy link
Contributor Author

jenkins please test this with servertests

@joshcooper
Copy link
Contributor Author

jenkins please test this with servertests on redhat8-64a,windows2019-64a

@GabrielNagy GabrielNagy merged commit a557077 into puppetlabs:6.x Jun 17, 2021
@joshcooper joshcooper deleted the ignore_bom_in_templates_8243 branch June 17, 2021 15:50
@ekohl
Copy link
Contributor

ekohl commented Aug 3, 2021

I've started to see opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet/file_system/file_impl.rb:83: warning: BOM with non-UTF encoding US-ASCII is nonsense recently. Could this be causing it?

I see it here with Puppet 6 on CentOS 7.

@joshcooper
Copy link
Contributor Author

Yes looks like it. Apparently, ruby will warn if you specify bom and the Encoding.default_external encoding isn't utf-. We should check the encoding before adding the bom prefix. I filed this as https://tickets.puppetlabs.com/browse/PUP-11196

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants