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
use bitflags::bitflags;

bitflags!{
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    pub struct SandboxingFlagSet: u32 {
        const SANDBOXED_NAVIGATION_BROWSING_CONTEXT_FLAG = 0x00000001;
        const SANDBOXED_AUXILIARY_NAVIGATION_BROWSING_CONTEXT_FLAG = 0x00000002;
        const SANDBOXED_TOP_LEVEL_NAVIGATION_WITHOUT_USER_ACTIVATION_BROWSING_CONTEXT_FLAG
            = 0x00000004;
        const SANDBOXED_TOP_LEVEL_NAVIGATION_WITH_USER_ACTIVATION_BROWSING_CONTEXT_FLAG
            = 0x00000008;
        const SANDBOXED_PLUGINS_BROWSING_CONTEXT_FLAG = 0x00000010;
        const SANDBOXED_ORIGIN_BROWSING_CONTEXT_FLAG = 0x00000020;
        const SANDBOXED_FORMS_BROWSING_CONTEXT_FLAG = 0x00000040;
        const SANDBOXED_POINTER_LOCK_BROWSING_CONTEXT_FLAG = 0x00000080;
        const SANDBOXED_SCRIPTS_BROWSING_CONTEXT_FLAG = 0x00000100;
        const SANDBOXED_AUTOMATIC_FEATURES_BROWSING_CONTEXT_FLAG = 0x00000200;
        const SANDBOXED_STORAGE_AREA_URLS_FLAG = 0x00000400;
        const SANDBOXED_DOCUMENT_DOMAIN_BROWSING_CONTEXT_FLAG = 0x00000800;
        const SANDBOX_PROPOGATES_TO_AUXILIARY_BROWSING_CONTEXTS_FLAG = 0x00001000;
        const SANDBOXED_MODALS_FLAG = 0x00002000;
        const SANDBOXED_ORIENTATION_LOCK_BROWSING_CONTEXT_FLAG = 0x00004000;
        const SANDBOXED_PRESENTATION_BROWSING_CONTEXT_FLAG = 0x00008000;
    }
}

pub fn parse_a_sandboxing_directive(tokens: &[String]) -> SandboxingFlagSet {
    let mut output = SandboxingFlagSet::all();
    for token in tokens {
        let remove = match &token[..] {
            "allow-popups" =>
                SandboxingFlagSet::SANDBOXED_AUXILIARY_NAVIGATION_BROWSING_CONTEXT_FLAG,
            "allow-top-navigation" =>
                SandboxingFlagSet::SANDBOXED_TOP_LEVEL_NAVIGATION_WITHOUT_USER_ACTIVATION_BROWSING_CONTEXT_FLAG |
                    SandboxingFlagSet::SANDBOXED_TOP_LEVEL_NAVIGATION_WITH_USER_ACTIVATION_BROWSING_CONTEXT_FLAG,
            "allow-top-navigation-by-user-activation" =>
                SandboxingFlagSet::SANDBOXED_TOP_LEVEL_NAVIGATION_WITH_USER_ACTIVATION_BROWSING_CONTEXT_FLAG,
            "allow-same-origin" =>
                SandboxingFlagSet::SANDBOXED_ORIGIN_BROWSING_CONTEXT_FLAG,
            "allow-forms" =>
                SandboxingFlagSet::SANDBOXED_FORMS_BROWSING_CONTEXT_FLAG,
            "allow-pointer-lock" =>
                SandboxingFlagSet::SANDBOXED_POINTER_LOCK_BROWSING_CONTEXT_FLAG,
            "allow-scripts" =>
                SandboxingFlagSet::SANDBOXED_SCRIPTS_BROWSING_CONTEXT_FLAG |
                    SandboxingFlagSet::SANDBOXED_AUTOMATIC_FEATURES_BROWSING_CONTEXT_FLAG,
            "allow-popups-to-escape-sandbox" =>
                SandboxingFlagSet::SANDBOX_PROPOGATES_TO_AUXILIARY_BROWSING_CONTEXTS_FLAG,
            "allow-modals" =>
                SandboxingFlagSet::SANDBOXED_MODALS_FLAG,
            "allow-orientation-lock" =>
                SandboxingFlagSet::SANDBOXED_ORIENTATION_LOCK_BROWSING_CONTEXT_FLAG,
            "allow-presentation" =>
                SandboxingFlagSet::SANDBOXED_PRESENTATION_BROWSING_CONTEXT_FLAG,
            _ =>
                SandboxingFlagSet::empty(),
        };
        output.remove(remove);
    }
    output
}