Skip to content

Commit 3f52864

Browse files
committed
chore(api, web-security): use BTreeSet instead of HashSet for CSP directive for stable ordering
1 parent 20fe1b8 commit 3f52864

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

src/utils/web_security/csp/content_security_policies/content_security_policy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod tests {
2626
use crate::utils::web_security::{ContentSecurityPolicy, ContentSecurityPolicyDirective};
2727
use insta::assert_json_snapshot;
2828
use serde_json::json;
29-
use std::collections::HashSet;
29+
use std::collections::BTreeSet;
3030
use time::OffsetDateTime;
3131
use uuid::uuid;
3232

@@ -92,7 +92,7 @@ mod tests {
9292
ContentSecurityPolicy {
9393
id: uuid!("00000000-0000-0000-0000-000000000001"),
9494
name: "some-name".to_string(),
95-
directives: vec![ContentSecurityPolicyDirective::Sandbox(HashSet::new())],
95+
directives: vec![ContentSecurityPolicyDirective::Sandbox(BTreeSet::new())],
9696
// January 1, 2000 11:00:00
9797
created_at: OffsetDateTime::from_unix_timestamp(946720800)?,
9898
// January 1, 2000 11:00:10

src/utils/web_security/csp/content_security_policies/content_security_policy_directive.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,44 @@ use crate::utils::web_security::{
66
use anyhow::anyhow;
77
use content_security_policy::Directive;
88
use serde::{Deserialize, Deserializer, Serialize, de};
9-
use std::collections::HashSet;
9+
use std::collections::BTreeSet;
1010

1111
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
1212
#[serde(rename_all = "kebab-case", tag = "name", content = "value")]
1313
pub enum ContentSecurityPolicyDirective {
1414
// 15 fetch directives
15-
ChildSrc(HashSet<String>),
16-
ConnectSrc(HashSet<String>),
17-
DefaultSrc(HashSet<String>),
18-
FontSrc(HashSet<String>),
19-
FrameSrc(HashSet<String>),
20-
ImgSrc(HashSet<String>),
21-
ManifestSrc(HashSet<String>),
22-
MediaSrc(HashSet<String>),
23-
ObjectSrc(HashSet<String>),
24-
ScriptSrc(HashSet<String>),
25-
ScriptSrcElem(HashSet<String>),
26-
ScriptSrcAttr(HashSet<String>),
27-
StyleSrc(HashSet<String>),
28-
StyleSrcElem(HashSet<String>),
29-
StyleSrcAttr(HashSet<String>),
15+
ChildSrc(BTreeSet<String>),
16+
ConnectSrc(BTreeSet<String>),
17+
DefaultSrc(BTreeSet<String>),
18+
FontSrc(BTreeSet<String>),
19+
FrameSrc(BTreeSet<String>),
20+
ImgSrc(BTreeSet<String>),
21+
ManifestSrc(BTreeSet<String>),
22+
MediaSrc(BTreeSet<String>),
23+
ObjectSrc(BTreeSet<String>),
24+
ScriptSrc(BTreeSet<String>),
25+
ScriptSrcElem(BTreeSet<String>),
26+
ScriptSrcAttr(BTreeSet<String>),
27+
StyleSrc(BTreeSet<String>),
28+
StyleSrcElem(BTreeSet<String>),
29+
StyleSrcAttr(BTreeSet<String>),
3030
// 2 other directives
3131
Webrtc([ContentSecurityPolicyWebrtcDirectiveValue; 1]),
32-
WorkerSrc(HashSet<String>),
32+
WorkerSrc(BTreeSet<String>),
3333
// 2 document directives
34-
BaseUri(HashSet<String>),
35-
Sandbox(HashSet<ContentSecurityPolicySandboxDirectiveValue>),
34+
BaseUri(BTreeSet<String>),
35+
Sandbox(BTreeSet<ContentSecurityPolicySandboxDirectiveValue>),
3636
// 2 navigation directives
37-
FormAction(HashSet<String>),
38-
FrameAncestors(HashSet<String>),
37+
FormAction(BTreeSet<String>),
38+
FrameAncestors(BTreeSet<String>),
3939
// 1 extension directive
4040
#[serde(deserialize_with = "deserialize_directive_without_value")]
4141
UpgradeInsecureRequests,
4242
// 2 trusted types directives
4343
RequireTrustedTypesFor([ContentSecurityPolicyRequireTrustedTypesForDirectiveValue; 1]),
44-
TrustedTypes(HashSet<ContentSecurityPolicyTrustedTypesDirectiveValue>),
44+
TrustedTypes(BTreeSet<ContentSecurityPolicyTrustedTypesDirectiveValue>),
4545
// 2 reporting directives
46-
ReportUri(HashSet<String>),
46+
ReportUri(BTreeSet<String>),
4747
ReportTo([String; 1]),
4848
}
4949

@@ -139,11 +139,11 @@ mod tests {
139139
use content_security_policy::Directive;
140140
use insta::{assert_debug_snapshot, assert_json_snapshot};
141141
use serde_json::json;
142-
use std::collections::HashSet;
142+
use std::collections::BTreeSet;
143143

144144
#[test]
145145
fn serialization_to_json() -> anyhow::Result<()> {
146-
let sources = ["'self'".to_string()].into_iter().collect::<HashSet<_>>();
146+
let sources = ["'self'".to_string()].into_iter().collect::<BTreeSet<_>>();
147147
assert_json_snapshot!(ContentSecurityPolicyDirective::ChildSrc(sources.clone()), @r###"
148148
{
149149
"name": "child-src",
@@ -368,7 +368,7 @@ mod tests {
368368
}
369369
"###);
370370

371-
assert_json_snapshot!(ContentSecurityPolicyDirective::TrustedTypes(HashSet::new()), @r###"
371+
assert_json_snapshot!(ContentSecurityPolicyDirective::TrustedTypes(BTreeSet::new()), @r###"
372372
{
373373
"name": "trusted-types",
374374
"value": []
@@ -401,7 +401,7 @@ mod tests {
401401
assert_debug_snapshot!(
402402
String::try_from(ContentSecurityPolicyDirective::DefaultSrc(["'self'".to_string(), "https:".to_string()]
403403
.into_iter()
404-
.collect::<HashSet<_>>()))?, @r###""default-src 'self' https:""###);
404+
.collect::<BTreeSet<_>>()))?, @r###""default-src 'self' https:""###);
405405

406406
assert_debug_snapshot!(
407407
String::try_from(ContentSecurityPolicyDirective::UpgradeInsecureRequests)?,
@@ -430,7 +430,7 @@ mod tests {
430430

431431
assert_debug_snapshot!(
432432
String::try_from(
433-
ContentSecurityPolicyDirective::TrustedTypes(HashSet::new())
433+
ContentSecurityPolicyDirective::TrustedTypes(BTreeSet::new())
434434
)?,
435435
@r###""trusted-types""###
436436
);
@@ -440,7 +440,7 @@ mod tests {
440440

441441
#[test]
442442
fn deserialization() -> anyhow::Result<()> {
443-
let sources = ["'self'".to_string()].into_iter().collect::<HashSet<_>>();
443+
let sources = ["'self'".to_string()].into_iter().collect::<BTreeSet<_>>();
444444
assert_eq!(
445445
serde_json::from_str::<ContentSecurityPolicyDirective>(
446446
r#"{ "name": "child-src", "value": ["'self'"] }"#
@@ -682,7 +682,7 @@ mod tests {
682682

683683
assert_eq!(
684684
ContentSecurityPolicyDirective::try_from(&directive)?,
685-
ContentSecurityPolicyDirective::TrustedTypes(HashSet::new())
685+
ContentSecurityPolicyDirective::TrustedTypes(BTreeSet::new())
686686
);
687687

688688
Ok(())
@@ -729,7 +729,7 @@ mod tests {
729729
fn should_correctly_determine_if_supported_for_source() -> anyhow::Result<()> {
730730
let sources = ["'self'".to_string()]
731731
.into_iter()
732-
.collect::<HashSet<String>>();
732+
.collect::<BTreeSet<String>>();
733733
let all_directives = vec![
734734
ContentSecurityPolicyDirective::ChildSrc(sources.clone()),
735735
ContentSecurityPolicyDirective::ConnectSrc(sources.clone()),

src/utils/web_security/csp/content_security_policies/content_security_policy_sandbox_directive_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22

3-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
3+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
44
#[serde(rename_all = "kebab-case")]
55
#[allow(clippy::enum_variant_names)]
66
pub enum ContentSecurityPolicySandboxDirectiveValue {

src/utils/web_security/csp/content_security_policies/content_security_policy_trusted_types_directive_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::{Deserialize, Deserializer, Serialize, de};
22

33
/// See https://www.w3.org/TR/trusted-types.
4-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
4+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
55
pub enum ContentSecurityPolicyTrustedTypesDirectiveValue {
66
#[serde(rename = "'allow-duplicates'")]
77
AllowDuplicates,

0 commit comments

Comments
 (0)