use super::text;
pub(crate) const JSON_WINDOW_LINES: usize = 6;
pub(crate) const REQUEST_WINDOW_LINES: usize = 10;
pub(crate) const SCOPED_LOOKBACK_LINES: usize = REQUEST_WINDOW_LINES;
pub(crate) const MAX_WINDOW_BYTES: usize = 2_048;
const JSON_TRIGGER_MEMBERS: &[&str] = &["api_key", "encoded", "sys", "scopes", "token"];
pub(super) struct ScopedRead {
pub(super) alias: &'static str,
pub(super) name_phrase_is_reference: bool,
}
impl ScopedRead {
const fn alias(alias: &'static str) -> Self {
Self {
alias,
name_phrase_is_reference: false,
}
}
}
pub(super) fn is_quoted_member(input: &str, name_start: usize, name_end: usize) -> bool {
let before = &input[..name_start];
let after = &input[name_end..];
before.ends_with('"') && (after.starts_with('"') || after.starts_with("\\\""))
}
pub(super) fn scoped_alias(
input: &str,
name_start: usize,
name_end: usize,
normalized: &str,
) -> Option<ScopedRead> {
match normalized {
"token_key" if is_quoted_member(input, name_start, name_end) => Some(ScopedRead {
alias: "access_token",
name_phrase_is_reference: true,
}),
"encoded"
if is_quoted_member(input, name_start, name_end)
&& json_partner(input, name_start, &["api_key"]) =>
{
Some(ScopedRead::alias("api_key"))
}
"token" => {
if is_quoted_member(input, name_start, name_end) {
json_partner(input, name_start, &["sys", "scopes"])
.then(|| ScopedRead::alias("access_token"))
} else if input[name_end..].starts_with('=') && revocation_context(input, name_start) {
Some(ScopedRead::alias("access_token"))
} else {
None
}
}
_ => None,
}
}
fn line_around(input: &str, position: usize) -> (usize, usize) {
let start = text::line_start_before(input, position);
let end = text::line_end_from(input.as_bytes(), start)
.0
.min(input.len());
(start, end)
}
fn any_line_in_window(
input: &str,
(line_start, line_end): (usize, usize),
window: usize,
above_only: bool,
mut visit: impl FnMut(&str) -> bool,
) -> bool {
let mut start = line_start;
for _ in 1..window {
let Some((above_start, above_end)) = text::previous_line(input, start) else {
break;
};
start = above_start;
if line_end - start > MAX_WINDOW_BYTES {
break;
}
if visit(&input[above_start..above_end]) {
return true;
}
}
if above_only {
return false;
}
let mut end = line_end;
for _ in 1..window {
let Some((below_start, below_end)) = text::next_line(input, end) else {
break;
};
end = below_end;
if end - line_start > MAX_WINDOW_BYTES {
break;
}
if visit(&input[below_start..below_end]) {
return true;
}
}
false
}
fn line_has_member(line: &str, names: &[&str]) -> bool {
let bytes = line.as_bytes();
names.iter().any(|name| {
line.match_indices(name).any(|(start, _)| {
let end = start + name.len();
if start == 0 || bytes[start - 1] != b'"' {
return false;
}
let mut after = match (bytes.get(end), bytes.get(end + 1)) {
(Some(b'"'), _) => end + 1,
(Some(b'\\'), Some(b'"')) => end + 2,
_ => return false,
};
while bytes
.get(after)
.is_some_and(|&byte| matches!(byte, b' ' | b'\t'))
{
after += 1;
}
bytes.get(after) == Some(&b':')
})
})
}
fn json_partner(input: &str, name_start: usize, partners: &[&str]) -> bool {
let line = line_around(input, name_start);
line_has_member(&input[line.0..line.1], partners)
|| any_line_in_window(input, line, JSON_WINDOW_LINES, false, |other| {
line_has_member(other, partners)
})
}
fn line_is_revocation_context(line: &str) -> bool {
let bytes = line.as_bytes();
if text::find_ci(bytes, 0, b"token_type_hint=").is_some() {
return true;
}
for needle in [&b"/revoke"[..], &b"/introspect"[..]] {
let mut from = 0usize;
while let Some(at) = text::find_ci(bytes, from, needle) {
let mut end = at + needle.len();
if bytes.len() >= end + 6 && bytes[end..end + 6].eq_ignore_ascii_case(b"_token") {
end += 6;
}
if bytes.get(end).is_none_or(|&byte| {
matches!(
byte,
b' ' | b'\t' | b'?' | b'"' | b'\'' | b'&' | b'#' | b'\\' | b'\r' | b')'
)
}) {
return true;
}
from = at + 1;
}
}
false
}
fn revocation_context(input: &str, name_start: usize) -> bool {
let line = line_around(input, name_start);
line_is_revocation_context(&input[line.0..line.1])
|| any_line_in_window(input, line, REQUEST_WINDOW_LINES, true, |above| {
line_is_revocation_context(above)
})
}
fn line_opens_json_window(line: &str) -> bool {
line.len() <= MAX_WINDOW_BYTES && line_has_member(line, JSON_TRIGGER_MEMBERS)
}
fn line_opens_request_window(line: &str) -> bool {
line.len() <= MAX_WINDOW_BYTES && line_is_revocation_context(line)
}
fn open_window(lines: &[&str], window_lines: usize, opens: fn(&str) -> bool) -> bool {
let recent = text::tail_lines(lines, window_lines - 1);
let mut total = 0usize;
for line in recent.iter().rev() {
total += line.len();
if total > MAX_WINDOW_BYTES {
return false;
}
if opens(line) {
return true;
}
}
false
}
pub(crate) fn has_open_scoped_context_in(tail: &[&str]) -> bool {
open_window(tail, JSON_WINDOW_LINES, line_opens_json_window)
|| open_window(tail, REQUEST_WINDOW_LINES, line_opens_request_window)
}
#[cfg(test)]
pub(crate) fn has_open_scoped_context(input: &str) -> bool {
has_open_scoped_context_in(&super::lookback_tail(input))
}