Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ categories = ["network-programming", "web-programming::http-server"]

# 二进制走 CalVer,crate 走 SemVer —— 两者是两回事,卖给不同的人。
# 这里是 crate 的版本。
version = "0.70.1"
version = "0.71.0"

[workspace.dependencies]
# ── 内部 crate ───────────────────────────────────────────
Expand Down
2 changes: 2 additions & 0 deletions crates/tw-api/msg-codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ config.rotate.no_provider
config.rotate.read_back_differs
config.rule_codepoints_bad
config.rule_label_bad
config.rule_match_bad
config.rule_name_empty
config.rule_name_taken
config.rule_pattern_bad
Expand Down Expand Up @@ -500,6 +501,7 @@ security.bad_pattern
security.content_action_unknown
security.no_action_of_its_own
security.pattern_empty
security.redact_match_bad
security.rule_name_empty
security.unknown_action
security.unknown_guard
Expand Down
31 changes: 26 additions & 5 deletions crates/tw-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,19 @@ pub const MSG_CODES: &str = include_str!("../msg-codes.txt");
/// **46 起看得出密钥是账号登录换来的**:[`ProviderView`] 多了 `signed_in`([`ZaiFamily`])。
/// Z.ai / BigModel 的账号登录在它写的上游上记下 `signed_in: zai | bigmodel`;重新登录照旧
/// 写上,在界面里保存时密钥没变就留着,换成了别的密钥就去掉。手填的密钥和别的上游是 null。
pub const CONTROL_API_VERSION: u32 = 46;
///
/// **47 起出站脱敏的自定义规则能选怎么认、分不分大小写**:[`CustomRuleSave`] 和
/// [`SecurityTestRequest`] 的 `match` 出站脱敏也认(`contains` 按原样找这段文字、`regex`;
/// 不给是 `contains`,**以前是正则**),多了 `case_sensitive`(不给是不分)。规则视图里
/// 出站脱敏的自定义规则是 [`Matcher`] 的 `contains` 或 `regex`,两者都多了
/// `case_sensitive`(工具调用审查的正则开头写了 `(?i)` 的是 `false`,别的是 `true`)。安全日志的 [`RuleSnapshot`] 多了
/// `case_sensitive`,出站脱敏的自定义规则也带 `matching`。码位交给出站脱敏是 400
/// (`security.redact_match_bad`),配置里写了是 `config.rule_match_bad`。配置里不写
/// `match` 的老规则从此按原样找。
///
/// 出站脱敏不再碰请求体的键;自定义规则也不碰协议里的 id、类型、角色、工具名、模型名这些值
/// (内置规则照找)。
pub const CONTROL_API_VERSION: u32 = 47;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
Expand Down Expand Up @@ -5897,9 +5909,13 @@ pub struct RuleSnapshot {
/// 自定义规则写的样子,原样(正则、关键词、码位)。内置规则没有
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pattern: Option<String>,
/// 内容过滤:这条规则怎么认(`contains` / `regex` / `codepoints`)。别的防护没有
/// 自定义规则怎么认:内容过滤是 `contains` / `regex` / `codepoints`,出站脱敏是
/// `contains` / `regex`。内置规则和工具调用审查没有
#[serde(default, skip_serializing_if = "Option::is_none")]
pub matching: Option<ContentMatch>,
/// 出站脱敏的自定义规则:分大小写
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub case_sensitive: bool,
/// 认出它的 core 的版本
pub core_version: String,
}
Expand Down Expand Up @@ -6028,15 +6044,19 @@ pub struct ActionSave {
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
pub struct CustomRuleSave {
pub name: String,
/// 正则;内容过滤按 `match`:要找的那段文字、正则,或者码位(`U+200B, U+E0000–U+E007F`)
/// 工具调用审查是正则;出站脱敏和内容过滤按 `match`:要找的那段文字、正则,或者码位
/// (`U+200B, U+E0000–U+E007F`,只有内容过滤)
pub pattern: String,
/// 工具调用审查:`cut` / `record`;内容过滤:`block` / `strip` / `record`。不给按 `record`
#[serde(default, skip_serializing_if = "Option::is_none")]
pub action: Option<RuleAction>,
/// 内容过滤才有:`contains`(不分大小写的子串)/ `regex` / `codepoints`。不给按
/// `contains`。别的防护的自定义规则都是正则
/// 怎么认。内容过滤:`contains`(不分大小写的子串)/ `regex` / `codepoints`;出站脱敏:
/// `contains`(按原样找这段文字)/ `regex`。不给按 `contains`。工具调用审查的都是正则
#[serde(rename = "match", default, skip_serializing_if = "Option::is_none")]
pub matching: Option<ContentMatch>,
/// 出站脱敏才有:分大小写。不给是不分
#[serde(default, skip_serializing_if = "Option::is_none")]
pub case_sensitive: Option<bool>,
/// 出站脱敏才有:占位符名称,`PROJECT` 换成 `<<TW_PROJECT_1>>`。大写字母开头,其余是
/// 大写字母、数字、下划线,最多 24 个字符。不给是 `SECRET`
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -6887,6 +6907,7 @@ mod tests {
name: "plan".into(),
pattern: Some("forbidden-plan".into()),
matching: Some(ContentMatch::Contains),
case_sensitive: false,
core_version: "0.67.1".into(),
};
assert_eq!(
Expand Down
11 changes: 10 additions & 1 deletion crates/tw-config/src/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ pub fn policy_msg(e: &PolicyError) -> Msg {
"the placeholder name of custom redaction rule `{name}` is `{label}`; it has to be \
1 to 24 capital letters, digits and underscores, starting with a letter"
),
PolicyError::BadMatch { name, matching } => msg!(
"config.rule_match_bad", name = name.clone(), matching = matching.slug() =>
"custom redaction rule `{name}` matches by `{matching}`; redaction rules match by \
contains or regex"
),
PolicyError::UnknownRule { guard, id } => msg!(
"config.unknown_rule", guard = guard.slug(), rule = id.clone() =>
"security.{guard} names `{rule}`, which is not a built-in rule"
Expand Down Expand Up @@ -89,9 +94,13 @@ mod tests {
"config.rule_pattern_empty",
),
(
"redact:\n custom:\n - { name: a, pattern: '(' }",
"redact:\n custom:\n - { name: a, pattern: '(', match: regex }",
"config.rule_pattern_bad",
),
(
"redact:\n custom:\n - { name: a, pattern: x, match: codepoints }",
"config.rule_match_bad",
),
(
"content:\n custom:\n - { name: a, pattern: 'U+GG', match: codepoints }",
"config.rule_codepoints_bad",
Expand Down
2 changes: 2 additions & 0 deletions crates/tw-config/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,8 @@ groups:
.map(|(n, pat)| crate::CustomRedactRule {
name: n.to_string(),
pattern: pat.to_string(),
matching: crate::ContentMatch::Regex,
case_sensitive: true,
label: None,
disabled: false,
})
Expand Down
41 changes: 36 additions & 5 deletions crates/tw-config/tests/manual/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ fn content_actions() -> Vec<&'static str> {
fn content_matches() -> Vec<&'static str> {
super::fields::<ContentMatch>()
}
/// 出站脱敏认的那几种:码位只有内容过滤认
fn redact_matches() -> Vec<&'static str> {
super::fields::<ContentMatch>()
.into_iter()
.filter(|m| {
let m: ContentMatch = serde_yaml_ng::from_str(m).unwrap();
tw_guard::policy::redact_match("x", m, false).is_some()
})
.collect()
}
fn group_types() -> Vec<&'static str> {
super::fields::<GroupType>()
}
Expand Down Expand Up @@ -1118,8 +1128,8 @@ pub fn sections() -> Vec<Section> {
Kind::Objs("security.redact.custom[]"),
Def::Is("[]"),
t(
"Rules of your own: whatever a pattern matches is replaced like a credential.",
"自定义规则:正则匹配到的内容和凭据一样替换。",
"Rules of your own: whatever a rule matches is replaced like a credential.",
"自定义规则:匹配到的内容和凭据一样替换。",
),
),
],
Expand All @@ -1133,15 +1143,36 @@ pub fn sections() -> Vec<Section> {
"pattern",
Kind::Str,
Def::Required,
t("Regular expression.", "正则表达式。"),
t(
"The text to look for; a regular expression with `match: regex`.",
"要查找的文字;`match: regex` 时为正则表达式。",
),
),
row(
"match",
Kind::Enum(redact_matches),
Def::Is("contains"),
t(
"`contains`: `pattern` is looked for as written; characters such as `.` and `*` have no special meaning. `regex`: `pattern` is a regular expression.",
"`contains`:按原样查找 `pattern`,`.`、`*` 等字符没有特殊含义。`regex`:`pattern` 是正则表达式。",
),
),
row(
"case_sensitive",
Kind::Bool,
Def::Is("false"),
t(
"Tell capital letters from small ones. Off by default: `ABC` and `abc` both match.",
"区分大小写。默认不区分:`ABC` 和 `abc` 都会匹配。",
),
),
row(
"label",
Kind::Str,
Def::Is("SECRET"),
t(
"Placeholder name: what the pattern matches is replaced with `<<TW_label_1>>`, numbered per name. Capital letters, digits and underscores, starting with a letter, at most 24 characters.",
"占位符名称:正则匹配到的内容替换为 `<<TW_名称_序号>>`,每个名称各自编号。只能使用大写字母、数字和下划线,以字母开头,最多 24 个字符。",
"Placeholder name: what the rule matches is replaced with `<<TW_label_1>>`, numbered per name. Capital letters, digits and underscores, starting with a letter, at most 24 characters.",
"占位符名称:匹配到的内容替换为 `<<TW_名称_序号>>`,每个名称各自编号。只能使用大写字母、数字和下划线,以字母开头,最多 24 个字符。",
),
),
row("disabled", Kind::Bool, Def::Is("false"), RULE_DISABLED),
Expand Down
Loading
Loading