| 
									
										
										
										
											2023-03-04 22:23:32 +00:00
										 |  |  | # typed: true | 
					
						
							|  |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module Cask | 
					
						
							|  |  |  |       # This cop checks that a cask's `url` stanza is formatted correctly. | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @example | 
					
						
							|  |  |  |       #   # bad | 
					
						
							| 
									
										
										
										
											2023-04-02 15:29:17 +01:00
										 |  |  |       #   url "https://example.com/download/foo.dmg", | 
					
						
							| 
									
										
										
										
											2023-04-02 16:38:33 +01:00
										 |  |  |       #       verified: "https://example.com/download" | 
					
						
							| 
									
										
										
										
											2023-03-04 22:23:32 +00:00
										 |  |  |       # | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       #   # good | 
					
						
							| 
									
										
										
										
											2023-04-02 15:29:17 +01:00
										 |  |  |       #   url "https://example.com/download/foo.dmg", | 
					
						
							| 
									
										
										
										
											2023-04-02 16:38:33 +01:00
										 |  |  |       #       verified: "example.com/download/" | 
					
						
							| 
									
										
										
										
											2023-03-04 22:23:32 +00:00
										 |  |  |       # | 
					
						
							|  |  |  |       class Url < Base | 
					
						
							|  |  |  |         extend AutoCorrector | 
					
						
							|  |  |  |         extend Forwardable | 
					
						
							|  |  |  |         include OnUrlStanza | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def on_url_stanza(stanza) | 
					
						
							|  |  |  |           return if stanza.stanza_node.block_type? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-02 23:00:14 +01:00
										 |  |  |           url_stanza = stanza.stanza_node.first_argument | 
					
						
							| 
									
										
										
										
											2023-03-04 22:23:32 +00:00
										 |  |  |           hash_node = stanza.stanza_node.last_argument | 
					
						
							|  |  |  |           return unless hash_node.hash_type? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           hash_node.each_pair do |key_node, value_node| | 
					
						
							| 
									
										
										
										
											2023-04-18 15:06:50 -07:00
										 |  |  |             next if key_node.source != "verified" | 
					
						
							| 
									
										
										
										
											2023-03-04 22:23:32 +00:00
										 |  |  |             next unless value_node.str_type? | 
					
						
							| 
									
										
										
										
											2023-04-02 15:29:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if value_node.source.start_with?(%r{^"https?://}) | 
					
						
							|  |  |  |               add_offense( | 
					
						
							|  |  |  |                 value_node.source_range, | 
					
						
							| 
									
										
										
										
											2023-04-02 22:43:52 +01:00
										 |  |  |                 message: "Verified URL parameter value should not contain a URL scheme.", | 
					
						
							| 
									
										
										
										
											2023-04-02 15:29:17 +01:00
										 |  |  |               ) do |corrector| | 
					
						
							|  |  |  |                 corrector.replace(value_node.source_range, value_node.source.gsub(%r{^"https?://}, "\"")) | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-02 19:39:23 +01:00
										 |  |  |             # Skip if the URL and the verified value are the same. | 
					
						
							| 
									
										
										
										
											2023-04-02 23:00:14 +01:00
										 |  |  |             next if value_node.source == url_stanza.source.gsub(%r{^"https?://}, "\"") | 
					
						
							| 
									
										
										
										
											2023-04-04 16:11:44 +01:00
										 |  |  |             # Skip if the URL has two path components, eg: `https://github.com/google/fonts.git`. | 
					
						
							|  |  |  |             next if url_stanza.source.gsub(%r{^"https?://}, "\"").count("/") == 2
 | 
					
						
							| 
									
										
										
										
											2023-04-02 19:39:23 +01:00
										 |  |  |             # Skip if the verified value ends with a slash. | 
					
						
							| 
									
										
										
										
											2023-04-02 15:29:17 +01:00
										 |  |  |             next if value_node.str_content.end_with?("/") | 
					
						
							| 
									
										
										
										
											2023-03-04 22:23:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             add_offense( | 
					
						
							|  |  |  |               value_node.source_range, | 
					
						
							| 
									
										
										
										
											2023-04-02 15:29:17 +01:00
										 |  |  |               message: "Verified URL parameter value should end with a /.", | 
					
						
							| 
									
										
										
										
											2023-03-04 22:23:32 +00:00
										 |  |  |             ) do |corrector| | 
					
						
							| 
									
										
										
										
											2023-04-02 15:29:17 +01:00
										 |  |  |               corrector.replace(value_node.source_range, value_node.source.gsub(/"$/, "/\"")) | 
					
						
							| 
									
										
										
										
											2023-03-04 22:23:32 +00:00
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |