rsigma-lsp 0.12.0

Language Server Protocol (LSP) server for Sigma detection and correlation rules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! LSP server implementation for Sigma detection rules.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::{Mutex, RwLock};
use tower_lsp_server::jsonrpc::Result;
use tower_lsp_server::ls_types::*;
use tower_lsp_server::{Client, LanguageServer};

use rsigma_parser::lint::LintConfig;

use crate::code_action;
use crate::completion;
use crate::data;
use crate::diagnostics;

/// Debounce delay for diagnostics on text changes (milliseconds).
const DIAGNOSTICS_DEBOUNCE_MS: u64 = 150;

/// In-memory document state: latest text and version for each open file.
#[derive(Debug, Clone)]
struct DocumentState {
    text: String,
    version: i32,
}

/// The Sigma Language Server.
pub struct SigmaLanguageServer {
    client: Client,
    documents: Arc<RwLock<HashMap<Uri, DocumentState>>>,
    /// Abort handles for pending debounced diagnostic tasks.
    pending_diagnostics: Arc<Mutex<HashMap<Uri, tokio::task::AbortHandle>>>,
}

impl SigmaLanguageServer {
    pub fn new(client: Client) -> Self {
        Self {
            client,
            documents: Arc::new(RwLock::new(HashMap::new())),
            pending_diagnostics: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Load lint config for a document by walking ancestors from the file URI.
    fn load_lint_config(uri: &Uri) -> LintConfig {
        if let Some(path) = uri.to_file_path()
            && let Some(config_path) = LintConfig::find_in_ancestors(&path)
            && let Ok(config) = LintConfig::load(&config_path)
        {
            return config;
        }
        LintConfig::default()
    }

    /// Run diagnostics immediately on a blocking thread and publish results.
    async fn publish_diagnostics_now(&self, uri: &Uri, text: &str, version: i32) {
        let text = text.to_string();
        let config = Self::load_lint_config(uri);
        let diags = match tokio::task::spawn_blocking(move || {
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                diagnostics::diagnose_with_config(&text, &config)
            }))
        })
        .await
        {
            Ok(Ok(d)) => d,
            Ok(Err(_)) => {
                log::error!("panic in diagnostics::diagnose");
                vec![]
            }
            Err(e) => {
                log::error!("diagnostics task error: {e}");
                vec![]
            }
        };
        self.client
            .publish_diagnostics(uri.clone(), diags, Some(version))
            .await;
    }

    /// Schedule diagnostics with debounce. Cancels any pending run for this URI.
    async fn schedule_diagnostics(&self, uri: Uri, text: String, version: i32) {
        let client = self.client.clone();
        let pending = self.pending_diagnostics.clone();
        let uri_for_task = uri.clone();
        let config = Self::load_lint_config(&uri);

        // Abort any existing pending task, then spawn the new one while holding
        // the lock so no concurrent call can slip in between.
        let mut guard: tokio::sync::MutexGuard<'_, _> = self.pending_diagnostics.lock().await;
        if let Some(handle) = guard.remove(&uri) {
            handle.abort();
        }

        let task = tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(DIAGNOSTICS_DEBOUNCE_MS)).await;

            let diags = match tokio::task::spawn_blocking(move || {
                std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    diagnostics::diagnose_with_config(&text, &config)
                }))
            })
            .await
            {
                Ok(Ok(d)) => d,
                Ok(Err(_)) => {
                    log::error!("panic in diagnostics::diagnose");
                    vec![]
                }
                Err(_) => return, // task was cancelled
            };

            client
                .publish_diagnostics(uri_for_task.clone(), diags, Some(version))
                .await;

            let _: Option<_> = pending.lock().await.remove(&uri_for_task);
        });

        guard.insert(uri, task.abort_handle());
    }

    /// Check if a URI looks like a Sigma YAML file.
    fn is_sigma_file(uri: &Uri) -> bool {
        let path = uri.path().as_str();
        path.ends_with(".yml") || path.ends_with(".yaml")
    }
}

impl LanguageServer for SigmaLanguageServer {
    async fn initialize(&self, _params: InitializeParams) -> Result<InitializeResult> {
        Ok(InitializeResult {
            capabilities: ServerCapabilities {
                // Full text sync — we get the entire document on every change.
                text_document_sync: Some(TextDocumentSyncCapability::Kind(
                    TextDocumentSyncKind::FULL,
                )),
                // Completions
                completion_provider: Some(CompletionOptions {
                    trigger_characters: Some(vec!["|".into(), ":".into(), " ".into(), "\n".into()]),
                    resolve_provider: Some(false),
                    ..Default::default()
                }),
                // Code actions (quick-fixes from lint)
                code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
                // Hover
                hover_provider: Some(HoverProviderCapability::Simple(true)),
                // Document symbols
                document_symbol_provider: Some(OneOf::Left(true)),
                ..Default::default()
            },
            server_info: Some(ServerInfo {
                name: "rsigma-lsp".to_string(),
                version: Some(env!("CARGO_PKG_VERSION").to_string()),
            }),
            offset_encoding: None,
        })
    }

    async fn initialized(&self, _params: InitializedParams) {
        log::info!("rsigma-lsp initialized");
    }

    async fn shutdown(&self) -> Result<()> {
        Ok(())
    }

    // ── Document synchronization ────────────────────────────────────────

    async fn did_open(&self, params: DidOpenTextDocumentParams) {
        let uri = params.text_document.uri;
        let text = params.text_document.text;
        let version = params.text_document.version;

        if Self::is_sigma_file(&uri) {
            self.publish_diagnostics_now(&uri, &text, version).await;
        }

        let mut docs: tokio::sync::RwLockWriteGuard<'_, _> = self.documents.write().await;
        docs.insert(uri, DocumentState { text, version });
    }

    async fn did_change(&self, params: DidChangeTextDocumentParams) {
        let uri = params.text_document.uri;
        let version = params.text_document.version;

        // FULL sync: the last content change has the full text.
        if let Some(change) = params.content_changes.into_iter().last() {
            let text = change.text;

            if Self::is_sigma_file(&uri) {
                self.schedule_diagnostics(uri.clone(), text.clone(), version)
                    .await;
            }

            let mut docs: tokio::sync::RwLockWriteGuard<'_, _> = self.documents.write().await;
            docs.insert(uri, DocumentState { text, version });
        }
    }

    async fn did_save(&self, params: DidSaveTextDocumentParams) {
        let uri = params.text_document.uri;

        // Re-lint on save (belt and suspenders — didChange already does it).
        if Self::is_sigma_file(&uri) {
            let docs: tokio::sync::RwLockReadGuard<'_, _> = self.documents.read().await;
            if let Some(doc) = docs.get(&uri) {
                self.publish_diagnostics_now(&uri, &doc.text, doc.version)
                    .await;
            }
        }
    }

    async fn did_close(&self, params: DidCloseTextDocumentParams) {
        let uri = params.text_document.uri;

        // Cancel any pending diagnostics for this file.
        {
            let mut pending: tokio::sync::MutexGuard<'_, _> = self.pending_diagnostics.lock().await;
            if let Some(handle) = pending.remove(&uri) {
                handle.abort();
            }
        }

        let mut docs: tokio::sync::RwLockWriteGuard<'_, _> = self.documents.write().await;
        docs.remove(&uri);
        drop(docs);

        // Clear diagnostics for the closed file.
        self.client.publish_diagnostics(uri, vec![], None).await;
    }

    // ── Code actions (quick-fixes) ────────────────────────────────────

    async fn code_action(&self, params: CodeActionParams) -> Result<Option<CodeActionResponse>> {
        let uri = &params.text_document.uri;

        let docs: tokio::sync::RwLockReadGuard<'_, _> = self.documents.read().await;
        let Some(doc) = docs.get(uri) else {
            return Ok(None);
        };

        let text = doc.text.clone();
        let config = Self::load_lint_config(uri);
        let range = params.range;
        let uri_owned = uri.clone();

        let actions = match tokio::task::spawn_blocking(move || {
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                code_action::code_actions(&uri_owned, &text, &range, &config)
            }))
        })
        .await
        {
            Ok(Ok(a)) => a,
            Ok(Err(_)) => {
                log::error!("panic in code_action::code_actions");
                vec![]
            }
            Err(e) => {
                log::error!("code action task error: {e}");
                vec![]
            }
        };

        if actions.is_empty() {
            Ok(None)
        } else {
            Ok(Some(
                actions
                    .into_iter()
                    .map(CodeActionOrCommand::CodeAction)
                    .collect(),
            ))
        }
    }

    // ── Completions ─────────────────────────────────────────────────────

    async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> {
        let uri = &params.text_document_position.text_document.uri;
        let position = params.text_document_position.position;

        let docs: tokio::sync::RwLockReadGuard<'_, _> = self.documents.read().await;
        let Some(doc) = docs.get(uri) else {
            return Ok(None);
        };

        let text = doc.text.clone();
        let items = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            completion::complete(&text, position)
        })) {
            Ok(items) => items,
            Err(_) => {
                log::error!("panic in completion::complete");
                vec![]
            }
        };
        if items.is_empty() {
            Ok(None)
        } else {
            Ok(Some(CompletionResponse::Array(items)))
        }
    }

    // ── Hover ───────────────────────────────────────────────────────────

    async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
        let uri = &params.text_document_position_params.text_document.uri;
        let position = params.text_document_position_params.position;

        let docs: tokio::sync::RwLockReadGuard<'_, _> = self.documents.read().await;
        let Some(doc) = docs.get(uri) else {
            return Ok(None);
        };

        let text = doc.text.clone();
        let result = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            hover_at(&text, position)
        })) {
            Ok(r) => r,
            Err(_) => {
                log::error!("panic in hover_at");
                None
            }
        };
        Ok(result)
    }

    // ── Document symbols ────────────────────────────────────────────────

    async fn document_symbol(
        &self,
        params: DocumentSymbolParams,
    ) -> Result<Option<DocumentSymbolResponse>> {
        let uri = &params.text_document.uri;

        let docs: tokio::sync::RwLockReadGuard<'_, _> = self.documents.read().await;
        let Some(doc) = docs.get(uri) else {
            return Ok(None);
        };

        let text = doc.text.clone();
        let symbols = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            document_symbols(&text)
        })) {
            Ok(s) => s,
            Err(_) => {
                log::error!("panic in document_symbols");
                vec![]
            }
        };
        if symbols.is_empty() {
            Ok(None)
        } else {
            Ok(Some(DocumentSymbolResponse::Nested(symbols)))
        }
    }
}

// =============================================================================
// Hover — MITRE ATT&CK tags, modifier help
// =============================================================================

fn hover_at(text: &str, position: Position) -> Option<Hover> {
    let line = text.lines().nth(position.line as usize)?;
    let word = word_at(line, position.character as usize)?;

    // MITRE ATT&CK tag hover
    if let Some(info) = mitre_hover(word) {
        return Some(Hover {
            contents: HoverContents::Markup(MarkupContent {
                kind: MarkupKind::Markdown,
                value: info,
            }),
            range: None,
        });
    }

    // Modifier hover
    if let Some(info) = modifier_hover(word) {
        return Some(Hover {
            contents: HoverContents::Markup(MarkupContent {
                kind: MarkupKind::Markdown,
                value: info,
            }),
            range: None,
        });
    }

    None
}

/// Extract the word at a given column position (UTF-8 safe).
fn word_at(line: &str, col: usize) -> Option<&str> {
    // LSP columns are UTF-16 offsets; approximate as byte offset but clamp
    // to the nearest char boundary to avoid panics.
    let col = col.min(line.len());

    // Snap to nearest char boundary
    let col = if line.is_char_boundary(col) {
        col
    } else {
        // Walk backward to find a valid boundary
        (0..col)
            .rev()
            .find(|&i| line.is_char_boundary(i))
            .unwrap_or(0)
    };

    let is_word_byte = |b: u8| b.is_ascii_alphanumeric() || b == b'_' || b == b'.' || b == b'-';

    let bytes = line.as_bytes();

    let mut start = col;
    while start > 0 && start <= bytes.len() && is_word_byte(bytes[start - 1]) {
        start -= 1;
    }

    let mut end = col;
    while end < bytes.len() && is_word_byte(bytes[end]) {
        end += 1;
    }

    if start == end {
        return None;
    }

    // Final boundary check
    if line.is_char_boundary(start) && line.is_char_boundary(end) {
        Some(&line[start..end])
    } else {
        None
    }
}

fn mitre_hover(word: &str) -> Option<String> {
    // Match patterns like attack.t1059, attack.t1059.001, etc.
    let lower = word.to_lowercase();
    if lower.starts_with("attack.t") {
        let technique = &lower["attack.".len()..];
        return Some(format!(
            "**MITRE ATT&CK Technique**\n\n\
             `{}`\n\n\
             [View on MITRE ATT&CK](https://attack.mitre.org/techniques/{})",
            technique.to_uppercase(),
            technique.to_uppercase().replace('.', "/")
        ));
    }

    // Tactic hover from shared data
    data::MITRE_TACTICS
        .iter()
        .find(|(tag, _)| lower == *tag)
        .map(|(_, description)| format!("**MITRE ATT&CK Tactic**\n\n{description}"))
}

fn modifier_hover(word: &str) -> Option<String> {
    data::MODIFIERS
        .iter()
        .find(|(name, _)| *name == word)
        .map(|(name, desc)| format!("**`{name}`** \u{2014} {desc}"))
}

// =============================================================================
// Document symbols (hierarchical DocumentSymbol tree)
// =============================================================================

/// Build a hierarchical document symbol tree from the Sigma YAML text.
#[allow(deprecated)] // DocumentSymbol::deprecated field is itself deprecated
fn document_symbols(text: &str) -> Vec<DocumentSymbol> {
    let lines: Vec<&str> = text.lines().collect();
    let mut symbols = Vec::new();
    let sections = find_top_level_sections(&lines);

    for (key, value, start, end) in &sections {
        match key.as_str() {
            "title" => {
                let title = value.trim();
                if !title.is_empty() {
                    let sel = symbol_line_range(*start, lines[*start]);
                    symbols.push(DocumentSymbol {
                        name: title.to_string(),
                        detail: Some("title".to_string()),
                        kind: SymbolKind::STRING,
                        tags: None,
                        deprecated: None,
                        range: sel,
                        selection_range: sel,
                        children: None,
                    });
                }
            }
            "logsource" | "correlation" => {
                let sel = symbol_line_range(*start, lines[*start]);
                let full = symbol_section_range(*start, *end, &lines);
                symbols.push(DocumentSymbol {
                    name: key.clone(),
                    detail: None,
                    kind: SymbolKind::NAMESPACE,
                    tags: None,
                    deprecated: None,
                    range: full,
                    selection_range: sel,
                    children: None,
                });
            }
            "detection" => {
                let sel = symbol_line_range(*start, lines[*start]);
                let full = symbol_section_range(*start, *end, &lines);
                let children = detection_children(&lines, *start, *end);
                symbols.push(DocumentSymbol {
                    name: "detection".to_string(),
                    detail: None,
                    kind: SymbolKind::NAMESPACE,
                    tags: None,
                    deprecated: None,
                    range: full,
                    selection_range: sel,
                    children: if children.is_empty() {
                        None
                    } else {
                        Some(children)
                    },
                });
            }
            _ => {}
        }
    }

    symbols
}

/// Find top-level YAML sections: `(key, value_after_colon, start_line, end_line)`.
fn find_top_level_sections(lines: &[&str]) -> Vec<(String, String, usize, usize)> {
    let mut sections = Vec::new();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i];
        let trimmed = line.trim();

        if trimmed.is_empty() || trimmed.starts_with('#') || trimmed == "---" {
            i += 1;
            continue;
        }

        let indent = line.len() - trimmed.len();
        if indent == 0
            && let Some(colon_pos) = trimmed.find(':')
        {
            let key = trimmed[..colon_pos].to_string();
            let value = trimmed[colon_pos + 1..].to_string();
            let start_line = i;

            // Find end of section: next top-level key or EOF
            let mut end_line = i;
            let mut j = i + 1;
            while j < lines.len() {
                let jline = lines[j];
                let jtrimmed = jline.trim();
                if jtrimmed.is_empty() || jtrimmed.starts_with('#') {
                    j += 1;
                    continue;
                }
                let jindent = jline.len() - jtrimmed.len();
                if jindent == 0 && jtrimmed.contains(':') {
                    break;
                }
                end_line = j;
                j += 1;
            }

            sections.push((key, value, start_line, end_line));
            i = j;
            continue;
        }

        i += 1;
    }

    sections
}

/// Build child symbols for the detection block, dynamically detecting indent.
#[allow(deprecated)]
fn detection_children(
    lines: &[&str],
    section_start: usize,
    section_end: usize,
) -> Vec<DocumentSymbol> {
    let mut children = Vec::new();
    let mut detection_indent: Option<usize> = None;

    let mut i = section_start + 1;
    while i <= section_end {
        let line = lines[i];
        let trimmed = line.trim();

        if trimmed.is_empty() || trimmed.starts_with('#') {
            i += 1;
            continue;
        }

        let indent = line.len() - trimmed.len();

        // First indented line with a colon sets the detection indent level
        if detection_indent.is_none() && indent > 0 && trimmed.contains(':') {
            detection_indent = Some(indent);
        }

        if let Some(det_indent) = detection_indent
            && indent == det_indent
            && let Some(colon_pos) = trimmed.find(':')
        {
            let key = trimmed[..colon_pos].trim();
            if key.is_empty() {
                i += 1;
                continue;
            }

            // Find the end of this child section
            let child_start = i;
            let mut child_end = i;
            let mut j = i + 1;
            while j <= section_end {
                let jline = lines[j];
                let jtrimmed = jline.trim();
                if jtrimmed.is_empty() || jtrimmed.starts_with('#') {
                    j += 1;
                    continue;
                }
                let jindent = jline.len() - jtrimmed.len();
                if jindent <= det_indent {
                    break;
                }
                child_end = j;
                j += 1;
            }

            let kind = if key == "condition" {
                SymbolKind::BOOLEAN
            } else {
                SymbolKind::FIELD
            };

            let sel = symbol_line_range(child_start, lines[child_start]);
            let full = symbol_section_range(child_start, child_end, lines);

            children.push(DocumentSymbol {
                name: key.to_string(),
                detail: None,
                kind,
                tags: None,
                deprecated: None,
                range: full,
                selection_range: sel,
                children: None,
            });

            i = j;
            continue;
        }

        i += 1;
    }

    children
}

/// Range covering a single line.
fn symbol_line_range(line_idx: usize, line: &str) -> Range {
    let start = Position::new(line_idx as u32, 0);
    let end_col = line.len().min(u32::MAX as usize) as u32;
    Range::new(start, Position::new(line_idx as u32, end_col))
}

/// Range covering from `start_line` to `end_line` (inclusive).
fn symbol_section_range(start_line: usize, end_line: usize, lines: &[&str]) -> Range {
    let start = Position::new(start_line as u32, 0);
    let end_col = lines[end_line].len().min(u32::MAX as usize) as u32;
    Range::new(start, Position::new(end_line as u32, end_col))
}