Skip to content

Commit d0c1cd5

Browse files
authored
Merge pull request #152 from bastelfreak/test_for_invalid_escape
Fail on escape sequences in metadata.json
2 parents 76a9a59 + f0b04de commit d0c1cd5

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

lib/metadata_json_lint.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

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

1215
def options
1316
@options ||= Struct.new(
@@ -69,6 +72,11 @@ def run
6972
end
7073
module_function :run
7174

75+
def contains_invalid_escape?(content)
76+
content.match?(INVALID_ESCAPE_REGEX)
77+
end
78+
module_function :contains_invalid_escape?
79+
7280
def parse(metadata)
7381
@errors = []
7482
@warnings = []
@@ -83,6 +91,8 @@ def parse(metadata)
8391
abort("Error: Unable to read metadata file: #{e.exception}")
8492
end
8593

94+
abort('Error: Unable to parse metadata.json: Invalid escape character in string') if contains_invalid_escape?(f)
95+
8696
begin
8797
parsed = JSON.parse(f)
8898
rescue Exception => e

tests/invalid_escape_char/Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$LOAD_PATH.unshift(File.expand_path('../../lib', __dir__))
2+
require 'metadata-json-lint/rake_task'

tests/invalid_escape_char/expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Error: Unable to parse metadata.json: Invalid escape character in string
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "puppetlabs-postgresql",
3+
"version": "3.4.1",
4+
"author": "Inkling/Puppet Labs",
5+
"summary": "A description with an invalid \( escape sequence",
6+
"license": "Apache-2.0",
7+
"source": "git://github.com/puppetlabs/puppet-postgresql.git",
8+
"project_page": "https://github.com/puppetlabs/puppet-postgresql",
9+
"issues_url": "https://github.com/puppetlabs/puppet-postgresql/issues",
10+
"operatingsystem_support": [
11+
],
12+
"requirements": [
13+
],
14+
"dependencies": [
15+
]
16+
}

tests/test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ test "bad_license" $SUCCESS --no-strict-license
103103
# Run with --no-fail-on-warnings, expect SUCCESS
104104
test "bad_license" $SUCCESS --no-fail-on-warnings
105105

106+
# Run a broken one, expect FAILURE
107+
test "invalid_escape_char" $FAILURE
108+
106109
# Run a broken one, expect FAILURE
107110
test "long_summary" $FAILURE
108111

0 commit comments

Comments
 (0)