mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-31 01:52:26 -05:00 
			
		
		
		
	Additional IP range validations (#1152)
* [bugfix] Ensure requests happen over TCP It's possible for the network to be udp4 or udp6. This is rather unlikely to occur, but since we're given the network anyway as part of the Sanitize function getting called we might as well check for it. * [chore] Align reserved v6 blocks to IANA registry * [chore] Add test for ValidateIP The net and netip packages diverge in that net.ParseIP will consider an IPv4-mapped address to be an IPv4 address and as such it would get caught by the IPv4Reserved list. However, netip considers it an IPv6 address, so we need to ensure the mapped range is in IPv6Reserved. * [chore] Align reserved v4 blocks to IANA registry This includes a number of tests for /32's explicitly called out in the registry to ensure we always consider those invalid.
This commit is contained in:
		
					parent
					
						
							
								e6cd81babc
							
						
					
				
			
			
				commit
				
					
						746f3fa4e6
					
				
			
		
					 4 changed files with 89 additions and 6 deletions
				
			
		
							
								
								
									
										54
									
								
								internal/netutil/validate_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								internal/netutil/validate_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,54 @@ | |||
| package netutil | ||||
| 
 | ||||
| import ( | ||||
| 	"net/netip" | ||||
| 	"testing" | ||||
| ) | ||||
| 
 | ||||
| func TestValidateIP(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		name string | ||||
| 		ip   netip.Addr | ||||
| 	}{ | ||||
| 		// IPv4 tests | ||||
| 		{ | ||||
| 			name: "IPv4 this host on this network", | ||||
| 			ip:   netip.MustParseAddr("0.0.0.0"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			name: "IPv4 dummy address", | ||||
| 			ip:   netip.MustParseAddr("192.0.0.8"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			name: "IPv4 Port Control Protocol Anycast", | ||||
| 			ip:   netip.MustParseAddr("192.0.0.9"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			name: "IPv4 Traversal Using Relays around NAT Anycast", | ||||
| 			ip:   netip.MustParseAddr("192.0.0.10"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			name: "IPv4 NAT64/DNS64 Discovery 1", | ||||
| 			ip:   netip.MustParseAddr("192.0.0.17"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			name: "IPv4 NAT64/DNS64 Discovery 2", | ||||
| 			ip:   netip.MustParseAddr("192.0.0.171"), | ||||
| 		}, | ||||
| 		// IPv6 tests | ||||
| 		{ | ||||
| 			name: "IPv4-mapped address", | ||||
| 			ip:   netip.MustParseAddr("::ffff:169.254.169.254"), | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	for _, tc := range tests { | ||||
| 		tc := tc | ||||
| 		t.Run(tc.name, func(t *testing.T) { | ||||
| 			t.Parallel() | ||||
| 			if valid := ValidateIP(tc.ip); valid != false { | ||||
| 				t.Fatalf("Expected IP %s to be: %t, got: %t", tc.ip, false, valid) | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue