diff --git a/cli/flags.go b/cli/flags.go index 4695d53..b7c2c4e 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -50,6 +50,17 @@ var ( EnvVars: []string{"ENABLE_SYMLINK_SUPPORT"}, Value: true, }, + &cli.StringFlag{ + Name: "default-mime-type", + Usage: "specifies the default mime type for files that don't have a specific mime type.", + EnvVars: []string{"DEFAULT_MIME_TYPE"}, + Value: "application/octet-stream", + }, + &cli.StringSliceFlag{ + Name: "forbidden-mime-types", + Usage: "specifies the forbidden mime types. Use this flag multiple times for multiple mime types.", + EnvVars: []string{"FORBIDDEN_MIME_TYPES"}, + }, // ########################### // ### Page Server Domains ### @@ -103,19 +114,19 @@ var ( // Default branches to fetch assets from &cli.StringSliceFlag{ Name: "pages-branch", - Usage: "define a branch to fetch assets from", + Usage: "define a branch to fetch assets from. Use this flag multiple times for multiple branches.", EnvVars: []string{"PAGES_BRANCHES"}, Value: cli.NewStringSlice("pages"), }, &cli.StringSliceFlag{ Name: "allowed-cors-domains", - Usage: "specify allowed CORS domains", + Usage: "specify allowed CORS domains. Use this flag multiple times for multiple domains.", EnvVars: []string{"ALLOWED_CORS_DOMAINS"}, }, &cli.StringSliceFlag{ Name: "blacklisted-paths", - Usage: "return an error on these url paths", + Usage: "return an error on these url paths.Use this flag multiple times for multiple paths.", EnvVars: []string{"BLACKLISTED_PATHS"}, }, diff --git a/config/assets/test_config.toml b/config/assets/test_config.toml index 92db152..eda0fc1 100644 --- a/config/assets/test_config.toml +++ b/config/assets/test_config.toml @@ -15,6 +15,8 @@ root = 'codeberg.org' token = 'XXXXX' lfsEnabled = true followSymlinks = true +defaultMimeType = "application/wasm" +forbiddenMimeTypes = [] [database] type = 'sqlite' diff --git a/config/config.go b/config/config.go index 0647869..b4321c3 100644 --- a/config/config.go +++ b/config/config.go @@ -15,7 +15,7 @@ type ServerConfig struct { HttpServerEnabled bool MainDomain string RawDomain string - DefaultBranches []string + PagesBranches []string AllowedCorsDomains []string BlacklistedPaths []string } diff --git a/config/setup.go b/config/setup.go index f0a6ee0..1cfa6ee 100644 --- a/config/setup.go +++ b/config/setup.go @@ -86,6 +86,12 @@ func mergeGiteaConfig(ctx *cli.Context, config *GiteaConfig) { if ctx.IsSet("enable-symlink-support") { config.FollowSymlinks = ctx.Bool("enable-symlink-support") } + if ctx.IsSet("default-mime-type") { + config.DefaultMimeType = ctx.String("default-mime-type") + } + if ctx.IsSet("forbidden-mime-types") { + config.ForbiddenMimeTypes = ctx.StringSlice("forbidden-mime-types") + } } func mergeDatabaseConfig(ctx *cli.Context, config *DatabaseConfig) { diff --git a/config/setup_test.go b/config/setup_test.go index 5de66a4..975071a 100644 --- a/config/setup_test.go +++ b/config/setup_test.go @@ -92,10 +92,12 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T BlacklistedPaths: []string{"original"}, }, Gitea: GiteaConfig{ - Root: "original", - Token: "original", - LFSEnabled: false, - FollowSymlinks: false, + Root: "original", + Token: "original", + LFSEnabled: false, + FollowSymlinks: false, + DefaultMimeType: "original", + ForbiddenMimeTypes: []string{"original"}, }, Database: DatabaseConfig{ Type: "original", @@ -128,10 +130,12 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T BlacklistedPaths: append([]string{"changed"}, ALWAYS_BLACKLISTED_PATHS...), }, Gitea: GiteaConfig{ - Root: "changed", - Token: "changed", - LFSEnabled: true, - FollowSymlinks: true, + Root: "changed", + Token: "changed", + LFSEnabled: true, + FollowSymlinks: true, + DefaultMimeType: "changed", + ForbiddenMimeTypes: []string{"changed"}, }, Database: DatabaseConfig{ Type: "changed", @@ -169,6 +173,8 @@ func TestMergeConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *testing.T "--gitea-api-token", "changed", "--enable-lfs-support", "--enable-symlink-support", + "--default-mime-type", "changed", + "--forbidden-mime-types", "changed", // Database "--db-type", "changed", "--db-conn", "changed", @@ -302,19 +308,23 @@ func TestMergeGiteaConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *test t, func(ctx *cli.Context) error { cfg := &GiteaConfig{ - Root: "original", - Token: "original", - LFSEnabled: false, - FollowSymlinks: false, + Root: "original", + Token: "original", + LFSEnabled: false, + FollowSymlinks: false, + DefaultMimeType: "original", + ForbiddenMimeTypes: []string{"original"}, } mergeGiteaConfig(ctx, cfg) expectedConfig := &GiteaConfig{ - Root: "changed", - Token: "changed", - LFSEnabled: true, - FollowSymlinks: true, + Root: "changed", + Token: "changed", + LFSEnabled: true, + FollowSymlinks: true, + DefaultMimeType: "changed", + ForbiddenMimeTypes: fixArrayFromCtx(ctx, "forbidden-mime-types", []string{"changed"}), } assert.Equal(t, expectedConfig, cfg) @@ -326,6 +336,8 @@ func TestMergeGiteaConfigShouldReplaceAllExistingValuesGivenAllArgsExist(t *test "--gitea-api-token", "changed", "--enable-lfs-support", "--enable-symlink-support", + "--default-mime-type", "changed", + "--forbidden-mime-types", "changed", }, ) } @@ -340,6 +352,8 @@ func TestMergeGiteaConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgEx {args: []string{"--gitea-api-token", "changed"}, callback: func(gc *GiteaConfig) { gc.Token = "changed" }}, {args: []string{"--enable-lfs-support"}, callback: func(gc *GiteaConfig) { gc.LFSEnabled = true }}, {args: []string{"--enable-symlink-support"}, callback: func(gc *GiteaConfig) { gc.FollowSymlinks = true }}, + {args: []string{"--default-mime-type", "changed"}, callback: func(gc *GiteaConfig) { gc.DefaultMimeType = "changed" }}, + {args: []string{"--forbidden-mime-types", "changed"}, callback: func(gc *GiteaConfig) { gc.ForbiddenMimeTypes = []string{"changed"} }}, } for _, pair := range testValuePairs { @@ -347,10 +361,12 @@ func TestMergeGiteaConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgEx t, func(ctx *cli.Context) error { cfg := GiteaConfig{ - Root: "original", - Token: "original", - LFSEnabled: false, - FollowSymlinks: false, + Root: "original", + Token: "original", + LFSEnabled: false, + FollowSymlinks: false, + DefaultMimeType: "original", + ForbiddenMimeTypes: []string{"original"}, } expectedConfig := cfg @@ -358,6 +374,8 @@ func TestMergeGiteaConfigShouldReplaceOnlyOneValueExistingValueGivenOnlyOneArgEx mergeGiteaConfig(ctx, &cfg) + expectedConfig.ForbiddenMimeTypes = fixArrayFromCtx(ctx, "forbidden-mime-types", expectedConfig.ForbiddenMimeTypes) + assert.Equal(t, expectedConfig, cfg) return nil diff --git a/server/handler/handler.go b/server/handler/handler.go index 6bbed5d..96788e4 100644 --- a/server/handler/handler.go +++ b/server/handler/handler.go @@ -96,7 +96,7 @@ func Handler( log.Debug().Msg("subdomain request detected") handleSubDomain(log, ctx, giteaClient, cfg.MainDomain, - cfg.DefaultBranches, + cfg.PagesBranches, trimmedHost, pathElements, canonicalDomainCache, redirectsCache) @@ -106,7 +106,7 @@ func Handler( cfg.MainDomain, trimmedHost, pathElements, - cfg.DefaultBranches[0], + cfg.PagesBranches[0], dnsLookupCache, canonicalDomainCache, redirectsCache) } } diff --git a/server/handler/handler_test.go b/server/handler/handler_test.go index daa799a..4cb859a 100644 --- a/server/handler/handler_test.go +++ b/server/handler/handler_test.go @@ -27,7 +27,7 @@ func TestHandlerPerformance(t *testing.T) { "/.well-known/acme-challenge/", }, AllowedCorsDomains: []string{"raw.codeberg.org", "fonts.codeberg.org", "design.codeberg.org"}, - DefaultBranches: []string{"pages"}, + PagesBranches: []string{"pages"}, } testHandler := Handler(serverCfg, giteaClient, cache.NewInMemoryCache(), cache.NewInMemoryCache(), cache.NewInMemoryCache()) diff --git a/server/startup.go b/server/startup.go index 286726a..9036535 100644 --- a/server/startup.go +++ b/server/startup.go @@ -55,7 +55,7 @@ func Serve(ctx *cli.Context) error { cfg.Server.MainDomain = "." + cfg.Server.MainDomain } - if len(cfg.Server.DefaultBranches) == 0 { + if len(cfg.Server.PagesBranches) == 0 { return fmt.Errorf("no default branches set (PAGES_BRANCHES)") } @@ -103,7 +103,7 @@ func Serve(ctx *cli.Context) error { cfg.Server.MainDomain, giteaClient, acmeClient, - cfg.Server.DefaultBranches[0], + cfg.Server.PagesBranches[0], keyCache, challengeCache, dnsLookupCache, canonicalDomainCache, certDB, ))