const MAX_DEPTH: usize = 8;
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct ClientData {
pub kind: Option<String>,
pub challenge: Option<String>,
pub cross_origin: Option<Result<bool, ()>>,
}
enum Value {
String(String),
Bool(bool),
Other,
}
pub(crate) fn parse(bytes: &[u8]) -> Option<ClientData> {
std::str::from_utf8(bytes).ok()?;
let mut reader = Reader { bytes, at: 0 };
reader.space();
let mut data = ClientData::default();
let mut seen: Vec<String> = Vec::new();
reader.object(0, &mut |key, value| {
if seen.contains(&key) {
return false;
}
match (key.as_str(), value) {
("type", Value::String(text)) => data.kind = Some(text),
("challenge", Value::String(text)) => data.challenge = Some(text),
("crossOrigin", Value::Bool(cross)) => data.cross_origin = Some(Ok(cross)),
("crossOrigin", _) => data.cross_origin = Some(Err(())),
("type" | "challenge", _) => return false,
_ => {}
}
seen.push(key);
true
})?;
reader.space();
(reader.at == bytes.len()).then_some(data)
}
struct Reader<'a> {
bytes: &'a [u8],
at: usize,
}
impl Reader<'_> {
fn peek(&self) -> Option<u8> {
self.bytes.get(self.at).copied()
}
fn next(&mut self) -> Option<u8> {
let byte = self.peek()?;
self.at += 1;
Some(byte)
}
fn eat(&mut self, byte: u8) -> Option<()> {
(self.next()? == byte).then_some(())
}
fn space(&mut self) {
while matches!(self.peek(), Some(b' ' | b'\t' | b'\n' | b'\r')) {
self.at += 1;
}
}
fn object(
&mut self,
depth: usize,
member: &mut dyn FnMut(String, Value) -> bool,
) -> Option<()> {
self.eat(b'{')?;
self.space();
if self.peek() == Some(b'}') {
self.at += 1;
return Some(());
}
loop {
self.space();
let key = self.string()?;
self.space();
self.eat(b':')?;
self.space();
let value = self.value(depth)?;
if !member(key, value) {
return None;
}
self.space();
match self.next()? {
b',' => continue,
b'}' => return Some(()),
_ => return None,
}
}
}
fn array(&mut self, depth: usize) -> Option<()> {
self.eat(b'[')?;
self.space();
if self.peek() == Some(b']') {
self.at += 1;
return Some(());
}
loop {
self.space();
self.value(depth)?;
self.space();
match self.next()? {
b',' => continue,
b']' => return Some(()),
_ => return None,
}
}
}
fn value(&mut self, depth: usize) -> Option<Value> {
match self.peek()? {
b'"' => self.string().map(Value::String),
b'{' | b'[' if depth >= MAX_DEPTH => None,
b'{' => self
.object(depth + 1, &mut |_, _| true)
.map(|()| Value::Other),
b'[' => self.array(depth + 1).map(|()| Value::Other),
b't' => self.literal(b"true").map(|()| Value::Bool(true)),
b'f' => self.literal(b"false").map(|()| Value::Bool(false)),
b'n' => self.literal(b"null").map(|()| Value::Other),
b'-' | b'0'..=b'9' => self.number().map(|()| Value::Other),
_ => None,
}
}
fn literal(&mut self, word: &[u8]) -> Option<()> {
let end = self.at.checked_add(word.len())?;
(self.bytes.get(self.at..end)? == word).then(|| self.at = end)
}
fn digits(&mut self) -> Option<()> {
let start = self.at;
while matches!(self.peek(), Some(b'0'..=b'9')) {
self.at += 1;
}
(self.at > start).then_some(())
}
fn number(&mut self) -> Option<()> {
if self.peek() == Some(b'-') {
self.at += 1;
}
match self.next()? {
b'0' => {}
b'1'..=b'9' => {
while matches!(self.peek(), Some(b'0'..=b'9')) {
self.at += 1;
}
}
_ => return None,
}
if self.peek() == Some(b'.') {
self.at += 1;
self.digits()?;
}
if matches!(self.peek(), Some(b'e' | b'E')) {
self.at += 1;
if matches!(self.peek(), Some(b'+' | b'-')) {
self.at += 1;
}
self.digits()?;
}
Some(())
}
fn hex4(&mut self) -> Option<u32> {
let mut code = 0;
for _ in 0..4 {
code = code * 16 + char::from(self.next()?).to_digit(16)?;
}
Some(code)
}
fn string(&mut self) -> Option<String> {
self.eat(b'"')?;
let mut out = Vec::new();
loop {
match self.next()? {
b'"' => return String::from_utf8(out).ok(),
b'\\' => {
let decoded = match self.next()? {
b'"' => '"',
b'\\' => '\\',
b'/' => '/',
b'b' => '\u{8}',
b'f' => '\u{c}',
b'n' => '\n',
b'r' => '\r',
b't' => '\t',
b'u' => {
let high = self.hex4()?;
let code = if (0xd800..0xdc00).contains(&high) {
self.eat(b'\\')?;
self.eat(b'u')?;
let low = self.hex4()?;
if !(0xdc00..0xe000).contains(&low) {
return None;
}
0x10000 + ((high - 0xd800) << 10) + (low - 0xdc00)
} else {
high
};
char::from_u32(code)?
}
_ => return None,
};
let mut buffer = [0; 4];
out.extend_from_slice(decoded.encode_utf8(&mut buffer).as_bytes());
}
0..0x20 => return None,
byte => out.push(byte),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn read(text: &str) -> Option<ClientData> {
parse(text.as_bytes())
}
#[test]
fn it_reads_the_members_a_root_signature_is_checked_by() {
let data = read(
r#" { "type" : "webauthn.get", "challenge":"q83v", "origin":"https://x",
"crossOrigin":false, "other":{"a":[1,-2.5e+3,true,null,{}]}, "n":0 } "#,
)
.unwrap();
assert_eq!(data.kind.as_deref(), Some("webauthn.get"));
assert_eq!(data.challenge.as_deref(), Some("q83v"));
assert_eq!(data.cross_origin, Some(Ok(false)));
}
#[test]
fn it_decodes_escapes() {
let data = read(r#"{"type":"webauthn.get","challenge":"a\/b😀\""}"#).unwrap();
assert_eq!(data.kind.as_deref(), Some("webauthn.get"));
assert_eq!(data.challenge.as_deref(), Some("a/b\u{1f600}\""));
}
#[test]
fn it_refuses_what_is_not_strict_json() {
for text in [
"",
"{",
"{}x",
"[]",
r#"{"a":1,}"#,
r#"{"a":01}"#,
r#"{"a":1.}"#,
r#"{"a":-}"#,
r#"{"a":tru}"#,
r#"{'a':1}"#,
r#"{"a":"\x"}"#,
r#"{"a":"\ud800"}"#,
r#"{"a":"\udc00"}"#,
"{\"a\":\"\n\"}",
r#"{"type":"webauthn.get","type":"webauthn.get"}"#,
r#"{"type":1}"#,
r#"{"challenge":null}"#,
r#"{"a":[[[[[[[[[1]]]]]]]]]}"#,
] {
assert_eq!(read(text), None, "{text}");
}
assert_eq!(parse(b"{\"a\":\"\xff\"}"), None);
assert!(read(r#"{"a":[[[[[[[[1]]]]]]]]}"#).is_some());
}
#[test]
fn a_crossorigin_that_is_not_a_boolean_is_marked() {
assert_eq!(
read(r#"{"crossOrigin":"no"}"#).unwrap().cross_origin,
Some(Err(()))
);
}
}