Skip to content

Commit f0a2e30

Browse files
authored
Merge pull request #1 from RobDWaller/0.1.0-beta
0.1.0 beta
2 parents 411a701 + dd766ae commit f0a2e30

File tree

7 files changed

+107
-13
lines changed

7 files changed

+107
-13
lines changed

.github/workflows/build-test.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Build and Test Project
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v2
10+
- name: Build, Test, Analyse
11+
uses: actions-rs/toolchain@v1
12+
with:
13+
toolchain: stable
14+
- run: rustup component add clippy
15+
- run: rustup component add rustfmt
16+
- run: cargo build
17+
- run: cargo test
18+
- run: cargo fmt -- --check
19+
- run: cargo clippy --all-targets --all-features -- -D warnings
20+
- name: Code Coverage
21+
uses: actions-rs/tarpaulin@v0.1
22+
with:
23+
args: '-v'
24+
- name: Codecov.io
25+
uses: codecov/codecov-action@v1.0.2
26+
with:
27+
token: ${{secrets.CODECOV_TOKEN}}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Publish to Crates.io
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: Build and Publish
13+
uses: actions-rs/toolchain@v1
14+
with:
15+
toolchain: stable
16+
- run: cargo build
17+
- run: cargo login${{secrets.CRATES_TOKEN}}
18+
- run: cargo publish

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.1.0-alpha"
4+
version = "0.1.0-beta"
55
authors = ["Rob Waller <rdwaller1984@gmail.com>"]
66
edition = "2018"
77
keywords = ["csp", "json", "content-security", "csp-generator", "security"]

src/config.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,27 @@ impl GetDirectives for Directives {
1212

1313
pub fn get_directives() -> Directives {
1414
Directives {
15-
list: vec![String::from("script-src"), String::from("connect-src")],
15+
list: vec![
16+
String::from("default-src"),
17+
String::from("script-src"),
18+
String::from("style-src"),
19+
String::from("img-src"),
20+
String::from("connect-src"),
21+
String::from("font-src"),
22+
String::from("object-src"),
23+
String::from("media-src"),
24+
String::from("frame-src"),
25+
String::from("sandbox"),
26+
String::from("report-uri"),
27+
String::from("child-src"),
28+
String::from("form-action"),
29+
String::from("frame-ancestors"),
30+
String::from("plugin-types"),
31+
String::from("report-to"),
32+
String::from("worker-src"),
33+
String::from("manifest-src"),
34+
String::from("navigate-to"),
35+
],
1636
}
1737
}
1838

@@ -24,6 +44,8 @@ mod config_test {
2444
fn test_get_directives() {
2545
let config: super::Directives = super::get_directives();
2646

27-
assert_eq!(config.get_directives()[0], String::from("script-src"));
47+
assert_eq!(config.get_directives()[0], String::from("default-src"));
48+
assert_eq!(config.get_directives()[9], String::from("sandbox"));
49+
assert_eq!(config.get_directives()[18], String::from("navigate-to"));
2850
}
2951
}

src/directives.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ use std::thread;
66
use std::thread::JoinHandle;
77

88
fn build_line(directive: String, domains: domains::Collection) -> String {
9-
let mut directive_line: String = directive.to_string();
10-
directive_line.push_str(":");
9+
let mut directive_line: String = directive.clone();
10+
let mut directive_check: String = directive.clone();
11+
directive_check.push_str("; ");
1112

1213
for domain in domains.domains {
13-
if domain.directive.contains(&directive.to_string()) {
14+
if domain.directive.contains(&directive) {
1415
directive_line.push_str(" ");
1516
directive_line.push_str(domain.domain.as_str());
1617
}
1718
}
1819

1920
directive_line.push_str("; ");
21+
22+
if directive_line == directive_check {
23+
return String::from("");
24+
}
25+
2026
directive_line
2127
}
2228

@@ -28,7 +34,7 @@ fn build_lines(directives: Vec<String>, domains: domains::Collection) -> Vec<Joi
2834
let mut threads: Vec<JoinHandle<String>> = vec![];
2935

3036
for directive in directives {
31-
threads.push(self::create_thread(directive.to_string(), domains.clone()));
37+
threads.push(self::create_thread(directive, domains.clone()));
3238
}
3339

3440
threads
@@ -78,7 +84,28 @@ mod directives_test {
7884

7985
let connect_src: String = super::build_line(String::from("connect-src"), json);
8086

81-
assert_eq!(connect_src, String::from("connect-src: *.example.com; "));
87+
assert_eq!(connect_src, String::from("connect-src *.example.com; "));
88+
}
89+
90+
#[test]
91+
fn test_build_line_no_directive() {
92+
let directives: Vec<String> = vec![String::from("connect-src"), String::from("script-src")];
93+
94+
let item = domains::Item {
95+
domain: String::from("*.example.com"),
96+
directive: directives,
97+
};
98+
99+
let mut domain_list: Vec<domains::Item> = Vec::new();
100+
domain_list.push(item);
101+
102+
let json = domains::Collection {
103+
domains: domain_list,
104+
};
105+
106+
let default_src: String = super::build_line(String::from("default-src"), json);
107+
108+
assert_eq!(default_src, String::from(""));
82109
}
83110

84111
#[test]
@@ -96,7 +123,7 @@ mod directives_test {
96123

97124
assert_eq!(
98125
csp.unwrap(),
99-
String::from("script-src: test.com; connect-src: example.com test.com;")
126+
String::from("script-src test.com; connect-src example.com test.com;")
100127
);
101128
}
102129
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn enforce(directives: impl GetDirectives, json: &str) -> String {
1717

1818
match result {
1919
Ok(result) => {
20-
let mut directive: String = String::from("content-security-policy ");
20+
let mut directive: String = String::from("Content-Security-Policy ");
2121
directive.push_str(result.as_str());
2222
directive
2323
}
@@ -30,7 +30,7 @@ pub fn report_only(directives: impl GetDirectives, json: &str) -> String {
3030

3131
match result {
3232
Ok(result) => {
33-
let mut directive: String = String::from("content-security-policy-report-only ");
33+
let mut directive: String = String::from("Content-Security-Policy-Report-Only ");
3434
directive.push_str(result.as_str());
3535
directive
3636
}

tests/csp_generator_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod csp_generator_test {
1919
assert_eq!(
2020
csp,
2121
String::from(
22-
"content-security-policy script-src: test.com; connect-src: example.com test.com;"
22+
"Content-Security-Policy script-src test.com; connect-src example.com test.com;"
2323
)
2424
);
2525
}
@@ -63,7 +63,7 @@ mod csp_generator_test {
6363
assert_eq!(
6464
csp,
6565
String::from(
66-
"content-security-policy-report-only script-src: test.com; connect-src: example.com test.com;"
66+
"Content-Security-Policy-Report-Only script-src test.com; connect-src example.com test.com;"
6767
)
6868
);
6969
}

0 commit comments

Comments
 (0)