Skip to content

Commit f983e4e

Browse files
committed
Copilot feedback implemented, and formatted with Fantomas
1 parent 4223357 commit f983e4e

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

src/SwaggerProvider.DesignTime/Utils.fs

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,57 @@ module SchemaReader =
3131

3232
// Block localhost and loopback, and private IP ranges using proper IP address parsing
3333
let isIp, ipAddr = System.Net.IPAddress.TryParse(host)
34+
3435
if isIp then
3536
// Loopback
36-
if System.Net.IPAddress.IsLoopback(ipAddr) || ipAddr.ToString() = "0.0.0.0" then
37+
if
38+
System.Net.IPAddress.IsLoopback(ipAddr)
39+
|| ipAddr.ToString() = "0.0.0.0"
40+
then
3741
failwithf "Cannot fetch schemas from localhost/loopback addresses: %s (set SsrfProtection=false for development)" host
3842
// Private IPv4 ranges
3943
let bytes = ipAddr.GetAddressBytes()
44+
4045
let isPrivate =
4146
// 10.0.0.0/8
42-
(ipAddr.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork && bytes.[0] = 10uy)
47+
(ipAddr.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork
48+
&& bytes.[0] = 10uy)
4349
// 172.16.0.0/12
44-
|| (ipAddr.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork && bytes.[0] = 172uy && bytes.[1] >= 16uy && bytes.[1] <= 31uy)
50+
|| (ipAddr.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork
51+
&& bytes.[0] = 172uy
52+
&& bytes.[1] >= 16uy
53+
&& bytes.[1] <= 31uy)
4554
// 192.168.0.0/16
46-
|| (ipAddr.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork && bytes.[0] = 192uy && bytes.[1] = 168uy)
55+
|| (ipAddr.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork
56+
&& bytes.[0] = 192uy
57+
&& bytes.[1] = 168uy)
4758
// Link-local 169.254.0.0/16
48-
|| (ipAddr.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork && bytes.[0] = 169uy && bytes.[1] = 254uy)
59+
|| (ipAddr.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork
60+
&& bytes.[0] = 169uy
61+
&& bytes.[1] = 254uy)
62+
4963
if isPrivate then
5064
failwithf "Cannot fetch schemas from private or link-local IP addresses: %s (set SsrfProtection=false for development)" host
51-
else
65+
else if
5266
// Block localhost by name
53-
if host = "localhost" then
54-
failwithf "Cannot fetch schemas from localhost/loopback addresses: %s (set SsrfProtection=false for development)" host
67+
host = "localhost"
68+
then
69+
failwithf "Cannot fetch schemas from localhost/loopback addresses: %s (set SsrfProtection=false for development)" host
70+
71+
let validateContentType(contentType: Headers.MediaTypeHeaderValue) =
72+
if not(isNull contentType) then
73+
let mediaType = contentType.MediaType.ToLowerInvariant()
74+
75+
if
76+
not(
77+
mediaType.Contains "json"
78+
|| mediaType.Contains "yaml"
79+
|| mediaType.Contains "text"
80+
|| mediaType.Contains "application/octet-stream"
81+
)
82+
then
83+
failwithf "Invalid Content-Type for schema: %s. Expected JSON or YAML." mediaType
84+
5585
let readSchemaPath (ignoreSsrfProtection: bool) (headersStr: string) (schemaPathRaw: string) =
5686
async {
5787
let uri = Uri schemaPathRaw
@@ -82,20 +112,7 @@ module SchemaReader =
82112
let! response = client.SendAsync request |> Async.AwaitTask
83113

84114
// Validate Content-Type to ensure we're parsing the correct format
85-
let contentType = response.Content.Headers.ContentType
86-
87-
if not(isNull contentType) then
88-
let mediaType = contentType.MediaType.ToLowerInvariant()
89-
90-
if
91-
not(
92-
mediaType.Contains "json"
93-
|| mediaType.Contains "yaml"
94-
|| mediaType.Contains "text"
95-
|| mediaType.Contains "application/octet-stream"
96-
)
97-
then
98-
failwithf "Invalid Content-Type for schema: %s. Expected JSON or YAML." mediaType
115+
validateContentType response.Content.Headers.ContentType
99116

100117
return! response.Content.ReadAsStringAsync() |> Async.AwaitTask
101118
}
@@ -152,6 +169,10 @@ module SchemaReader =
152169
let! res =
153170
async {
154171
let! response = client.SendAsync(request) |> Async.AwaitTask
172+
173+
// Validate Content-Type to ensure we're parsing the correct format
174+
validateContentType response.Content.Headers.ContentType
175+
155176
return! response.Content.ReadAsStringAsync() |> Async.AwaitTask
156177
}
157178
|> Async.Catch

0 commit comments

Comments
 (0)