sarif-ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +5 -0
  3. data/CODE_OF_CONDUCT.md +10 -0
  4. data/LICENSE +21 -0
  5. data/README.md +191 -0
  6. data/Rakefile +10 -0
  7. data/lib/sarif/address.rb +67 -0
  8. data/lib/sarif/artifact.rb +76 -0
  9. data/lib/sarif/artifact_change.rb +46 -0
  10. data/lib/sarif/artifact_content.rb +49 -0
  11. data/lib/sarif/artifact_location.rb +52 -0
  12. data/lib/sarif/attachment.rb +52 -0
  13. data/lib/sarif/code_flow.rb +46 -0
  14. data/lib/sarif/configuration_override.rb +46 -0
  15. data/lib/sarif/conversion.rb +49 -0
  16. data/lib/sarif/edge.rb +52 -0
  17. data/lib/sarif/edge_traversal.rb +52 -0
  18. data/lib/sarif/exception.rb +52 -0
  19. data/lib/sarif/external_properties.rb +100 -0
  20. data/lib/sarif/external_property_file_reference.rb +49 -0
  21. data/lib/sarif/external_property_file_references.rb +88 -0
  22. data/lib/sarif/fix.rb +46 -0
  23. data/lib/sarif/graph.rb +49 -0
  24. data/lib/sarif/graph_traversal.rb +58 -0
  25. data/lib/sarif/invocation.rb +115 -0
  26. data/lib/sarif/location.rb +58 -0
  27. data/lib/sarif/location_relationship.rb +49 -0
  28. data/lib/sarif/log.rb +52 -0
  29. data/lib/sarif/logical_location.rb +58 -0
  30. data/lib/sarif/message.rb +52 -0
  31. data/lib/sarif/multiformat_message_string.rb +46 -0
  32. data/lib/sarif/node.rb +52 -0
  33. data/lib/sarif/notification.rb +64 -0
  34. data/lib/sarif/physical_location.rb +52 -0
  35. data/lib/sarif/property_bag.rb +40 -0
  36. data/lib/sarif/rectangle.rb +55 -0
  37. data/lib/sarif/region.rb +73 -0
  38. data/lib/sarif/replacement.rb +46 -0
  39. data/lib/sarif/reporting_configuration.rb +52 -0
  40. data/lib/sarif/reporting_descriptor.rb +79 -0
  41. data/lib/sarif/reporting_descriptor_reference.rb +52 -0
  42. data/lib/sarif/reporting_descriptor_relationship.rb +49 -0
  43. data/lib/sarif/result.rb +127 -0
  44. data/lib/sarif/result_provenance.rb +58 -0
  45. data/lib/sarif/run.rb +121 -0
  46. data/lib/sarif/run_automation_details.rb +52 -0
  47. data/lib/sarif/schema/sarif-schema-2.1.0.json +3389 -0
  48. data/lib/sarif/special_locations.rb +43 -0
  49. data/lib/sarif/stack.rb +46 -0
  50. data/lib/sarif/stack_frame.rb +52 -0
  51. data/lib/sarif/suppression.rb +55 -0
  52. data/lib/sarif/thread_flow.rb +55 -0
  53. data/lib/sarif/thread_flow_location.rb +79 -0
  54. data/lib/sarif/tool.rb +46 -0
  55. data/lib/sarif/tool_component.rb +121 -0
  56. data/lib/sarif/tool_component_reference.rb +49 -0
  57. data/lib/sarif/translation_metadata.rb +58 -0
  58. data/lib/sarif/version.rb +5 -0
  59. data/lib/sarif/version_control_details.rb +58 -0
  60. data/lib/sarif/web_request.rb +64 -0
  61. data/lib/sarif/web_response.rb +64 -0
  62. data/lib/sarif.rb +121 -0
  63. data/sig/sarif.rbs +4 -0
  64. metadata +106 -0
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sarif
4
+ # Describes an HTTP request.
5
+ class WebRequest
6
+ attr_accessor :index, :protocol, :version, :target, :method, :headers, :parameters, :body, :properties
7
+
8
+ def initialize(index: -1, protocol: nil, version: nil, target: nil, method: nil, headers: nil, parameters: nil, body: nil, properties: nil)
9
+ @index = index
10
+ @protocol = protocol
11
+ @version = version
12
+ @target = target
13
+ @method = method
14
+ @headers = headers
15
+ @parameters = parameters
16
+ @body = body
17
+ @properties = properties
18
+ end
19
+
20
+ def to_h
21
+ h = {}
22
+ h["index"] = @index if @index && @index != -1
23
+ h["protocol"] = @protocol unless @protocol.nil?
24
+ h["version"] = @version unless @version.nil?
25
+ h["target"] = @target unless @target.nil?
26
+ h["method"] = @method unless @method.nil?
27
+ h["headers"] = @headers unless @headers.nil?
28
+ h["parameters"] = @parameters unless @parameters.nil?
29
+ h["body"] = @body&.to_h unless @body.nil?
30
+ h["properties"] = @properties unless @properties.nil?
31
+ h
32
+ end
33
+
34
+ def to_json(pretty: false)
35
+ pretty ? JSON.pretty_generate(to_h) : JSON.generate(to_h)
36
+ end
37
+
38
+ def self.from_hash(h)
39
+ return nil if h.nil?
40
+ new(
41
+ index: h["index"] || -1,
42
+ protocol: h["protocol"],
43
+ version: h["version"],
44
+ target: h["target"],
45
+ method: h["method"],
46
+ headers: h["headers"],
47
+ parameters: h["parameters"],
48
+ body: ArtifactContent.from_hash(h["body"]),
49
+ properties: h["properties"]
50
+ )
51
+ end
52
+
53
+ def ==(other)
54
+ return false unless other.is_a?(WebRequest)
55
+ @index == other.index && @protocol == other.protocol && @version == other.version && @target == other.target && @method == other.method && @headers == other.headers && @parameters == other.parameters && @body == other.body && @properties == other.properties
56
+ end
57
+
58
+ alias eql? ==
59
+
60
+ def hash
61
+ [@index, @protocol, @version, @target, @method, @headers, @parameters, @body, @properties].hash
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sarif
4
+ # Describes the response to an HTTP request.
5
+ class WebResponse
6
+ attr_accessor :index, :protocol, :version, :status_code, :reason_phrase, :headers, :body, :no_response_received, :properties
7
+
8
+ def initialize(index: -1, protocol: nil, version: nil, status_code: nil, reason_phrase: nil, headers: nil, body: nil, no_response_received: false, properties: nil)
9
+ @index = index
10
+ @protocol = protocol
11
+ @version = version
12
+ @status_code = status_code
13
+ @reason_phrase = reason_phrase
14
+ @headers = headers
15
+ @body = body
16
+ @no_response_received = no_response_received
17
+ @properties = properties
18
+ end
19
+
20
+ def to_h
21
+ h = {}
22
+ h["index"] = @index if @index && @index != -1
23
+ h["protocol"] = @protocol unless @protocol.nil?
24
+ h["version"] = @version unless @version.nil?
25
+ h["statusCode"] = @status_code unless @status_code.nil?
26
+ h["reasonPhrase"] = @reason_phrase unless @reason_phrase.nil?
27
+ h["headers"] = @headers unless @headers.nil?
28
+ h["body"] = @body&.to_h unless @body.nil?
29
+ h["noResponseReceived"] = @no_response_received if @no_response_received
30
+ h["properties"] = @properties unless @properties.nil?
31
+ h
32
+ end
33
+
34
+ def to_json(pretty: false)
35
+ pretty ? JSON.pretty_generate(to_h) : JSON.generate(to_h)
36
+ end
37
+
38
+ def self.from_hash(h)
39
+ return nil if h.nil?
40
+ new(
41
+ index: h["index"] || -1,
42
+ protocol: h["protocol"],
43
+ version: h["version"],
44
+ status_code: h["statusCode"],
45
+ reason_phrase: h["reasonPhrase"],
46
+ headers: h["headers"],
47
+ body: ArtifactContent.from_hash(h["body"]),
48
+ no_response_received: h.key?("noResponseReceived") ? h["noResponseReceived"] : false,
49
+ properties: h["properties"]
50
+ )
51
+ end
52
+
53
+ def ==(other)
54
+ return false unless other.is_a?(WebResponse)
55
+ @index == other.index && @protocol == other.protocol && @version == other.version && @status_code == other.status_code && @reason_phrase == other.reason_phrase && @headers == other.headers && @body == other.body && @no_response_received == other.no_response_received && @properties == other.properties
56
+ end
57
+
58
+ alias eql? ==
59
+
60
+ def hash
61
+ [@index, @protocol, @version, @status_code, @reason_phrase, @headers, @body, @no_response_received, @properties].hash
62
+ end
63
+ end
64
+ end
data/lib/sarif.rb ADDED
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ require_relative "sarif/version"
6
+
7
+ # Generated classes
8
+ require_relative "sarif/address"
9
+ require_relative "sarif/artifact"
10
+ require_relative "sarif/artifact_change"
11
+ require_relative "sarif/artifact_content"
12
+ require_relative "sarif/artifact_location"
13
+ require_relative "sarif/attachment"
14
+ require_relative "sarif/code_flow"
15
+ require_relative "sarif/configuration_override"
16
+ require_relative "sarif/conversion"
17
+ require_relative "sarif/edge"
18
+ require_relative "sarif/edge_traversal"
19
+ require_relative "sarif/exception"
20
+ require_relative "sarif/external_properties"
21
+ require_relative "sarif/external_property_file_reference"
22
+ require_relative "sarif/external_property_file_references"
23
+ require_relative "sarif/fix"
24
+ require_relative "sarif/graph"
25
+ require_relative "sarif/graph_traversal"
26
+ require_relative "sarif/invocation"
27
+ require_relative "sarif/location"
28
+ require_relative "sarif/location_relationship"
29
+ require_relative "sarif/log"
30
+ require_relative "sarif/logical_location"
31
+ require_relative "sarif/message"
32
+ require_relative "sarif/multiformat_message_string"
33
+ require_relative "sarif/node"
34
+ require_relative "sarif/notification"
35
+ require_relative "sarif/physical_location"
36
+ require_relative "sarif/property_bag"
37
+ require_relative "sarif/rectangle"
38
+ require_relative "sarif/region"
39
+ require_relative "sarif/replacement"
40
+ require_relative "sarif/reporting_configuration"
41
+ require_relative "sarif/reporting_descriptor"
42
+ require_relative "sarif/reporting_descriptor_reference"
43
+ require_relative "sarif/reporting_descriptor_relationship"
44
+ require_relative "sarif/result"
45
+ require_relative "sarif/result_provenance"
46
+ require_relative "sarif/run"
47
+ require_relative "sarif/run_automation_details"
48
+ require_relative "sarif/special_locations"
49
+ require_relative "sarif/stack"
50
+ require_relative "sarif/stack_frame"
51
+ require_relative "sarif/suppression"
52
+ require_relative "sarif/thread_flow"
53
+ require_relative "sarif/thread_flow_location"
54
+ require_relative "sarif/tool"
55
+ require_relative "sarif/tool_component"
56
+ require_relative "sarif/tool_component_reference"
57
+ require_relative "sarif/translation_metadata"
58
+ require_relative "sarif/version_control_details"
59
+ require_relative "sarif/web_request"
60
+ require_relative "sarif/web_response"
61
+
62
+ module Sarif
63
+ class Error < StandardError; end
64
+ class ParseError < Error; end
65
+ class ValidationError < Error
66
+ attr_reader :errors
67
+
68
+ def initialize(errors)
69
+ @errors = errors
70
+ super("SARIF validation failed: #{errors.first["error"]}")
71
+ end
72
+ end
73
+
74
+ SCHEMA_PATH = File.expand_path("sarif/schema/sarif-schema-2.1.0.json", __dir__)
75
+
76
+ def self.load(path)
77
+ parse(File.read(path))
78
+ rescue Errno::ENOENT
79
+ raise Error, "File not found: #{path}"
80
+ end
81
+
82
+ def self.parse(json_string)
83
+ data = JSON.parse(json_string)
84
+ Log.from_hash(data)
85
+ rescue JSON::ParserError => e
86
+ raise ParseError, "Invalid JSON: #{e.message}"
87
+ rescue ArgumentError => e
88
+ raise ParseError, "Invalid SARIF structure: #{e.message}"
89
+ end
90
+
91
+ def self.dump(log, path_or_io, pretty: false)
92
+ json = log.to_json(pretty: pretty)
93
+ if path_or_io.respond_to?(:write)
94
+ path_or_io.write(json)
95
+ else
96
+ File.write(path_or_io, json)
97
+ end
98
+ json
99
+ end
100
+
101
+ def self.schema
102
+ @schema ||= begin
103
+ require "json_schemer"
104
+ JSONSchemer.schema(Pathname.new(SCHEMA_PATH))
105
+ end
106
+ end
107
+
108
+ def self.validate(log)
109
+ schema.validate(log.to_h).to_a
110
+ end
111
+
112
+ def self.valid?(log)
113
+ validate(log).empty?
114
+ end
115
+
116
+ def self.validate!(log)
117
+ errors = validate(log)
118
+ raise ValidationError, errors unless errors.empty?
119
+ true
120
+ end
121
+ end
data/sig/sarif.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Sarif
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sarif-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Nesbitt
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A Ruby library for creating, reading, and manipulating SARIF 2.1.0 files.
13
+ SARIF is an OASIS standard format for the output of static analysis tools.
14
+ email:
15
+ - andrewnez@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - CODE_OF_CONDUCT.md
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - lib/sarif.rb
26
+ - lib/sarif/address.rb
27
+ - lib/sarif/artifact.rb
28
+ - lib/sarif/artifact_change.rb
29
+ - lib/sarif/artifact_content.rb
30
+ - lib/sarif/artifact_location.rb
31
+ - lib/sarif/attachment.rb
32
+ - lib/sarif/code_flow.rb
33
+ - lib/sarif/configuration_override.rb
34
+ - lib/sarif/conversion.rb
35
+ - lib/sarif/edge.rb
36
+ - lib/sarif/edge_traversal.rb
37
+ - lib/sarif/exception.rb
38
+ - lib/sarif/external_properties.rb
39
+ - lib/sarif/external_property_file_reference.rb
40
+ - lib/sarif/external_property_file_references.rb
41
+ - lib/sarif/fix.rb
42
+ - lib/sarif/graph.rb
43
+ - lib/sarif/graph_traversal.rb
44
+ - lib/sarif/invocation.rb
45
+ - lib/sarif/location.rb
46
+ - lib/sarif/location_relationship.rb
47
+ - lib/sarif/log.rb
48
+ - lib/sarif/logical_location.rb
49
+ - lib/sarif/message.rb
50
+ - lib/sarif/multiformat_message_string.rb
51
+ - lib/sarif/node.rb
52
+ - lib/sarif/notification.rb
53
+ - lib/sarif/physical_location.rb
54
+ - lib/sarif/property_bag.rb
55
+ - lib/sarif/rectangle.rb
56
+ - lib/sarif/region.rb
57
+ - lib/sarif/replacement.rb
58
+ - lib/sarif/reporting_configuration.rb
59
+ - lib/sarif/reporting_descriptor.rb
60
+ - lib/sarif/reporting_descriptor_reference.rb
61
+ - lib/sarif/reporting_descriptor_relationship.rb
62
+ - lib/sarif/result.rb
63
+ - lib/sarif/result_provenance.rb
64
+ - lib/sarif/run.rb
65
+ - lib/sarif/run_automation_details.rb
66
+ - lib/sarif/schema/sarif-schema-2.1.0.json
67
+ - lib/sarif/special_locations.rb
68
+ - lib/sarif/stack.rb
69
+ - lib/sarif/stack_frame.rb
70
+ - lib/sarif/suppression.rb
71
+ - lib/sarif/thread_flow.rb
72
+ - lib/sarif/thread_flow_location.rb
73
+ - lib/sarif/tool.rb
74
+ - lib/sarif/tool_component.rb
75
+ - lib/sarif/tool_component_reference.rb
76
+ - lib/sarif/translation_metadata.rb
77
+ - lib/sarif/version.rb
78
+ - lib/sarif/version_control_details.rb
79
+ - lib/sarif/web_request.rb
80
+ - lib/sarif/web_response.rb
81
+ - sig/sarif.rbs
82
+ homepage: https://github.com/andrew/sarif
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ homepage_uri: https://github.com/andrew/sarif
87
+ source_code_uri: https://github.com/andrew/sarif
88
+ changelog_uri: https://github.com/andrew/sarif/blob/main/CHANGELOG.md
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.2.0
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 4.0.1
104
+ specification_version: 4
105
+ summary: Ruby SDK for SARIF (Static Analysis Results Interchange Format)
106
+ test_files: []