Skip to content
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

Fail on escape sequences in metadata.json #152

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/metadata_json_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

module MetadataJsonLint
MIN_PUPPET_VER = '4.10.0'.freeze
# Regex looks for:
# 1. Invalid escape sequences (\x or incomplete \u)
INVALID_ESCAPE_REGEX = %r{\\[^"/bfnrtu]|\\u(?![0-9a-fA-F]{4})}.freeze

def options
@options ||= Struct.new(
Expand Down Expand Up @@ -69,6 +72,11 @@ def run
end
module_function :run

def contains_invalid_escape?(content)
content.match?(INVALID_ESCAPE_REGEX)
end
module_function :contains_invalid_escape?

def parse(metadata)
@errors = []
@warnings = []
Expand All @@ -83,6 +91,8 @@ def parse(metadata)
abort("Error: Unable to read metadata file: #{e.exception}")
end

abort('Error: Unable to parse metadata.json: Invalid escape character in string') if contains_invalid_escape?(f)

begin
parsed = JSON.parse(f)
rescue Exception => e
Expand Down
2 changes: 2 additions & 0 deletions tests/invalid_escape_char/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift(File.expand_path('../../lib', __dir__))
require 'metadata-json-lint/rake_task'
1 change: 1 addition & 0 deletions tests/invalid_escape_char/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: Unable to parse metadata.json: Invalid escape character in string
16 changes: 16 additions & 0 deletions tests/invalid_escape_char/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "puppetlabs-postgresql",
"version": "3.4.1",
"author": "Inkling/Puppet Labs",
"summary": "A description with an invalid \( escape sequence",
"license": "Apache-2.0",
"source": "git://github.com/puppetlabs/puppet-postgresql.git",
"project_page": "https://github.com/puppetlabs/puppet-postgresql",
"issues_url": "https://github.com/puppetlabs/puppet-postgresql/issues",
"operatingsystem_support": [
],
"requirements": [
],
"dependencies": [
]
}
3 changes: 3 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ test "bad_license" $SUCCESS --no-strict-license
# Run with --no-fail-on-warnings, expect SUCCESS
test "bad_license" $SUCCESS --no-fail-on-warnings

# Run a broken one, expect FAILURE
test "invalid_escape_char" $FAILURE

# Run a broken one, expect FAILURE
test "long_summary" $FAILURE

Expand Down
Loading