ssukka 0.3.0

HTML obfuscation library and CLI for Rust. Renders identically in browsers but is hard for humans to read.
Documentation
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
use crate::error::{Result, SsukkaError};
use crate::symbol_map::SymbolMap;

/// Extract class and ID names from CSS source text.
///
/// Uses lightningcss to parse the CSS and walks all rules to find
/// class selectors (`.foo`) and ID selectors (`#bar`).
pub fn extract_selectors(css: &str, symbols: &mut SymbolMap, rename_classes: bool, rename_ids: bool) {
    let stylesheet =
        lightningcss::stylesheet::StyleSheet::parse(css, lightningcss::stylesheet::ParserOptions::default());

    let stylesheet = match stylesheet {
        Ok(s) => s,
        Err(_) => return,
    };

    for rule in stylesheet.rules.0.iter() {
        extract_from_rule(rule, symbols, rename_classes, rename_ids);
    }
}

fn extract_from_rule(
    rule: &lightningcss::rules::CssRule,
    symbols: &mut SymbolMap,
    rename_classes: bool,
    rename_ids: bool,
) {
    use lightningcss::rules::CssRule;

    match rule {
        CssRule::Style(style_rule) => {
            for selector in style_rule.selectors.0.iter() {
                for component in selector.iter_raw_match_order() {
                    match component {
                        lightningcss::selector::Component::Class(name) if rename_classes => {
                            symbols.register_class(&name.0);
                        },
                        lightningcss::selector::Component::ID(name) if rename_ids => {
                            symbols.register_id(&name.0);
                        },
                        _ => {},
                    }
                }
            }
            for nested in style_rule.rules.0.iter() {
                extract_from_rule(nested, symbols, rename_classes, rename_ids);
            }
        },
        CssRule::Media(media) => {
            for r in media.rules.0.iter() {
                extract_from_rule(r, symbols, rename_classes, rename_ids);
            }
        },
        CssRule::Supports(supports) => {
            for r in supports.rules.0.iter() {
                extract_from_rule(r, symbols, rename_classes, rename_ids);
            }
        },
        // Keyframe names (grouped with class renaming). Only names *defined* here
        // are registered, so a reference to an external keyframe stays intact.
        CssRule::Keyframes(kf) if rename_classes => {
            use lightningcss::rules::keyframes::KeyframesName;
            let name = match &kf.name {
                KeyframesName::Ident(id) => &id.0,
                KeyframesName::Custom(s) => s,
            };
            symbols.register_keyframe(name);
        },
        _ => {},
    }
}

/// lightningcss visitor that renames `@keyframes` names and their
/// `animation` / `animation-name` references (all `CustomIdent`s) via the map.
struct KeyframeRenamer<'a> {
    symbols: &'a SymbolMap,
}

impl<'i> lightningcss::visitor::Visitor<'i> for KeyframeRenamer<'_> {
    type Error = std::convert::Infallible;

    fn visit_types(&self) -> lightningcss::visitor::VisitTypes {
        lightningcss::visitor::VisitTypes::CUSTOM_IDENTS
    }

    fn visit_custom_ident(
        &mut self,
        ident: &mut lightningcss::values::ident::CustomIdent<'_>,
    ) -> std::result::Result<(), Self::Error> {
        if let Some(new) = self.symbols.get_keyframe(ident.0.as_ref()) {
            ident.0 = new.to_string().into();
        }
        Ok(())
    }
}

/// Transform CSS source: rename classes/IDs, minify, and unicode-escape selectors.
pub fn transform_css(
    css: &str,
    symbols: &SymbolMap,
    minify: bool,
    unicode_escape: bool,
    rename_classes: bool,
    rename_ids: bool,
) -> Result<String> {
    let mut result = css.to_owned();

    // Rename only inside selector preludes, so a class/ID can't corrupt a
    // declaration value sharing its text (e.g. `color:#abc`, `content:".x"`).
    if rename_classes || rename_ids {
        result = rename_selectors(&result, symbols, rename_classes, rename_ids);
    }

    // Rename keyframe/animation names via a lightningcss visitor (handles the
    // shorthand). Shares the parse/print with minification when both are on.
    let rename_kf = rename_classes && !symbols.keyframes().is_empty();
    if minify || rename_kf {
        use lightningcss::visitor::Visit;
        // Parse from an owned copy so the borrow is scoped.
        let to_parse = result.clone();
        let mut stylesheet =
            lightningcss::stylesheet::StyleSheet::parse(&to_parse, lightningcss::stylesheet::ParserOptions::default())
                .map_err(|e| SsukkaError::Css(e.to_string()))?;

        if rename_kf {
            let _ = stylesheet.visit(&mut KeyframeRenamer { symbols });
        }

        let print_options = lightningcss::printer::PrinterOptions {
            minify,
            ..Default::default()
        };
        let output = stylesheet
            .to_css(print_options)
            .map_err(|e| SsukkaError::Css(e.to_string()))?;
        result = output.code;
    }

    if unicode_escape {
        result = unicode_escape_selectors(&result);
    }

    Ok(result)
}

/// Rename `.class` / `#id` selectors via the [`SymbolMap`], touching only
/// selector preludes (see [`map_selector_preludes`]).
fn rename_selectors(css: &str, symbols: &SymbolMap, rename_classes: bool, rename_ids: bool) -> String {
    map_selector_preludes(css, |prelude| {
        rewrite_prelude_names(
            prelude,
            |name| {
                if rename_classes {
                    symbols.get_class(name).map(str::to_owned)
                } else {
                    None
                }
            },
            |name| {
                if rename_ids {
                    symbols.get_id(name).map(str::to_owned)
                } else {
                    None
                }
            },
        )
    })
}

/// Apply unicode escape sequences to class and ID selectors in CSS.
///
/// Converts `.foo` -> `.\66 \6f \6f ` and `#bar` -> `#\62 \61 \72 `. Only
/// selector preludes are escaped, so hex colors / value tokens are untouched.
fn unicode_escape_selectors(css: &str) -> String {
    map_selector_preludes(css, |prelude| {
        rewrite_prelude_names(prelude, |name| Some(escape_name(name)), |name| Some(escape_name(name)))
    })
}

/// Escape every char of a selector name as `\<hex> ` (CSS unicode escape).
fn escape_name(name: &str) -> String {
    let mut out = String::with_capacity(name.len() * 4);
    for ch in name.chars() {
        out.push('\\');
        out.push_str(&format!("{:x}", ch as u32));
        out.push(' ');
    }
    out
}

/// Run `f` over each selector-prelude region of `css` (the run of text that
/// ends with `{` at brace/paren depth 0), copying declaration blocks, strings,
/// comments, and at-rule statements through verbatim.
fn map_selector_preludes(css: &str, mut f: impl FnMut(&str) -> String) -> String {
    let chars: Vec<char> = css.chars().collect();
    let n = chars.len();
    let mut out = String::with_capacity(css.len());
    let mut seg = String::new();
    let mut paren: u32 = 0;
    let mut i = 0;

    while i < n {
        let c = chars[i];

        // Comments and strings pass through into the current segment verbatim.
        if c == '/' && i + 1 < n && chars[i + 1] == '*' {
            seg.push('/');
            seg.push('*');
            i += 2;
            while i < n {
                seg.push(chars[i]);
                if chars[i] == '*' && i + 1 < n && chars[i + 1] == '/' {
                    seg.push('/');
                    i += 2;
                    break;
                }
                i += 1;
            }
            continue;
        }
        if c == '"' || c == '\'' {
            seg.push(c);
            i += 1;
            while i < n {
                let d = chars[i];
                seg.push(d);
                i += 1;
                if d == '\\' && i < n {
                    seg.push(chars[i]);
                    i += 1;
                } else if d == c {
                    break;
                }
            }
            continue;
        }

        // Track paren depth so `;` / `{` inside `url(...)`, `:not(...)`, or a
        // media query don't prematurely terminate a segment.
        match c {
            '(' => {
                paren += 1;
                seg.push(c);
                i += 1;
            },
            ')' => {
                paren = paren.saturating_sub(1);
                seg.push(c);
                i += 1;
            },
            '{' if paren == 0 => {
                out.push_str(&f(&seg));
                out.push('{');
                seg.clear();
                i += 1;
            },
            '}' | ';' if paren == 0 => {
                out.push_str(&seg);
                out.push(c);
                seg.clear();
                i += 1;
            },
            _ => {
                seg.push(c);
                i += 1;
            },
        }
    }
    out.push_str(&seg);
    out
}

/// Rewrite `.class` / `#id` names within a selector prelude. Strings and
/// comments are skipped (e.g. attribute-selector values). The callbacks receive
/// the bare name and return its replacement, or `None` to leave it unchanged.
fn rewrite_prelude_names(
    prelude: &str,
    mut on_class: impl FnMut(&str) -> Option<String>,
    mut on_id: impl FnMut(&str) -> Option<String>,
) -> String {
    let chars: Vec<char> = prelude.chars().collect();
    let n = chars.len();
    let mut out = String::with_capacity(prelude.len());
    let mut i = 0;

    while i < n {
        let c = chars[i];
        if c == '/' && i + 1 < n && chars[i + 1] == '*' {
            out.push('/');
            out.push('*');
            i += 2;
            while i < n {
                out.push(chars[i]);
                if chars[i] == '*' && i + 1 < n && chars[i + 1] == '/' {
                    out.push('/');
                    i += 2;
                    break;
                }
                i += 1;
            }
            continue;
        }
        if c == '"' || c == '\'' {
            out.push(c);
            i += 1;
            while i < n {
                let d = chars[i];
                out.push(d);
                i += 1;
                if d == '\\' && i < n {
                    out.push(chars[i]);
                    i += 1;
                } else if d == c {
                    break;
                }
            }
            continue;
        }

        // `.`/`#` + an ident start (CSS idents never start with a digit).
        if (c == '.' || c == '#')
            && i + 1 < n
            && (chars[i + 1].is_ascii_alphabetic() || chars[i + 1] == '_' || chars[i + 1] == '-')
        {
            let start = i + 1;
            let mut j = start;
            while j < n && (chars[j].is_ascii_alphanumeric() || chars[j] == '_' || chars[j] == '-') {
                j += 1;
            }
            let name: String = chars[start..j].iter().collect();
            let repl = if c == '.' { on_class(&name) } else { on_id(&name) };
            out.push(c);
            out.push_str(repl.as_deref().unwrap_or(&name));
            i = j;
            continue;
        }
        out.push(c);
        i += 1;
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn extract_class_selectors() {
        let css = ".foo { color: red; } .bar .baz { display: flex; }";
        let mut symbols = SymbolMap::new(Some(42));
        extract_selectors(css, &mut symbols, true, true);
        assert!(symbols.get_class("foo").is_some());
        assert!(symbols.get_class("bar").is_some());
        assert!(symbols.get_class("baz").is_some());
    }

    #[test]
    fn extract_id_selectors() {
        let css = "#main { width: 100%; } #sidebar { float: left; }";
        let mut symbols = SymbolMap::new(Some(42));
        extract_selectors(css, &mut symbols, true, true);
        assert!(symbols.get_id("main").is_some());
        assert!(symbols.get_id("sidebar").is_some());
    }

    #[test]
    fn unicode_escape() {
        let result = unicode_escape_selectors(".foo{color:red}");
        assert!(result.contains('\\'));
        assert!(!result.contains(".foo"));
    }

    #[test]
    fn minify_css() {
        let css = ".foo  {  color:  red;  }";
        let symbols = SymbolMap::new(Some(42));
        let result = transform_css(css, &symbols, true, false, false, false).unwrap();
        assert!(!result.contains("  "));
    }

    #[test]
    fn id_rename_skips_hex_color_values() {
        // An ID named like a hex color must not corrupt `#abc` color values.
        let mut symbols = SymbolMap::new(Some(1));
        symbols.register_id("abc");
        let obf = symbols.get_id("abc").unwrap().to_owned();
        let css = "#abc{color:#abcdef;background:#abc}";
        let out = transform_css(css, &symbols, false, false, false, true).unwrap();
        assert!(out.contains(&format!("#{obf}{{")), "selector should be renamed: {out}");
        assert!(out.contains("#abcdef"), "long hex color preserved: {out}");
        assert!(out.contains("#abc}"), "short hex color value preserved: {out}");
    }

    #[test]
    fn class_rename_skips_value_strings() {
        // `.foo` inside a `content` string must survive; the selector is renamed.
        let mut symbols = SymbolMap::new(Some(1));
        symbols.register_class("foo");
        let obf = symbols.get_class("foo").unwrap().to_owned();
        let css = r#".foo{content:".foo"}"#;
        let out = transform_css(css, &symbols, false, false, true, false).unwrap();
        assert!(out.contains(&format!(".{obf}{{")), "selector renamed: {out}");
        assert!(out.contains(r#"content:".foo""#), "string value preserved: {out}");
    }

    #[test]
    fn unicode_escape_skips_value_tokens() {
        // Only the selector is escaped; the `#fff` color value is left alone.
        let out = unicode_escape_selectors(".foo{color:#fff}");
        assert!(!out.contains(".foo"), "selector escaped: {out}");
        assert!(out.contains("#fff"), "hex color value untouched: {out}");
    }

    #[test]
    fn renames_keyframes_and_animation_references() {
        let css = "@keyframes spin{from{opacity:0}to{opacity:1}}.box{animation:spin 2s linear;animation-name:spin}";
        let mut symbols = SymbolMap::new(Some(1));
        extract_selectors(css, &mut symbols, true, true);
        let kf = symbols.get_keyframe("spin").expect("keyframe registered").to_owned();
        let out = transform_css(css, &symbols, true, false, true, false).unwrap();
        assert!(
            out.contains(&format!("@keyframes {kf}")),
            "keyframes def renamed: {out}"
        );
        assert!(out.contains(&kf), "animation reference renamed: {out}");
        assert!(!out.contains("spin"), "no original keyframe name remains: {out}");
    }

    #[test]
    fn keyframe_rename_skips_undefined_animation_names() {
        // An animation referencing a keyframe not defined here must stay intact.
        let css = ".box{animation-name:external}";
        let symbols = SymbolMap::new(Some(1));
        let out = transform_css(css, &symbols, true, false, true, false).unwrap();
        assert!(out.contains("external"), "undefined keyframe ref preserved: {out}");
    }

    #[test]
    fn rename_handles_nested_and_media_rules() {
        let mut symbols = SymbolMap::new(Some(1));
        symbols.register_class("a");
        symbols.register_class("b");
        let a = symbols.get_class("a").unwrap().to_owned();
        let b = symbols.get_class("b").unwrap().to_owned();
        let css = "@media (min-width:600px){.a{color:#aaa;.b{color:red}}}";
        let out = transform_css(css, &symbols, false, false, true, false).unwrap();
        assert!(out.contains(&format!(".{a}")), "outer class renamed: {out}");
        assert!(out.contains(&format!(".{b}")), "nested class renamed: {out}");
        assert!(out.contains("#aaa"), "color value preserved: {out}");
    }
}