| 
									
										
										
										
											2024-07-14 00:24:16 +00:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-20 10:22:39 -08:00
										 |  |  | require "rubocops/extend/formula_cop" | 
					
						
							| 
									
										
										
										
											2017-05-22 13:09:49 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module FormulaAudit | 
					
						
							| 
									
										
										
										
											2025-06-19 10:13:32 +01:00
										 |  |  |       # This cop ensures that caveats don't have problematic text or logic. | 
					
						
							| 
									
										
										
										
											2020-08-26 02:21:58 +02:00
										 |  |  |       # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |       # ### Example | 
					
						
							| 
									
										
										
										
											2020-08-26 02:21:58 +02:00
										 |  |  |       # | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |       # ```ruby | 
					
						
							|  |  |  |       # # bad | 
					
						
							|  |  |  |       # def caveats | 
					
						
							| 
									
										
										
										
											2025-06-19 10:13:32 +01:00
										 |  |  |       #   if File.exist?("/etc/issue") | 
					
						
							|  |  |  |       #     "This caveat only when file exists that won't work with JSON API." | 
					
						
							|  |  |  |       #   end | 
					
						
							|  |  |  |       # end | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # # good | 
					
						
							|  |  |  |       # def caveats | 
					
						
							|  |  |  |       #   "This caveat always works regardless of the JSON API." | 
					
						
							|  |  |  |       # end | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # # bad | 
					
						
							|  |  |  |       # def caveats | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |       #   <<~EOS | 
					
						
							| 
									
										
										
										
											2024-12-28 15:52:04 -05:00
										 |  |  |       #     Use `setuid` to allow running the executable by non-root users. | 
					
						
							| 
									
										
										
										
											2024-04-26 20:55:51 +02:00
										 |  |  |       #   EOS | 
					
						
							|  |  |  |       # end | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # # good | 
					
						
							|  |  |  |       # def caveats | 
					
						
							|  |  |  |       #   <<~EOS | 
					
						
							|  |  |  |       #     Use `sudo` to run the executable. | 
					
						
							|  |  |  |       #   EOS | 
					
						
							|  |  |  |       # end | 
					
						
							|  |  |  |       # ``` | 
					
						
							| 
									
										
										
										
											2023-02-20 18:10:59 -08:00
										 |  |  |       class Caveats < FormulaCop | 
					
						
							| 
									
										
										
										
											2024-07-07 15:18:29 -04:00
										 |  |  |         sig { override.params(_formula_nodes: FormulaNodes).void } | 
					
						
							|  |  |  |         def audit_formula(_formula_nodes) | 
					
						
							| 
									
										
										
										
											2017-05-22 13:09:49 +05:30
										 |  |  |           caveats_strings.each do |n| | 
					
						
							| 
									
										
										
										
											2023-01-16 01:24:23 -05:00
										 |  |  |             if regex_match_group(n, /\bsetuid\b/i) | 
					
						
							| 
									
										
										
										
											2025-05-30 16:42:32 -04:00
										 |  |  |               problem "Instead of recommending `setuid` in the caveats, suggest `sudo`." | 
					
						
							| 
									
										
										
										
											2023-01-16 01:24:23 -05:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-16 01:24:23 -05:00
										 |  |  |             problem "Don't use ANSI escape codes in the caveats." if regex_match_group(n, /\e/) | 
					
						
							| 
									
										
										
										
											2017-05-22 13:09:49 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2025-06-19 10:13:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-23 08:58:34 +01:00
										 |  |  |           return if formula_tap != "homebrew-core" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-19 10:13:32 +01:00
										 |  |  |           # Forbid dynamic logic in caveats (only if/else/unless) | 
					
						
							|  |  |  |           caveats_method = find_method_def(@body, :caveats) | 
					
						
							|  |  |  |           return unless caveats_method | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           dynamic_nodes = caveats_method.each_descendant.select do |descendant| | 
					
						
							|  |  |  |             descendant.type == :if | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           dynamic_nodes.each do |node| | 
					
						
							|  |  |  |             @offensive_node = node | 
					
						
							|  |  |  |             problem "Don't use dynamic logic (if/else/unless) in caveats." | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-05-22 13:09:49 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |