Skip to content

Commit d40206c

Browse files
authored
Merge pull request #66 from TimvdLippe/run-cargo-fmt
Run and enforce `cargo fmt`
2 parents a186ba8 + 3e227a7 commit d40206c

8 files changed

Lines changed: 662 additions & 240 deletions

File tree

.github/workflows/main.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ on:
1010
merge_group:
1111

1212
jobs:
13+
format:
14+
name: Check code formatting
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@master
18+
- name: Run cargo format
19+
run: cargo fmt --check
1320
test:
1421
name: Test
1522
runs-on: ${{ matrix.os }}
@@ -32,6 +39,7 @@ jobs:
3239
if: ${{ success() }}
3340
needs:
3441
- test
42+
- format
3543
runs-on: ubuntu-latest
3644
steps:
3745
- name: CI succeeded

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ To use `content-security-policy`, add it to your project's `Cargo.toml` file:
1515

1616
```toml
1717
[dependencies]
18-
content-security-policy = "0.6.0"
18+
content-security-policy = "0.6.1"
1919
```
2020

2121
# Example

src/lib.rs

Lines changed: 557 additions & 210 deletions
Large diffs are not rendered by default.

src/sandboxing_directive.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#[cfg(feature = "serde")] use serde::{Deserialize, Serialize};
21
use bitflags::bitflags;
2+
#[cfg(feature = "serde")]
3+
use serde::{Deserialize, Serialize};
34

4-
bitflags!{
5+
bitflags! {
56
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
67
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
78
pub struct SandboxingFlagSet: u32 {

src/text_util.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ pub(crate) fn strip_leading_and_trailing_ascii_whitespace(string: &str) -> &str
66
string.trim_matches(is_char_ascii_whitespace)
77
}
88

9-
pub(crate) fn collect_a_sequence_of_non_ascii_white_space_code_points(string: &str)
10-
-> (&str, &str) {
9+
pub(crate) fn collect_a_sequence_of_non_ascii_white_space_code_points(
10+
string: &str,
11+
) -> (&str, &str) {
1112
match string.find(is_char_ascii_whitespace) {
1213
Some(i) => string.split_at(i),
13-
None => (string, "")
14+
None => (string, ""),
1415
}
1516
}
1617

tests/base-uri.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@ extern crate content_security_policy;
22
use content_security_policy::*;
33
#[test]
44
fn base_uri_test_allow() {
5-
let csp_list = CspList::parse("base-uri https://www.notriddle.com", PolicySource::Header, PolicyDisposition::Enforce);
6-
let (check_result, _) = csp_list.is_base_allowed_for_document(&Url::parse("https://www.notriddle.com").unwrap(), &Origin::new_opaque());
5+
let csp_list = CspList::parse(
6+
"base-uri https://www.notriddle.com",
7+
PolicySource::Header,
8+
PolicyDisposition::Enforce,
9+
);
10+
let (check_result, _) = csp_list.is_base_allowed_for_document(
11+
&Url::parse("https://www.notriddle.com").unwrap(),
12+
&Origin::new_opaque(),
13+
);
714
assert_eq!(check_result, CheckResult::Allowed);
815
}
916
#[test]
1017
fn base_uri_test_blocked() {
11-
let csp_list = CspList::parse("base-uri https://www.example.com", PolicySource::Header, PolicyDisposition::Enforce);
12-
let (check_result, _) = csp_list.is_base_allowed_for_document(&Url::parse("https://www.notriddle.com").unwrap(), &Origin::new_opaque());
18+
let csp_list = CspList::parse(
19+
"base-uri https://www.example.com",
20+
PolicySource::Header,
21+
PolicyDisposition::Enforce,
22+
);
23+
let (check_result, _) = csp_list.is_base_allowed_for_document(
24+
&Url::parse("https://www.notriddle.com").unwrap(),
25+
&Origin::new_opaque(),
26+
);
1327
assert_eq!(check_result, CheckResult::Blocked);
14-
}
28+
}

tests/examples.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ macro_rules! test_should_request_be_blocked {
2424
}
2525

2626
// all tests should have a name starting with pre_request_
27-
test_should_request_be_blocked!{
27+
test_should_request_be_blocked! {
2828
( name: pre_request_wild_connect_port_allow,
2929
url: "https://www.notriddle.com:443/meta",
3030
origin: "https://example.com",
@@ -388,7 +388,7 @@ macro_rules! test_should_elements_inline_type_behavior_be_blocked {
388388
}
389389

390390
// all tests should have a name starting with inline_
391-
test_should_elements_inline_type_behavior_be_blocked!{
391+
test_should_elements_inline_type_behavior_be_blocked! {
392392
( name: inline_blocked_script,
393393
policy: "script-src 'none'",
394394
nonce: None,
@@ -417,7 +417,7 @@ macro_rules! test_should_js_evaluation_be_blocked {
417417
}
418418

419419
// all tests should have a name starting with eval_
420-
test_should_js_evaluation_be_blocked!{
420+
test_should_js_evaluation_be_blocked! {
421421
( name: eval_javascript_with_no_directive,
422422
policy: "script-src",
423423
disposition: Enforce,
@@ -458,7 +458,6 @@ test_should_js_evaluation_be_blocked!{
458458
)
459459
}
460460

461-
462461
macro_rules! test_should_wasm_evaluation_be_blocked {
463462
($((name: $name:ident, policy: $policy:expr, disposition: $disposition:tt, result: $result:tt)),*$(,)*) => {
464463
$(
@@ -473,7 +472,7 @@ macro_rules! test_should_wasm_evaluation_be_blocked {
473472
}
474473

475474
// all tests should have a name starting with eval_
476-
test_should_wasm_evaluation_be_blocked!{
475+
test_should_wasm_evaluation_be_blocked! {
477476
( name: eval_webassembly_with_no_directive,
478477
policy: "script-src",
479478
disposition: Enforce,

tests/sandbox.rs

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,73 @@
11
extern crate content_security_policy;
2-
use content_security_policy::*;
32
use content_security_policy::sandboxing_directive::SandboxingFlagSet;
3+
use content_security_policy::*;
44

55
#[test]
66
fn sandbox_document_flags() {
77
let policy = CspList::parse("sandbox", PolicySource::Header, PolicyDisposition::Enforce);
8-
assert_eq!(Some(SandboxingFlagSet::all()), policy.get_sandboxing_flag_set_for_document());
9-
let policy = CspList::parse("sandbox allow-popups", PolicySource::Header, PolicyDisposition::Enforce);
10-
assert_eq!(Some(SandboxingFlagSet::all() ^ SandboxingFlagSet::SANDBOXED_AUXILIARY_NAVIGATION_BROWSING_CONTEXT_FLAG ^ SandboxingFlagSet::SANBOXED_CUSTOM_PROTOCOLS_NAVIGATION_BROWSING_CONTEXT_FLAG), policy.get_sandboxing_flag_set_for_document());
11-
let policy = CspList::parse("sandbox allow-forms", PolicySource::Header, PolicyDisposition::Enforce);
12-
assert_eq!(Some(SandboxingFlagSet::all() ^ SandboxingFlagSet::SANDBOXED_FORMS_BROWSING_CONTEXT_FLAG), policy.get_sandboxing_flag_set_for_document());
13-
let policy = CspList::parse("sandbox allow-downloads", PolicySource::Header, PolicyDisposition::Enforce);
14-
assert_eq!(Some(SandboxingFlagSet::all() ^ SandboxingFlagSet::SANDBOXED_DOWNLOADS_BROWSING_CONTEXT_FLAG), policy.get_sandboxing_flag_set_for_document());
15-
let policy = CspList::parse("sandbox; connect-src https://*.notriddle.com:443", PolicySource::Header, PolicyDisposition::Enforce);
16-
assert_eq!(Some(SandboxingFlagSet::all()), policy.get_sandboxing_flag_set_for_document());
17-
let policy = CspList::parse("sandbox allow-popups; connect-src https://*.notriddle.com:443", PolicySource::Header, PolicyDisposition::Enforce);
18-
assert_eq!(Some(SandboxingFlagSet::all() ^ SandboxingFlagSet::SANDBOXED_AUXILIARY_NAVIGATION_BROWSING_CONTEXT_FLAG ^ SandboxingFlagSet::SANBOXED_CUSTOM_PROTOCOLS_NAVIGATION_BROWSING_CONTEXT_FLAG), policy.get_sandboxing_flag_set_for_document());
19-
let policy = CspList::parse("connect-src https://*.notriddle.com:443", PolicySource::Header, PolicyDisposition::Enforce);
8+
assert_eq!(
9+
Some(SandboxingFlagSet::all()),
10+
policy.get_sandboxing_flag_set_for_document()
11+
);
12+
let policy = CspList::parse(
13+
"sandbox allow-popups",
14+
PolicySource::Header,
15+
PolicyDisposition::Enforce,
16+
);
17+
assert_eq!(
18+
Some(
19+
SandboxingFlagSet::all()
20+
^ SandboxingFlagSet::SANDBOXED_AUXILIARY_NAVIGATION_BROWSING_CONTEXT_FLAG
21+
^ SandboxingFlagSet::SANBOXED_CUSTOM_PROTOCOLS_NAVIGATION_BROWSING_CONTEXT_FLAG
22+
),
23+
policy.get_sandboxing_flag_set_for_document()
24+
);
25+
let policy = CspList::parse(
26+
"sandbox allow-forms",
27+
PolicySource::Header,
28+
PolicyDisposition::Enforce,
29+
);
30+
assert_eq!(
31+
Some(SandboxingFlagSet::all() ^ SandboxingFlagSet::SANDBOXED_FORMS_BROWSING_CONTEXT_FLAG),
32+
policy.get_sandboxing_flag_set_for_document()
33+
);
34+
let policy = CspList::parse(
35+
"sandbox allow-downloads",
36+
PolicySource::Header,
37+
PolicyDisposition::Enforce,
38+
);
39+
assert_eq!(
40+
Some(
41+
SandboxingFlagSet::all() ^ SandboxingFlagSet::SANDBOXED_DOWNLOADS_BROWSING_CONTEXT_FLAG
42+
),
43+
policy.get_sandboxing_flag_set_for_document()
44+
);
45+
let policy = CspList::parse(
46+
"sandbox; connect-src https://*.notriddle.com:443",
47+
PolicySource::Header,
48+
PolicyDisposition::Enforce,
49+
);
50+
assert_eq!(
51+
Some(SandboxingFlagSet::all()),
52+
policy.get_sandboxing_flag_set_for_document()
53+
);
54+
let policy = CspList::parse(
55+
"sandbox allow-popups; connect-src https://*.notriddle.com:443",
56+
PolicySource::Header,
57+
PolicyDisposition::Enforce,
58+
);
59+
assert_eq!(
60+
Some(
61+
SandboxingFlagSet::all()
62+
^ SandboxingFlagSet::SANDBOXED_AUXILIARY_NAVIGATION_BROWSING_CONTEXT_FLAG
63+
^ SandboxingFlagSet::SANBOXED_CUSTOM_PROTOCOLS_NAVIGATION_BROWSING_CONTEXT_FLAG
64+
),
65+
policy.get_sandboxing_flag_set_for_document()
66+
);
67+
let policy = CspList::parse(
68+
"connect-src https://*.notriddle.com:443",
69+
PolicySource::Header,
70+
PolicyDisposition::Enforce,
71+
);
2072
assert_eq!(None, policy.get_sandboxing_flag_set_for_document());
21-
}
73+
}

0 commit comments

Comments
 (0)