Skip to content

Commit 72faabc

Browse files
authored
Merge pull request #7 from RobDWaller/0.2.0-beta.1
0.2.0 beta.1
2 parents cec5267 + 9a707b4 commit 72faabc

File tree

9 files changed

+69
-31
lines changed

9 files changed

+69
-31
lines changed

.github/workflows/build-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ jobs:
1515
- run: rustup component add rustfmt
1616
- run: cargo build
1717
- run: cargo test
18-
- run: cargo fmt -- --check
19-
- run: cargo clippy --all-targets --all-features -- -D warnings
18+
- run: cargo lint
19+
- run: cargo analyse
2020
- name: Code Coverage
2121
uses: actions-rs/tarpaulin@v0.1
2222
with:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "csp_generator"
33
description = "Consume a JSON formatted list of domains and CSP directives and output a correctly formatted Content Security Policy string."
4-
version = "0.2.0-beta"
4+
version = "0.2.0-beta.1"
55
authors = ["Rob Waller <rdwaller1984@gmail.com>"]
66
edition = "2018"
77
keywords = ["csp", "json", "content-security", "csp-generator", "security"]

src/directives/line.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn domains_to_directive(directive: String, domains: Vec<Item>) -> String {
44
let mut directive_line = directive.clone();
55

66
for domain in domains {
7-
if domain.directive.contains(&directive) {
7+
if domain.directives.contains(&directive) {
88
directive_line.push_str(" ");
99
directive_line.push_str(domain.domain.as_str());
1010
}
@@ -46,7 +46,7 @@ mod lines_test {
4646

4747
let item = Item {
4848
domain: String::from("*.example.com"),
49-
directive: directives,
49+
directives,
5050
};
5151

5252
let mut domain_list: Collection = Vec::new();
@@ -63,7 +63,7 @@ mod lines_test {
6363

6464
let item = Item {
6565
domain: String::from("*.example.com"),
66-
directive: directives,
66+
directives,
6767
};
6868

6969
let mut domain_list: Collection = Vec::new();

src/directives/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ mod directives_test {
4343
fn test_build_directives() {
4444
let json = r#"
4545
[
46-
{"domain": "example.com", "directive": ["connect-src"]},
47-
{"domain": "test.com", "directive": ["connect-src", "script-src"]}
46+
{"domain": "example.com", "directives": ["connect-src"]},
47+
{"domain": "test.com", "directives": ["connect-src", "script-src"]}
4848
]
4949
"#;
5050

src/directives/threads.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ mod threads_test {
2727
#[test]
2828
fn test_create() {
2929
let domain = String::from("*.google.com");
30-
let directive = vec![String::from("connect-src")];
30+
let directives = vec![String::from("connect-src")];
3131

32-
let domain = Item { domain, directive };
32+
let domain = Item { domain, directives };
3333
let domains: Collection = vec![domain];
3434

3535
let directive_check = String::from("connect-src");

src/domains.rs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,80 @@
1-
#[derive(Serialize, Deserialize, Clone)]
1+
#[derive(Deserialize, Serialize, Clone)]
22
pub struct Item {
33
pub domain: String,
4-
pub directive: Vec<String>,
4+
pub directives: Vec<String>,
55
}
66

77
pub type Collection = Vec<Item>;
88

9+
pub trait ToJson {
10+
fn to_json(&self) -> String;
11+
}
12+
13+
impl ToJson for Collection {
14+
fn to_json(&self) -> String {
15+
let mut json = "[".to_string();
16+
17+
for item in self {
18+
json.push_str(serde_json::to_string(&item).unwrap().as_str());
19+
}
20+
21+
json.push_str("]");
22+
23+
json
24+
}
25+
}
26+
927
// -----
1028
// Tests
1129
// -----
1230
#[cfg(test)]
1331
mod item_test {
32+
use super::{Collection, Item, ToJson};
33+
1434
#[test]
1535
fn test_item_struct() {
1636
let directives: Vec<String> = vec![String::from("connect-src"), String::from("script-src")];
1737

18-
let item = super::Item {
38+
let item = Item {
1939
domain: String::from("*.example.com"),
20-
directive: directives,
40+
directives,
2141
};
2242

2343
assert_eq!(item.domain, "*.example.com");
24-
assert_eq!(item.directive[1], "script-src");
44+
assert_eq!(item.directives[1], "script-src");
2545
}
2646

2747
#[test]
2848
fn test_collection() {
2949
let directives: Vec<String> = vec![String::from("connect-src"), String::from("script-src")];
3050

31-
let item = super::Item {
51+
let item = Item {
3252
domain: String::from("*.example.com"),
33-
directive: directives,
53+
directives,
3454
};
3555

36-
let mut domains: super::Collection = vec![];
56+
let mut domains: Collection = Collection::new();
3757
domains.push(item);
3858

3959
assert_eq!(domains[0].domain, "*.example.com");
40-
assert_eq!(domains[0].directive[1], "script-src");
60+
assert_eq!(domains[0].directives[1], "script-src");
61+
}
62+
63+
#[test]
64+
fn test_to_json() {
65+
let directives: Vec<String> = vec![String::from("connect-src"), String::from("script-src")];
66+
67+
let item = super::Item {
68+
domain: String::from("*.example.com"),
69+
directives,
70+
};
71+
72+
let mut domains: Collection = Collection::new();
73+
domains.push(item);
74+
75+
assert_eq!(
76+
domains.to_json(),
77+
r#"[{"domain":"*.example.com","directives":["connect-src","script-src"]}]"#
78+
);
4179
}
4280
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate serde_derive;
55

66
pub mod config;
77
mod directives;
8-
mod domains;
8+
pub mod domains;
99
mod parse;
1010

1111
pub struct Csp {

src/parse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ mod parse_json_test {
2222
fn test_parse_json() {
2323
let json = r#"
2424
[
25-
{"domain": "example.com", "directive": ["connect-src", "script-src"]},
26-
{"domain": "test.com", "directive": ["script-src", "img-src", "style-src"]}
25+
{"domain": "example.com", "directives": ["connect-src", "script-src"]},
26+
{"domain": "test.com", "directives": ["script-src", "img-src", "style-src"]}
2727
]
2828
"#;
2929

3030
let domains = super::json(json).unwrap();
3131

3232
assert_eq!(domains[0].domain, "example.com");
3333
assert_eq!(domains[1].domain, "test.com");
34-
assert_eq!(domains[1].directive[1], "img-src");
34+
assert_eq!(domains[1].directives[1], "img-src");
3535
}
3636

3737
#[test]

tests/csp_generator_test.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ mod csp_generator_test {
77
fn test_enforce() {
88
let json = r#"
99
[
10-
{"domain": "example.com", "directive": ["connect-src"]},
11-
{"domain": "test.com", "directive": ["connect-src", "script-src"]}
10+
{"domain": "example.com", "directives": ["connect-src"]},
11+
{"domain": "test.com", "directives": ["connect-src", "script-src"]}
1212
]
1313
"#;
1414

@@ -46,8 +46,8 @@ mod csp_generator_test {
4646
fn test_report_only() {
4747
let json = r#"
4848
[
49-
{"domain": "example.com", "directive": ["connect-src"]},
50-
{"domain": "test.com", "directive": ["connect-src", "script-src"]}
49+
{"domain": "example.com", "directives": ["connect-src"]},
50+
{"domain": "test.com", "directives": ["connect-src", "script-src"]}
5151
]
5252
"#;
5353

@@ -76,8 +76,8 @@ mod csp_generator_test {
7676
fn test_report_only_format_fail() {
7777
let json = r#"
7878
[
79-
{"domain": "example.com", "directive": ["connect-src"]},
80-
{"directive": ["connect-src", "script-src"]}
79+
{"domain": "example.com", "directives": ["connect-src"]},
80+
{"directives": ["connect-src", "script-src"]}
8181
]
8282
"#;
8383

@@ -88,8 +88,8 @@ mod csp_generator_test {
8888
fn test_csp_only() {
8989
let json = r#"
9090
[
91-
{"domain": "example.com", "directive": ["connect-src"]},
92-
{"domain": "test.com", "directive": ["connect-src", "script-src"]}
91+
{"domain": "example.com", "directives": ["connect-src"]},
92+
{"domain": "test.com", "directives": ["connect-src", "script-src"]}
9393
]
9494
"#;
9595

0 commit comments

Comments
 (0)