From eb88bfd70ee0015a57738e881ce5c6f74a519ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 24 Jul 2021 23:25:29 +0200 Subject: [PATCH] update policies + chromium. --- internal/policies/chromium/generator.go | 90 ++++++++++++++++++++++++ internal/policies/chromium/policies.json | 38 ++++++++++ internal/policies/firefox/generator.go | 87 ++++++++++------------- internal/policies/firefox/policies.json | 11 +-- internal/types/policies.go | 13 ++++ 5 files changed, 179 insertions(+), 60 deletions(-) create mode 100644 internal/policies/chromium/generator.go create mode 100644 internal/policies/chromium/policies.json create mode 100644 internal/types/policies.go diff --git a/internal/policies/chromium/generator.go b/internal/policies/chromium/generator.go new file mode 100644 index 0000000..2a0f372 --- /dev/null +++ b/internal/policies/chromium/generator.go @@ -0,0 +1,90 @@ +package chromium + +// https://chromeenterprise.google/policies/ + +import ( + _ "embed" + "encoding/json" + "fmt" + "m1k1o/neko_rooms/internal/types" +) + +//go:embed policies.json +var policiesJson string + +func Generate(policies types.Policies) (string, error) { + policiesTmpl := map[string]interface{}{} + if err := json.Unmarshal([]byte(policiesJson), &policiesTmpl); err != nil { + return "", err + } + + // + // Homepage + // + + if policies.Homepage != "" { + policiesTmpl["HomepageLocation"] = policies.Homepage + } + + // + // Extensions + // + + ExtensionInstallForcelist := []interface{}{} + for _, e := range policies.Extensions { + URL := e.URL + if URL == "" { + URL = "https://clients2.google.com/service/update2/crx" + } + + ExtensionInstallForcelist = append( + ExtensionInstallForcelist, + fmt.Sprintf("%s;%s", e.ID, URL), + ) + } + + ExtensionInstallWhitelist := []interface{}{} + for _, e := range policies.Extensions { + ExtensionInstallWhitelist = append( + ExtensionInstallWhitelist, + e.ID, + ) + } + + policiesTmpl["ExtensionInstallForcelist"] = ExtensionInstallForcelist + policiesTmpl["ExtensionInstallWhitelist"] = ExtensionInstallWhitelist + policiesTmpl["ExtensionInstallBlacklist"] = []interface{}{"*"} + + // + // Developer Tools + // + + if policies.DeveloperTools { + // Allow usage of the Developer Tools + policiesTmpl["DeveloperToolsAvailability"] = 1 + } else { + // Disallow usage of the Developer Tools + policiesTmpl["DeveloperToolsAvailability"] = 2 + } + + // + // Persistent Data + // + + if policies.PersistentData { + // Allow all sites to set local data + policiesTmpl["DefaultCookiesSetting"] = 1 + // Restore the last session + policiesTmpl["RestoreOnStartup"] = 1 + } else { + // Keep cookies for the duration of the session + policiesTmpl["DefaultCookiesSetting"] = 4 + } + + data, err := json.MarshalIndent(policiesTmpl, "", " ") + if err != nil { + return "", err + } + + return string(data), nil +} diff --git a/internal/policies/chromium/policies.json b/internal/policies/chromium/policies.json new file mode 100644 index 0000000..786c2b1 --- /dev/null +++ b/internal/policies/chromium/policies.json @@ -0,0 +1,38 @@ +{ + "HomepageLocation": "", + "AutoFillEnabled": false, + "AutofillAddressEnabled": false, + "AutofillCreditCardEnabled": false, + "BrowserSignin": 0, + "DefaultNotificationsSetting": 2, + "DeveloperToolsAvailability": 2, + "EditBookmarksEnabled": false, + "FullscreenAllowed": true, + "IncognitoModeAvailability": 1, + "SyncDisabled": true, + "AutoplayAllowed": true, + "BrowserAddPersonEnabled": false, + "BrowserGuestModeEnabled": false, + "DefaultPopupsSetting": 2, + "DownloadRestrictions": 3, + "VideoCaptureAllowed": true, + "AllowFileSelectionDialogs": false, + "PromptForDownloadLocation": false, + "BookmarkBarEnabled": false, + "PasswordManagerEnabled": false, + "URLBlacklist": [ + "file://*", + "chrome://policy" + ], + "ExtensionInstallForcelist": [ + "cjpalhdlnbpafiamejdnhcphjbkeiagm;https://clients2.google.com/service/update2/crx", + "fjoaledfpmneenckfbpdfhkmimnjocfa;https://clients2.google.com/service/update2/crx" + ], + "ExtensionInstallWhitelist": [ + "cjpalhdlnbpafiamejdnhcphjbkeiagm", + "fjoaledfpmneenckfbpdfhkmimnjocfa" + ], + "ExtensionInstallBlacklist": [ + "*" + ] +} diff --git a/internal/policies/firefox/generator.go b/internal/policies/firefox/generator.go index 528f678..4f1fac7 100644 --- a/internal/policies/firefox/generator.go +++ b/internal/policies/firefox/generator.go @@ -1,87 +1,74 @@ package firefox +// https://github.com/mozilla/policy-templates/blob/master/README.md#homepage + import ( _ "embed" "encoding/json" - "net/url" + "m1k1o/neko_rooms/internal/types" ) -type Settings struct { - Bookmarks []string `json:"bookmarks"` - Extensions map[string]string `json:"extensions"` - DeveloperTools bool `json:"developer_tools"` -} - //go:embed policies.json var policiesJson string -func Generate(settings Settings) (string, error) { - policiesObj := struct { +func Generate(policies types.Policies) (string, error) { + policiesTmpl := struct { Policies map[string]interface{} `json:"policies"` }{} - if err := json.Unmarshal([]byte(policiesJson), &policiesObj); err != nil { + if err := json.Unmarshal([]byte(policiesJson), &policiesTmpl); err != nil { return "", err } // - // Bookmarks + // Homepage // - // Do not append - //Bookmarks := policiesObj.Policies["Bookmarks"].([]interface{}) - Bookmarks := []interface{}{} - - for _, URL := range settings.Bookmarks { - title := URL - - if u, err := url.Parse(URL); err == nil { - title = u.Hostname() + if policies.Homepage != "" { + policiesTmpl.Policies["Homepage"] = map[string]interface{}{ + "URL": policies.Homepage, + "StartPage": "homepage", } - - Bookmarks = append(Bookmarks, map[string]interface{}{ - "Title": title, - "URL": URL, - //"Favicon": URL + "/favicon.ico", - "Folder": "Pages", - "Placement": "toolbar", - }) - } - - if len(settings.Bookmarks) > 0 { - policiesObj.Policies["Bookmarks"] = Bookmarks } // // Extensions // - // Do not append - //Extensions := policiesObj.Policies["ExtensionSettings"].(map[string]interface{}) - Extensions := map[string]interface{}{} - - // block all - Extensions["*"] = map[string]interface{}{ + ExtensionSettings := map[string]interface{}{} + ExtensionSettings["*"] = map[string]interface{}{ "installation_mode": "blocked", } - for id, url := range settings.Extensions { - Extensions[id] = map[string]interface{}{ - "install_url": url, + for _, e := range policies.Extensions { + ExtensionSettings[e.ID] = map[string]interface{}{ + "install_url": e.URL, "installation_mode": "force_installed", } } - if len(settings.Extensions) > 0 { - policiesObj.Policies["ExtensionSettings"] = Extensions + policiesTmpl.Policies["ExtensionSettings"] = ExtensionSettings + + // + // Developer Tools + // + + policiesTmpl.Policies["DisableDeveloperTools"] = !policies.DeveloperTools + + // + // Persistent Data + // + + if policies.PersistentData { + Preferences := policiesTmpl.Policies["Preferences"].(map[string]interface{}) + Preferences["browser.urlbar.suggest.history"] = true + Preferences["places.history.enabled"] = true + policiesTmpl.Policies["Preferences"] = Preferences + policiesTmpl.Policies["SanitizeOnShutdown"] = false + } else { + policiesTmpl.Policies["SanitizeOnShutdown"] = true } - // - // DeveloperTools - // - - policiesObj.Policies["DisableDeveloperTools"] = settings.DeveloperTools - - data, err := json.MarshalIndent(policiesObj, "", " ") + data, err := json.MarshalIndent(policiesTmpl, "", " ") if err != nil { return "", err } diff --git a/internal/policies/firefox/policies.json b/internal/policies/firefox/policies.json index ef4bdc1..0a0132f 100644 --- a/internal/policies/firefox/policies.json +++ b/internal/policies/firefox/policies.json @@ -115,15 +115,6 @@ "ui.key.menuAccessKeyFocuses": false }, "PromptForDownloadLocation": false, - "SanitizeOnShutdown": { - "Cache": true, - "Cookies": true, - "Downloads": true, - "FormData": true, - "History": true, - "OfflineApps": true, - "Sessions": true, - "SiteSettings": true - } + "SanitizeOnShutdown": true } } diff --git a/internal/types/policies.go b/internal/types/policies.go new file mode 100644 index 0000000..973c989 --- /dev/null +++ b/internal/types/policies.go @@ -0,0 +1,13 @@ +package types + +type Policies struct { + Homepage string `json:"homepage"` + Extensions []Extension `json:"extensions"` + DeveloperTools bool `json:"developer_tools"` + PersistentData bool `json:"persistent_data"` +} + +type Extension struct { + ID string `json:"id"` + URL string `json:"url"` +}