Skip to content

Commit

Permalink
Merge pull request #152 from bastelfreak/test_for_invalid_escape
Browse files Browse the repository at this point in the history
Fail on escape sequences in metadata.json
  • Loading branch information
bastelfreak authored Jan 24, 2025
2 parents 76a9a59 + f0b04de commit d0c1cd5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
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

0 comments on commit d0c1cd5

Please sign in to comment.