mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-24 23:21:18 +00:00
fix(moderation): prevent reminder tags from bypassing keyword checks
This commit is contained in:
1 parent
efe9aab1e4
commit
7b4de8b6ae
3 files changed
+313
-69
No files matched your search
@@ -909,30 +909,10 @@ func (s *ContentModerationService) Check(ctx context.Context, input ContentModer
|
||||
"configured_models", cfg.ModelFilter.Models)
|
||||
return allow, nil
|
||||
}
|
||||
content := ExtractContentModerationInput(input.Protocol, input.Body)
|
||||
if content.IsEmpty() {
|
||||
slog.Info("content_moderation.skip_empty_input",
|
||||
"user_id", input.UserID,
|
||||
"api_key_id", input.APIKeyID,
|
||||
"group_id", contentModerationLogGroupID(input.GroupID),
|
||||
"endpoint", input.Endpoint,
|
||||
"protocol", input.Protocol,
|
||||
"body_bytes", len(input.Body))
|
||||
return allow, nil
|
||||
}
|
||||
content.Normalize()
|
||||
slog.Info("content_moderation.input_extracted",
|
||||
"user_id", input.UserID,
|
||||
"api_key_id", input.APIKeyID,
|
||||
"group_id", contentModerationLogGroupID(input.GroupID),
|
||||
"endpoint", input.Endpoint,
|
||||
"protocol", input.Protocol,
|
||||
"text_runes", len([]rune(content.Text)),
|
||||
"image_count", len(content.Images))
|
||||
hashText := content.Hash()
|
||||
if cfg.Mode == ContentModerationModePreBlock {
|
||||
if cfg.KeywordBlockingMode != ContentModerationKeywordModeAPIOnly && len(cfg.BlockedKeywords) > 0 {
|
||||
if keyword, hit := runtimeSnapshot.matchBlockedKeyword(content.Text); hit {
|
||||
keywordText := extractContentModerationKeywordText(input.Protocol, input.Body)
|
||||
if keyword, hit := runtimeSnapshot.matchBlockedKeyword(keywordText); hit {
|
||||
s.recordPreBlockSyncMetric(0, ContentModerationActionKeywordBlock)
|
||||
slog.Info("content_moderation.keyword_block",
|
||||
"user_id", input.UserID,
|
||||
@@ -943,9 +923,9 @@ func (s *ContentModerationService) Check(ctx context.Context, input ContentModer
|
||||
"keyword_blocking_mode", cfg.KeywordBlockingMode,
|
||||
"keyword", keyword)
|
||||
scores := map[string]float64{contentModerationKeywordCategory: 1.0}
|
||||
log := s.buildLog(input, cfg, ContentModerationActionKeywordBlock, true, contentModerationKeywordCategory, 1.0, scores, content.ExcerptText(), nil, nil, "")
|
||||
log := s.buildLog(input, cfg, ContentModerationActionKeywordBlock, true, contentModerationKeywordCategory, 1.0, scores, keywordText, nil, nil, "")
|
||||
log.MatchedKeyword = keyword
|
||||
s.enqueueRecord(input, cfg, log, hashText, false, true)
|
||||
s.enqueueRecord(input, cfg, log, (ContentModerationInput{Text: keywordText}).Hash(), false, true)
|
||||
return &ContentModerationDecision{
|
||||
Allowed: false,
|
||||
Blocked: true,
|
||||
@@ -970,6 +950,27 @@ func (s *ContentModerationService) Check(ctx context.Context, input ContentModer
|
||||
return allow, nil
|
||||
}
|
||||
}
|
||||
content := ExtractContentModerationInput(input.Protocol, input.Body)
|
||||
if content.IsEmpty() {
|
||||
slog.Info("content_moderation.skip_empty_input",
|
||||
"user_id", input.UserID,
|
||||
"api_key_id", input.APIKeyID,
|
||||
"group_id", contentModerationLogGroupID(input.GroupID),
|
||||
"endpoint", input.Endpoint,
|
||||
"protocol", input.Protocol,
|
||||
"body_bytes", len(input.Body))
|
||||
return allow, nil
|
||||
}
|
||||
content.Normalize()
|
||||
slog.Info("content_moderation.input_extracted",
|
||||
"user_id", input.UserID,
|
||||
"api_key_id", input.APIKeyID,
|
||||
"group_id", contentModerationLogGroupID(input.GroupID),
|
||||
"endpoint", input.Endpoint,
|
||||
"protocol", input.Protocol,
|
||||
"text_runes", len([]rune(content.Text)),
|
||||
"image_count", len(content.Images))
|
||||
hashText := content.Hash()
|
||||
if cfg.PreHashCheckEnabled && s.hashCache != nil {
|
||||
matched, err := s.hashCache.HasFlaggedInputHash(ctx, hashText)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user