Skip to content

Commit

Permalink
Merge pull request #7 from RobDWaller/0.2.0-beta.1
Browse files Browse the repository at this point in the history
0.2.0 beta.1
  • Loading branch information
RobDWaller authored Apr 29, 2020
2 parents cec5267 + 9a707b4 commit 72faabc
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
- run: rustup component add rustfmt
- run: cargo build
- run: cargo test
- run: cargo fmt -- --check
- run: cargo clippy --all-targets --all-features -- -D warnings
- run: cargo lint
- run: cargo analyse
- name: Code Coverage
uses: actions-rs/[email protected]
with:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "csp_generator"
description = "Consume a JSON formatted list of domains and CSP directives and output a correctly formatted Content Security Policy string."
version = "0.2.0-beta"
version = "0.2.0-beta.1"
authors = ["Rob Waller <[email protected]>"]
edition = "2018"
keywords = ["csp", "json", "content-security", "csp-generator", "security"]
Expand Down
6 changes: 3 additions & 3 deletions src/directives/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn domains_to_directive(directive: String, domains: Vec<Item>) -> String {
let mut directive_line = directive.clone();

for domain in domains {
if domain.directive.contains(&directive) {
if domain.directives.contains(&directive) {
directive_line.push_str(" ");
directive_line.push_str(domain.domain.as_str());
}
Expand Down Expand Up @@ -46,7 +46,7 @@ mod lines_test {

let item = Item {
domain: String::from("*.example.com"),
directive: directives,
directives,
};

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

let item = Item {
domain: String::from("*.example.com"),
directive: directives,
directives,
};

let mut domain_list: Collection = Vec::new();
Expand Down
4 changes: 2 additions & 2 deletions src/directives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ mod directives_test {
fn test_build_directives() {
let json = r#"
[
{"domain": "example.com", "directive": ["connect-src"]},
{"domain": "test.com", "directive": ["connect-src", "script-src"]}
{"domain": "example.com", "directives": ["connect-src"]},
{"domain": "test.com", "directives": ["connect-src", "script-src"]}
]
"#;

Expand Down
4 changes: 2 additions & 2 deletions src/directives/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ mod threads_test {
#[test]
fn test_create() {
let domain = String::from("*.google.com");
let directive = vec![String::from("connect-src")];
let directives = vec![String::from("connect-src")];

let domain = Item { domain, directive };
let domain = Item { domain, directives };
let domains: Collection = vec![domain];

let directive_check = String::from("connect-src");
Expand Down
56 changes: 47 additions & 9 deletions src/domains.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,80 @@
#[derive(Serialize, Deserialize, Clone)]
#[derive(Deserialize, Serialize, Clone)]
pub struct Item {
pub domain: String,
pub directive: Vec<String>,
pub directives: Vec<String>,
}

pub type Collection = Vec<Item>;

pub trait ToJson {
fn to_json(&self) -> String;
}

impl ToJson for Collection {
fn to_json(&self) -> String {
let mut json = "[".to_string();

for item in self {
json.push_str(serde_json::to_string(&item).unwrap().as_str());
}

json.push_str("]");

json
}
}

// -----
// Tests
// -----
#[cfg(test)]
mod item_test {
use super::{Collection, Item, ToJson};

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

let item = super::Item {
let item = Item {
domain: String::from("*.example.com"),
directive: directives,
directives,
};

assert_eq!(item.domain, "*.example.com");
assert_eq!(item.directive[1], "script-src");
assert_eq!(item.directives[1], "script-src");
}

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

let item = super::Item {
let item = Item {
domain: String::from("*.example.com"),
directive: directives,
directives,
};

let mut domains: super::Collection = vec![];
let mut domains: Collection = Collection::new();
domains.push(item);

assert_eq!(domains[0].domain, "*.example.com");
assert_eq!(domains[0].directive[1], "script-src");
assert_eq!(domains[0].directives[1], "script-src");
}

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

let item = super::Item {
domain: String::from("*.example.com"),
directives,
};

let mut domains: Collection = Collection::new();
domains.push(item);

assert_eq!(
domains.to_json(),
r#"[{"domain":"*.example.com","directives":["connect-src","script-src"]}]"#
);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate serde_derive;

pub mod config;
mod directives;
mod domains;
pub mod domains;
mod parse;

pub struct Csp {
Expand Down
6 changes: 3 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ mod parse_json_test {
fn test_parse_json() {
let json = r#"
[
{"domain": "example.com", "directive": ["connect-src", "script-src"]},
{"domain": "test.com", "directive": ["script-src", "img-src", "style-src"]}
{"domain": "example.com", "directives": ["connect-src", "script-src"]},
{"domain": "test.com", "directives": ["script-src", "img-src", "style-src"]}
]
"#;

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

assert_eq!(domains[0].domain, "example.com");
assert_eq!(domains[1].domain, "test.com");
assert_eq!(domains[1].directive[1], "img-src");
assert_eq!(domains[1].directives[1], "img-src");
}

#[test]
Expand Down
16 changes: 8 additions & 8 deletions tests/csp_generator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ mod csp_generator_test {
fn test_enforce() {
let json = r#"
[
{"domain": "example.com", "directive": ["connect-src"]},
{"domain": "test.com", "directive": ["connect-src", "script-src"]}
{"domain": "example.com", "directives": ["connect-src"]},
{"domain": "test.com", "directives": ["connect-src", "script-src"]}
]
"#;

Expand Down Expand Up @@ -46,8 +46,8 @@ mod csp_generator_test {
fn test_report_only() {
let json = r#"
[
{"domain": "example.com", "directive": ["connect-src"]},
{"domain": "test.com", "directive": ["connect-src", "script-src"]}
{"domain": "example.com", "directives": ["connect-src"]},
{"domain": "test.com", "directives": ["connect-src", "script-src"]}
]
"#;

Expand Down Expand Up @@ -76,8 +76,8 @@ mod csp_generator_test {
fn test_report_only_format_fail() {
let json = r#"
[
{"domain": "example.com", "directive": ["connect-src"]},
{"directive": ["connect-src", "script-src"]}
{"domain": "example.com", "directives": ["connect-src"]},
{"directives": ["connect-src", "script-src"]}
]
"#;

Expand All @@ -88,8 +88,8 @@ mod csp_generator_test {
fn test_csp_only() {
let json = r#"
[
{"domain": "example.com", "directive": ["connect-src"]},
{"domain": "test.com", "directive": ["connect-src", "script-src"]}
{"domain": "example.com", "directives": ["connect-src"]},
{"domain": "test.com", "directives": ["connect-src", "script-src"]}
]
"#;

Expand Down

0 comments on commit 72faabc

Please sign in to comment.