Skip to content

Commit

Permalink
fix: add dfa
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Mar 8, 2024
1 parent 81d2434 commit 55928a5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
8 changes: 8 additions & 0 deletions crates/redos-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ pub fn vulnerabilities(regex: &str) -> String {
redos::vulnerabilities(regex, &Default::default()).map(|r| r.vulnerabilities)
)
}

#[wasm_bindgen]
pub fn dfa(regex: &str) -> String {
format!(
"{:#?}",
redos::vulnerabilities(regex, &Default::default()).map(|r| r.dfa)
)
}
4 changes: 2 additions & 2 deletions crates/redos/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ pub enum IrAssertion {
NotWordBoundary,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ExprConditional {
Condition(Box<Expr>),
BackrefExistsCondition(usize),
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Expr {
/// Some token, whether its a character class, any character, etc.
Token,
Expand Down
19 changes: 13 additions & 6 deletions crates/redos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ use fancy_regex::Expr as RegexExpr;
use ir::{to_expr, Expr, ExprConditional};
use vulnerability::{Vulnerability, VulnerabilityConfig};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct RegexInfo {
has_repeat: bool,
has_alternation: bool,
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RegexInfo {
pub has_repeat: bool,
pub has_alternation: bool,
}

impl RegexInfo {
fn merge(self, other: RegexInfo) -> RegexInfo {
RegexInfo {
has_repeat: self.has_repeat || other.has_repeat,
has_alternation: self.has_alternation || other.has_alternation,
has_alternation: self.has_alternation || other.has_alternation
}
}

Expand Down Expand Up @@ -84,7 +84,7 @@ fn regex_pre_scan(expr: &Expr) -> RegexInfo {
}
ExprConditional::Condition(condition) => regex_pre_scan(condition.as_ref())
.merge(regex_pre_scan_nested(true_branch.as_ref()))
.merge(regex_pre_scan_nested(false_branch.as_ref())),
.merge(regex_pre_scan_nested(false_branch.as_ref()))
}
}
}
Expand Down Expand Up @@ -139,6 +139,9 @@ pub struct VulnerabilityResult {

/// If this regex can be reduced to a DFA
pub dfa: bool,

/// The information about the regex
pub regex_info: RegexInfo,
}

/// Returns the list of vulnerabilities in a regex
Expand All @@ -156,6 +159,7 @@ pub fn vulnerabilities(
return Ok(VulnerabilityResult {
vulnerabilities: vec![],
dfa: can_be_dfa,
regex_info: RegexInfo::empty(),
});
}

Expand All @@ -166,6 +170,7 @@ pub fn vulnerabilities(
return Ok(VulnerabilityResult {
vulnerabilities: vec![],
dfa: can_be_dfa,
regex_info: RegexInfo::empty(),
})
}
};
Expand All @@ -176,6 +181,7 @@ pub fn vulnerabilities(
return Ok(VulnerabilityResult {
vulnerabilities: vec![],
dfa: can_be_dfa,
regex_info,
});
}

Expand All @@ -193,6 +199,7 @@ pub fn vulnerabilities(
Ok(VulnerabilityResult {
vulnerabilities,
dfa: can_be_dfa,
regex_info,
})
}
}
4 changes: 2 additions & 2 deletions crates/redos/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ mod tests {
}

fn assert_safe(regex: &str, message: &str) {
let vulnerabilities = vulnerabilities(regex, &Default::default())
.map(|r| r.vulnerabilities);
let vulnerabilities =
vulnerabilities(regex, &Default::default()).map(|r| r.vulnerabilities);

assert!(
vulnerabilities.is_ok(),
Expand Down
14 changes: 12 additions & 2 deletions website/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
// @ts-ignore
import init, { parse, ir, vulnerabilities } from 'redos-wasm';
import init, { parse, ir, vulnerabilities, dfa } from 'redos-wasm';
import { onMount } from 'svelte';
let hasBeenEnabled = false;
Expand All @@ -13,11 +13,13 @@
let ast = '';
let irValue = '';
let vulns = '';
let dfaInfo = '';
$: if (hasBeenEnabled) {
ast = parse(regex);
irValue = ir(regex);
vulns = vulnerabilities(regex);
dfaInfo = dfa(regex);
}
</script>

Expand All @@ -29,6 +31,8 @@
<h1>AST</h1>
<pre>{ast}</pre>
</div>
</div>
<div class="output">
<div class="subContainer">
<h1>IR</h1>
<pre>{irValue}</pre>
Expand All @@ -38,6 +42,12 @@
<pre>{vulns}</pre>
</div>
</div>
<div class="output">
<div class="subContainer">
<h1>DFA</h1>
<pre>{dfaInfo}</pre>
</div>
</div>
</main>

<style lang="scss">
Expand Down Expand Up @@ -78,7 +88,7 @@
.subContainer {
margin-top: 2rem;
width: 30%;
width: 100%;
height: 100%;
padding: 1rem;
background-color: var(--backgroundIsh);
Expand Down

0 comments on commit 55928a5

Please sign in to comment.