-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
got it working, need to clean up and test
- Loading branch information
1 parent
510817b
commit 0c1bff6
Showing
14 changed files
with
820 additions
and
177 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! The escape codes module provides an online (trie based) matcher | ||
//! to scan for escape codes we are interested in in the output of | ||
//! the subshell. For the moment, we just use this to scan for | ||
//! the ClearScreen code emitted by the prompt prefix injection shell | ||
//! code. We need to scan for this to avoid a race that can lead to | ||
//! the motd getting clobbered when in dump mode. | ||
use anyhow::{anyhow, Context}; | ||
|
||
use super::trie::{TrieCursor, Trie}; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum Code { | ||
ClearScreen, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Matcher { | ||
codes: Trie<u8, Code, Vec<Option<usize>>>, | ||
codes_cursor: TrieCursor, | ||
} | ||
|
||
impl Matcher { | ||
pub fn new(term_db: &terminfo::Database) -> anyhow::Result<Self> { | ||
let clear_code = term_db.get::<terminfo::capability::ClearScreen>() | ||
.ok_or(anyhow!("no clear screen code"))?; | ||
let clear_code_bytes = clear_code.expand().to_vec().context("expanding clear code")?; | ||
|
||
// TODO: delete | ||
let clear_code_hex_bytes = clear_code_bytes.iter().map(|b| format!("{:02x}", b)).collect::<Vec<_>>().join(" "); | ||
tracing::debug!("clear_code_hex_bytes={}", clear_code_hex_bytes); | ||
|
||
let raw_bindings = vec![ | ||
// We need to scan for the clear code that gets emitted by the prompt prefix | ||
// shell injection code so that we can make sure that the message of the day | ||
// won't get clobbered immediately. | ||
(clear_code_bytes, Code::ClearScreen), | ||
]; | ||
let mut codes = Trie::new(); | ||
for (raw_bytes, code) in raw_bindings.into_iter() { | ||
codes.insert(raw_bytes.into_iter(), code); | ||
} | ||
|
||
Ok(Matcher { | ||
codes, | ||
codes_cursor: TrieCursor::Start, | ||
}) | ||
} | ||
|
||
pub fn transition(&mut self, byte: u8) -> Option<Code> { | ||
let old_cursor = self.codes_cursor; | ||
self.codes_cursor = self.codes.advance(self.codes_cursor, byte); | ||
tracing::debug!("TRANSITION({:?}, {:02x}) -> {:?}", old_cursor, byte, self.codes_cursor); // TODO: delete | ||
match self.codes_cursor { | ||
TrieCursor::NoMatch => { | ||
self.codes_cursor = TrieCursor::Start; | ||
None | ||
}, | ||
TrieCursor::Match { is_partial, .. } if !is_partial => { | ||
let code = self.codes.get(self.codes_cursor).map(|c| *c); | ||
tracing::debug!("TRANSITION MATCH: code={:?}", code); | ||
self.codes_cursor = TrieCursor::Start; | ||
code | ||
} | ||
_ => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.