| 1 |
use crate::{httpdata::HttpData, media_types::RDAP_MEDIA_TYPE, prelude::ExtensionId}; |
| 2 |
|
| 3 |
use super::{Check, Checks, GetChecks}; |
| 4 |
|
| 5 |
impl GetChecks for HttpData { |
| 6 |
fn get_checks( |
| 7 |
&self, |
| 8 |
index: Option<usize>, |
| 9 |
params: crate::check::CheckParams, |
| 10 |
) -> crate::check::Checks { |
| 11 |
let mut items = vec![]; |
| 12 |
|
| 13 |
// RFC checks |
| 14 |
if let Some(allow_origin) = &self.access_control_allow_origin { |
| 15 |
if !allow_origin.eq("*") { |
| 16 |
items.push(Check::CorsAllowOriginStarRecommended.check_item()) |
| 17 |
} |
| 18 |
} else { |
| 19 |
items.push(Check::CorsAllowOriginRecommended.check_item()) |
| 20 |
} |
| 21 |
if self.access_control_allow_credentials.is_some() { |
| 22 |
items.push(Check::CorsAllowCredentialsNotRecommended.check_item()) |
| 23 |
} |
| 24 |
if let Some(content_type) = &self.content_type { |
| 25 |
if !content_type |
| 26 |
.to_lowercase() |
| 27 |
.starts_with(&RDAP_MEDIA_TYPE.to_lowercase()) |
| 28 |
{ |
| 29 |
items.push(Check::ContentTypeIsNotRdap.check_item()); |
| 30 |
} |
| 31 |
} else { |
| 32 |
items.push(Check::ContentTypeIsAbsent.check_item()); |
| 33 |
} |
| 34 |
|
| 35 |
// checks for Gtld profile |
| 36 |
if params |
| 37 |
.root |
| 38 |
.has_extension_id(ExtensionId::IcannRdapTechnicalImplementationGuide0) |
| 39 |
|| params |
| 40 |
.root |
| 41 |
.has_extension_id(ExtensionId::IcannRdapTechnicalImplementationGuide1) |
| 42 |
{ |
| 43 |
if let Some(scheme) = &self.scheme { |
| 44 |
if !scheme.eq_ignore_ascii_case("HTTPS") { |
| 45 |
items.push(Check::MustUseHttps.check_item()); |
| 46 |
} |
| 47 |
} else { |
| 48 |
items.push(Check::MustUseHttps.check_item()); |
| 49 |
} |
| 50 |
if let Some(allow_origin) = &self.access_control_allow_origin { |
| 51 |
if !allow_origin.eq("*") { |
| 52 |
items.push(Check::AllowOriginNotStar.check_item()) |
| 53 |
} |
| 54 |
} else { |
| 55 |
items.push(Check::AllowOriginNotStar.check_item()) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
Checks { |
| 60 |
rdap_struct: super::RdapStructure::HttpData, |
| 61 |
index, |
| 62 |
items, |
| 63 |
sub_checks: vec![], |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#[cfg(test)] |
| 69 |
mod tests { |
| 70 |
use crate::{ |
| 71 |
check::{Check, CheckParams, GetChecks}, |
| 72 |
httpdata::HttpData, |
| 73 |
media_types::{JSON_MEDIA_TYPE, RDAP_MEDIA_TYPE}, |
| 74 |
prelude::{Common, ExtensionId, ObjectCommon, ToResponse}, |
| 75 |
response::domain::Domain, |
| 76 |
}; |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn check_not_rdap_media() { |
| 80 |
// GIVEN an rdap response |
| 81 |
let domain = Domain { |
| 82 |
common: Common::level0() |
| 83 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 84 |
.build(), |
| 85 |
object_common: ObjectCommon::domain().build(), |
| 86 |
ldh_name: Some("foo.example".to_string()), |
| 87 |
unicode_name: None, |
| 88 |
variants: None, |
| 89 |
secure_dns: None, |
| 90 |
nameservers: None, |
| 91 |
public_ids: None, |
| 92 |
network: None, |
| 93 |
ttl0_data: None, |
| 94 |
}; |
| 95 |
let rdap = domain.to_response(); |
| 96 |
|
| 97 |
// and GIVEN httpdata with content type that is not RDAP media type |
| 98 |
let http_data = HttpData::example().content_type(JSON_MEDIA_TYPE).build(); |
| 99 |
|
| 100 |
// WHEN checks are run |
| 101 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 102 |
|
| 103 |
// THEN incorrect media type check is found |
| 104 |
assert!( |
| 105 |
checks |
| 106 |
.items |
| 107 |
.iter() |
| 108 |
.any(|c| c.check == Check::ContentTypeIsNotRdap) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn check_exactly_rdap_media() { |
| 114 |
// GIVEN an rdap response |
| 115 |
let domain = Domain { |
| 116 |
common: Common::level0() |
| 117 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 118 |
.build(), |
| 119 |
object_common: ObjectCommon::domain().build(), |
| 120 |
ldh_name: Some("foo.example".to_string()), |
| 121 |
unicode_name: None, |
| 122 |
variants: None, |
| 123 |
secure_dns: None, |
| 124 |
nameservers: None, |
| 125 |
public_ids: None, |
| 126 |
network: None, |
| 127 |
ttl0_data: None, |
| 128 |
}; |
| 129 |
let rdap = domain.to_response(); |
| 130 |
|
| 131 |
// and GIVEN httpdata with content type that is not RDAP media type |
| 132 |
let http_data = HttpData::example().content_type(RDAP_MEDIA_TYPE).build(); |
| 133 |
|
| 134 |
// WHEN checks are run |
| 135 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 136 |
|
| 137 |
// THEN incorrect media type check is not found |
| 138 |
assert!( |
| 139 |
!checks |
| 140 |
.items |
| 141 |
.iter() |
| 142 |
.any(|c| c.check == Check::ContentTypeIsNotRdap) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn check_rdap_media_with_charset_parameter() { |
| 148 |
// GIVEN an rdap response |
| 149 |
let domain = Domain { |
| 150 |
common: Common::level0() |
| 151 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 152 |
.build(), |
| 153 |
object_common: ObjectCommon::domain().build(), |
| 154 |
ldh_name: Some("foo.example".to_string()), |
| 155 |
unicode_name: None, |
| 156 |
variants: None, |
| 157 |
secure_dns: None, |
| 158 |
nameservers: None, |
| 159 |
public_ids: None, |
| 160 |
network: None, |
| 161 |
ttl0_data: None, |
| 162 |
}; |
| 163 |
let rdap = domain.to_response(); |
| 164 |
|
| 165 |
// and GIVEN httpdata with content type that is not RDAP media type with charset parameter |
| 166 |
let mt = format!("{RDAP_MEDIA_TYPE};charset=UTF-8"); |
| 167 |
let http_data = HttpData::example().content_type(mt).build(); |
| 168 |
|
| 169 |
// WHEN checks are run |
| 170 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 171 |
|
| 172 |
// THEN incorrect media type check is not found |
| 173 |
assert!( |
| 174 |
!checks |
| 175 |
.items |
| 176 |
.iter() |
| 177 |
.any(|c| c.check == Check::ContentTypeIsNotRdap) |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn check_rdap_media_type_case_insensitive() { |
| 183 |
// GIVEN |
| 184 |
let domain = Domain { |
| 185 |
common: Common::level0() |
| 186 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 187 |
.build(), |
| 188 |
object_common: ObjectCommon::domain().build(), |
| 189 |
ldh_name: Some("foo.example".to_string()), |
| 190 |
unicode_name: None, |
| 191 |
variants: None, |
| 192 |
secure_dns: None, |
| 193 |
nameservers: None, |
| 194 |
public_ids: None, |
| 195 |
network: None, |
| 196 |
ttl0_data: None, |
| 197 |
}; |
| 198 |
let rdap = domain.to_response(); |
| 199 |
|
| 200 |
let http_data = HttpData::example() |
| 201 |
.content_type("APPLICATION/RDAP+JSON") |
| 202 |
.build(); |
| 203 |
|
| 204 |
// WHEN |
| 205 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 206 |
|
| 207 |
// THEN |
| 208 |
assert!( |
| 209 |
!checks |
| 210 |
.items |
| 211 |
.iter() |
| 212 |
.any(|c| c.check == Check::ContentTypeIsNotRdap) |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn check_media_type_absent() { |
| 218 |
// GIVEN an rdap response |
| 219 |
let domain = Domain { |
| 220 |
common: Common::level0() |
| 221 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 222 |
.build(), |
| 223 |
object_common: ObjectCommon::domain().build(), |
| 224 |
ldh_name: Some("foo.example".to_string()), |
| 225 |
unicode_name: None, |
| 226 |
variants: None, |
| 227 |
secure_dns: None, |
| 228 |
nameservers: None, |
| 229 |
public_ids: None, |
| 230 |
network: None, |
| 231 |
ttl0_data: None, |
| 232 |
}; |
| 233 |
let rdap = domain.to_response(); |
| 234 |
|
| 235 |
// and GIVEN httpdata no content type |
| 236 |
let http_data = HttpData::example().build(); |
| 237 |
|
| 238 |
// WHEN checks are run |
| 239 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 240 |
|
| 241 |
// THEN no media type check is found |
| 242 |
assert!( |
| 243 |
checks |
| 244 |
.items |
| 245 |
.iter() |
| 246 |
.any(|c| c.check == Check::ContentTypeIsAbsent) |
| 247 |
); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn check_cors_header_with_tig() { |
| 252 |
// GIVEN a response with gtld tig |
| 253 |
let domain = Domain { |
| 254 |
common: Common::level0() |
| 255 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 256 |
.build(), |
| 257 |
object_common: ObjectCommon::domain().build(), |
| 258 |
ldh_name: Some("foo.example".to_string()), |
| 259 |
unicode_name: None, |
| 260 |
variants: None, |
| 261 |
secure_dns: None, |
| 262 |
nameservers: None, |
| 263 |
public_ids: None, |
| 264 |
network: None, |
| 265 |
ttl0_data: None, |
| 266 |
}; |
| 267 |
let rdap = domain.to_response(); |
| 268 |
|
| 269 |
// and GIVEN a cors header with * |
| 270 |
let http_data = HttpData::example().access_control_allow_origin("*").build(); |
| 271 |
|
| 272 |
// WHEN running checks |
| 273 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 274 |
|
| 275 |
// THEN no check is given |
| 276 |
assert!( |
| 277 |
!checks |
| 278 |
.items |
| 279 |
.iter() |
| 280 |
.any(|c| c.check == Check::AllowOriginNotStar) |
| 281 |
); |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
fn check_cors_header_with_foo_and_tig() { |
| 286 |
// GIVEN a response with gtld tig extension |
| 287 |
let domain = Domain { |
| 288 |
common: Common::level0() |
| 289 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 290 |
.build(), |
| 291 |
object_common: ObjectCommon::domain().build(), |
| 292 |
ldh_name: Some("foo.example".to_string()), |
| 293 |
unicode_name: None, |
| 294 |
variants: None, |
| 295 |
secure_dns: None, |
| 296 |
nameservers: None, |
| 297 |
public_ids: None, |
| 298 |
network: None, |
| 299 |
ttl0_data: None, |
| 300 |
}; |
| 301 |
let rdap = domain.to_response(); |
| 302 |
|
| 303 |
// and GIVEN response with cors header of "foo" (not "*") |
| 304 |
let http_data = HttpData::example() |
| 305 |
.access_control_allow_origin("foo") |
| 306 |
.build(); |
| 307 |
|
| 308 |
// WHEN running checks |
| 309 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 310 |
|
| 311 |
// THEN the check is found |
| 312 |
assert!( |
| 313 |
checks |
| 314 |
.items |
| 315 |
.iter() |
| 316 |
.any(|c| c.check == Check::AllowOriginNotStar) |
| 317 |
); |
| 318 |
} |
| 319 |
|
| 320 |
#[test] |
| 321 |
fn check_no_cors_header_and_tig() { |
| 322 |
// GIVEN domain response with gtld tig extension id |
| 323 |
let domain = Domain { |
| 324 |
common: Common::level0() |
| 325 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 326 |
.build(), |
| 327 |
object_common: ObjectCommon::domain().build(), |
| 328 |
ldh_name: Some("foo.example".to_string()), |
| 329 |
unicode_name: None, |
| 330 |
variants: None, |
| 331 |
secure_dns: None, |
| 332 |
nameservers: None, |
| 333 |
public_ids: None, |
| 334 |
network: None, |
| 335 |
ttl0_data: None, |
| 336 |
}; |
| 337 |
let rdap = domain.to_response(); |
| 338 |
|
| 339 |
// and GIVEN a response with no cors header |
| 340 |
let http_data = HttpData::example().build(); |
| 341 |
|
| 342 |
// WHEN running checks |
| 343 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 344 |
|
| 345 |
// THEN check for missing cors is found |
| 346 |
dbg!(&checks); |
| 347 |
assert!( |
| 348 |
checks |
| 349 |
.items |
| 350 |
.iter() |
| 351 |
.any(|c| c.check == Check::AllowOriginNotStar) |
| 352 |
); |
| 353 |
} |
| 354 |
|
| 355 |
#[test] |
| 356 |
fn given_response_is_over_https_and_tig() { |
| 357 |
// GIVEN response with gtld tig extension |
| 358 |
let domain = Domain { |
| 359 |
common: Common::level0() |
| 360 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 361 |
.build(), |
| 362 |
object_common: ObjectCommon::domain().build(), |
| 363 |
ldh_name: Some("foo.example".to_string()), |
| 364 |
unicode_name: None, |
| 365 |
variants: None, |
| 366 |
secure_dns: None, |
| 367 |
nameservers: None, |
| 368 |
public_ids: None, |
| 369 |
network: None, |
| 370 |
ttl0_data: None, |
| 371 |
}; |
| 372 |
let rdap = domain.to_response(); |
| 373 |
|
| 374 |
// and GIVEN response is over https |
| 375 |
let http_data = HttpData::now().scheme("https").host("example.com").build(); |
| 376 |
|
| 377 |
// WHEN running checks |
| 378 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 379 |
|
| 380 |
// THEN then check for must use https is not present |
| 381 |
assert!(!checks.items.iter().any(|c| c.check == Check::MustUseHttps)); |
| 382 |
} |
| 383 |
|
| 384 |
#[test] |
| 385 |
fn response_over_http_and_tig() { |
| 386 |
// GIVEN domain response with gtld tig extension |
| 387 |
let domain = Domain { |
| 388 |
common: Common::level0() |
| 389 |
.extension(ExtensionId::IcannRdapTechnicalImplementationGuide0.to_extension()) |
| 390 |
.build(), |
| 391 |
object_common: ObjectCommon::domain().build(), |
| 392 |
ldh_name: Some("foo.example".to_string()), |
| 393 |
unicode_name: None, |
| 394 |
variants: None, |
| 395 |
secure_dns: None, |
| 396 |
nameservers: None, |
| 397 |
public_ids: None, |
| 398 |
network: None, |
| 399 |
ttl0_data: None, |
| 400 |
}; |
| 401 |
let rdap = domain.to_response(); |
| 402 |
|
| 403 |
// and GIVEN response is with http (not https) |
| 404 |
let http_data = HttpData::now().scheme("http").host("example.com").build(); |
| 405 |
|
| 406 |
// WHEN running checks |
| 407 |
let checks = http_data.get_checks(None, CheckParams::for_rdap(&rdap)); |
| 408 |
|
| 409 |
// THEN check for must use https is found |
| 410 |
assert!(checks.items.iter().any(|c| c.check == Check::MustUseHttps)); |
| 411 |
} |
| 412 |
} |