json格式化程序的Ruby脚本



我正在为sonarqube制作json格式化程序,这是我的脚本:

require 'json'
tr_report = File.open('./tes.json').read
tr_report.gsub!(/rn?/, "n")
sq_generic_format = {'issues' => []}
sq_issue_format = {
'engineId' => '', # CONST='brakeman'
'ruleId' => '', #[check_name (warning_code)] warning_type [confidence]
'severity':'MAJOR', # MAJOR
'type':'VULNERABILITY', # CONST='VULNERABILITY'
'primaryLocation' => {},
'effortMinutes' => 0, #CONST=0
}
primary_location_format = {
'message' => '', # message + CONST='nCode:' + code + CONST='nUser Input:' + user_input + CONST='nLink: ' + link
'filePath' => '', # file
'textRange' => {}
}
text_range_format = {
'startLine' => 1,# line
'endLine' => 1,# line
'startColumn' => 0,
'endColumn' => 1
}
issues = []
tr_report.each_line do |line|
tr_data = JSON.parse(line)
# puts tr_data
# puts parsed["SourceMetadata"]["Data"]["Filesystem"]["file"]
issue = sq_issue_format
issue['engineId'] = 'trufflehog'
issue['ruleId'] = 'Sensitive Data Exposure - %s' %[tr_data['Raw']]
issue['severity'] = 'MAJOR' # MAJOR
issue['type'] = 'VULNERABILITY' # CONST='VULNERABILITY'
issue['effortMinutes'] = 0
issue['primaryLocation'] = {}
# filling up nested data lvl1 ^
primary_location = primary_location_format
primary_location['message'] = 'Sensitive Data Exposure'
primary_location['filePath'] = tr_data["SourceMetadata"]["Data"]["Filesystem"]["file"] # file
primary_location['textRange'] = {}
# filling up nested data lvl2 ^
text_range = text_range_format
# text_range['startLine'] = w['line']
# text_range['endLine'] = w['line']
# sticking all together
primary_location['textRange'] = text_range
issue['primaryLocation'] = primary_location
issues.append(issue)
end
# puts issues
sq_generic_format['issues'] = issues
puts JSON.dump(sq_generic_format)
File.write('./trufflehog-sq-report.json', JSON.dump(sq_generic_format))

这是我的jsonline tes.json:

{"SourceMetadata":{"Data":{"Filesystem":{"file":"../ruby/railsgoat/dependency-check-report.html"}}},"SourceID":15,"SourceType":15,"SourceName":"trufflehog - filesystem","DetectorType":9,"DetectorName":"Gitlab","Verified":false,"Raw":"vulnerable-to-driveby-","Redacted":"","ExtraData":null,"StructuredData":null}
{"SourceMetadata":{"Data":{"Filesystem":{"file":"../ruby/railsgoat/dependency-check-report.html"}}},"SourceID":15,"SourceType":15,"SourceName":"trufflehog - filesystem","DetectorType":800,"DetectorName":"Atera","Verified":false,"Raw":"39a6bda16ef9583fba2696cc3efde0da","Redacted":"","ExtraData":null,"StructuredData":null}

但每次我尝试运行它时,我总是得到解析的第一行,我无法得到下一行并使结果重复。如何捕获下一行的解析?而不仅仅是第一行。

此外,我制作了一个简单的脚本来解析jsonl,它像我预期的那样成功了。这是脚本:

require 'json'
text=File.open('tes.json').read
text.gsub!(/rn?/, "n")
text.each_line do |line|
parsed = JSON.parse(line)
puts parsed["Raw"]
end

结果:

vulnerable-to-driveby-
39a6bda16ef9583fba2696cc3efde0da

当前结果:它只解析第一行,预期结果:我正确地得到了所有的解析。我的格式化程序脚本的预期结果:

{"issues":[{"engineId":"trufflehog","ruleId":"Sensitive Data Exposure - vulnerable-to-driveby-","severity":"MAJOR","type":"VULNERABILITY","primaryLocation":{"message":"Sensitive Data Exposure","filePath":"../ruby/railsgoat/dependency-check-report.html","textRange":{"startLine":1,"endLine":1,"startColumn":0,"endColumn":1}},"effortMinutes":0,"severity":"MAJOR","type":"VULNERABILITY"},{"engineId":"trufflehog","ruleId":"Sensitive Data Exposure - 39a6bda16ef9583fba2696cc3efde0da","severity":"MAJOR","type":"VULNERABILITY","primaryLocation":{"message":"Sensitive Data Exposure","filePath":"../ruby/railsgoat/dependency-check-report.html","textRange":{"startLine":1,"endLine":1,"startColumn":0,"endColumn":1}},"effortMinutes":0,"severity":"MAJOR","type":"VULNERABILITY"}]}

我现在得到的是:

{"issues":[{"engineId":"trufflehog","ruleId":"Sensitive Data Exposure - 39a6bda16ef9583fba2696cc3efde0da","severity":"MAJOR","type":"VULNERABILITY","primaryLocation":{"message":"Sensitive Data Exposure","filePath":"../ruby/railsgoat/dependency-check-report.html","textRange":{"startLine":1,"endLine":1,"startColumn":0,"endColumn":1}},"effortMinutes":0,"severity":"MAJOR","type":"VULNERABILITY"},{"engineId":"trufflehog","ruleId":"Sensitive Data Exposure - 39a6bda16ef9583fba2696cc3efde0da","severity":"MAJOR","type":"VULNERABILITY","primaryLocation":{"message":"Sensitive Data Exposure","filePath":"../ruby/railsgoat/dependency-check-report.html","textRange":{"startLine":1,"endLine":1,"startColumn":0,"endColumn":1}},"effortMinutes":0,"severity":"MAJOR","type":"VULNERABILITY"}]}

附言:请参阅ruleId了解差异。

首先,我通过格式化程序运行了您的json,但它被报告为无效。如果你要有多个对象,你应该使用一个数组。所以我把它调整为:[{...},{...}]。(这是因为JSON期望只有一个根元素。(

我认为最简单的说法是,你正在做JSON.parser已经应该做的工作。你可以直接从解析器中迭代对象:JSON.parse(File.read("/tmp/tes.json")).map{ |obj| obj["Raw"] }

这给了我=> ["vulnerable-to-driveby-", "39a6bda16ef9583fba2696cc3efde0da"]的结果

最新更新