1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::collections::HashMap;
use super::*;
use swagger::scan::active::{ActiveScan, ActiveScanType};
use swagger::scan::passive::PassiveSwaggerScan;
use swagger::scan::Level;
use swagger::{Authorization, Check, EpTable, ParamTable, PassiveScanType, Swagger, OAS, OAS3_1};

pub fn run_passive_swagger_scan<T>(
    scan_try: Result<PassiveSwaggerScan<T>, &'static str>,
    verbosity: u8,
    output_file: Option<String>,
    passive_scan_type: PassiveScanType,
    json: bool,
) -> Result<i8, &'static str>
where
    T: OAS + Serialize + for<'de> Deserialize<'de> + std::fmt::Debug,
{
    let mut scan = match scan_try {
        Ok(s) => s,
        Err(e) => {
            return Err(e);
        }
    };
    scan.run(passive_scan_type);
    if json {
        let mut out_json : HashMap<&str,Vec<swagger::scan::Alert>>= HashMap::new();
        for check in scan.passive_checks.iter(){
            if !check.inner().is_empty() {
                out_json.insert(check.name(),check.inner().clone());
            }
        }
        print!("{}", serde_json::to_string(&out_json).unwrap());
    } else {
        scan.print(verbosity);
    }
    let failed = scan.passive_checks.iter().any(|c| c.result() == "FAILED"); //TODO support minimum level failure
    if let Some(f) = output_file {
        let print = if json {
            serde_json::to_string(&scan).unwrap()
        } else {
            scan.print_to_file_string()
        };
        write_to_file(&f, print);
    }
    if failed {
        Ok(101)
    } else {
        Ok(0)
    }
}

pub async fn run_active_swagger_scan<T>(
    scan_try: Result<ActiveScan<T>, &'static str>,
    verbosity: u8,
    output_file: Option<String>,
    auth: Authorization,
    scan_type: ActiveScanType,
    json:bool
) -> Result<i8, &'static str>
where
    T: OAS + Serialize + for<'de> Deserialize<'de> + std::fmt::Debug,
{
    let mut scan = match scan_try {
        Ok(s) => s,
        Err(e) => {
            return Err(e);
        }
    };
    scan.run(scan_type, &auth).await;
    if json {
        let mut out_json : HashMap<&str,Vec<swagger::scan::Alert>>= HashMap::new();
        for check in scan.checks.iter(){
            if !check.inner().is_empty() {
                out_json.insert(check.name(),check.inner().clone());
            }
        }
        print!("{}", serde_json::to_string(&out_json).unwrap());
    } else {
        scan.print(verbosity);
    }
    let failed = scan
        .checks
        .iter()
        .any(|c| (c.result() == "FAILED") || (c.top_severity() > Level::Info));
    if let Some(f) = output_file {
        let print = scan.print_to_file_string();
        write_to_file(&f, print);
    }
    if failed {
        Ok(101)
    } else {
        Ok(0)
    }
}

// todo create type, maybe add activeScanType none
#[allow(clippy::too_many_arguments)]
pub async fn run_swagger(
    file: &str,
    verbosity: u8,
    output_file: Option<String>,
    auth: Authorization,
    no_active: bool,
    active_scan_type: ActiveScanType,
    passive_scan_type: PassiveScanType,
    json: bool,
) -> i8 {
    let (value, version) = if let Some((v1, v2)) = get_oas_value_version(file) {
        (v1, v2)
    } else {
        return -1;
    };
    if version.starts_with("3.") {
        if json {
            print!("{{\"passive checks\":");
        }
        let passive_result = run_passive_swagger_scan::<OAS3_1>(
            PassiveSwaggerScan::<OAS3_1>::new(value.clone()),
            verbosity,
            output_file.clone(),
            passive_scan_type,
            json,
        );
        if let Err(e) = passive_result {
            print_err(e);
            return -1;
        }
        if json {
            print!(",\"active checks\":");
        }
        let active_result =
            if !no_active {
                run_active_swagger_scan::<OAS3_1>(
                    ActiveScan::<OAS3_1>::new(value.clone()),
                    verbosity,
                    output_file.clone(),
                    auth,
                    active_scan_type,
                    json,
                ).await
            } else {
                Ok(0)
            };
        if let Err(e) = active_result {
            print_err(e);
            return -1;
        }
        if json {
            print!("}}")
        }
        if passive_result.unwrap() == 0 && active_result.unwrap() == 0 {
            0i8
        } else {
            101i8
        }
    } else {
        print_err("Unsupported OpenAPI specification version");
        -1
    }
}

pub fn param_table(file: &str, param: Option<String>) {
    let (value, version) = if let Some((v1, v2)) = get_oas_value_version(file) {
        (v1, v2)
    } else {
        return;
    };
    if version.starts_with("3.0") {
        let table = ParamTable::new::<Swagger>(&value);
        if let Some(p) = param {
            table.named_param(&p).print();
        } else {
            table.print();
        }
    } else if version.starts_with("3.1") {
        let table = ParamTable::new::<OAS3_1>(&value);
        if let Some(p) = param {
            table.named_param(&p).print();
        } else {
            table.print();
        }
    } else {
        print_err("Unsupported OpenAPI specification version");
    }
}

pub fn ep_table(file: &str, path: Option<String>) {
    let (value, version) = if let Some((v1, v2)) = get_oas_value_version(file) {
        (v1, v2)
    } else {
        return;
    };
    if version.starts_with("3.0") {
        let table = EpTable::new::<Swagger>(&value);
        if let Some(p) = path {
            table.path_only(&p).print();
        } else {
            table.print();
        }
    } else if version.starts_with("3.1") {
        let table = EpTable::new::<OAS3_1>(&value);
        if let Some(p) = path {
            table.path_only(&p).print();
        } else {
            table.print();
        }
    } else {
        print_err("Unsupported OpenAPI specification version");
    }
}