Open platform to collect and prioritize feedback
Add `tenant.AllowedSchemes` advanced configuration item (#1332)
набa978d2ca6cb9
diff --git a/.example.env b/.example.envindex 6b906b20e..737c02333 100644--- a/.example.env+++ b/.example.env@@ -2,6 +2,7 @@ BASE_URL=http://localhost:3000GO_ENV=developmentDATABASE_URL=postgres://fider:fider_pw@localhost:5555/fider?sslmode=disableJWT_SECRET=hsjl]W;&ZcHxT&FK;s%bgIQF:#ch=~#Al4:5]N;7V<qPZ3e9lT4'%;go;LIkc%k+# ALLOW_ALLOWED_SCHEMES=falseLOG_LEVEL=DEBUGLOG_CONSOLE=truediff --git a/app/actions/tenant.go b/app/actions/tenant.goindex 40c041d9f..fcbd1df1a 100644--- a/app/actions/tenant.go+++ b/app/actions/tenant.go@@ -177,7 +177,8 @@ func (action *UpdateTenantSettings) Validate(ctx context.Context, user *entity.U// UpdateTenantAdvancedSettings is the input model used to update tenant advanced settingstype UpdateTenantAdvancedSettings struct {- CustomCSS string `json:"customCSS"`+ CustomCSS string `json:"customCSS"`+ AllowedSchemes string `json:"allowedSchemes"`}// IsAuthorized returns true if current user is authorized to perform this actiondiff --git a/app/handlers/admin.go b/app/handlers/admin.goindex 82ed74cd2..376bebf47 100644--- a/app/handlers/admin.go+++ b/app/handlers/admin.go@@ -32,6 +32,7 @@ func AdvancedSettingsPage() web.HandlerFunc {Title: "Advanced · Site Settings",Data: web.Map{"customCSS": c.Tenant().CustomCSS,+ "allowedSchemes": c.Tenant().AllowedSchemes,},})}@@ -84,6 +85,7 @@ func UpdateAdvancedSettings() web.HandlerFunc {if err := bus.Dispatch(c, &cmd.UpdateTenantAdvancedSettings{CustomCSS: action.CustomCSS,+ AllowedSchemes: action.AllowedSchemes,}); err != nil {return c.Failure(err)}diff --git a/app/models/cmd/tenant.go b/app/models/cmd/tenant.goindex a95a5fef3..482514280 100644--- a/app/models/cmd/tenant.go+++ b/app/models/cmd/tenant.go@@ -35,7 +35,8 @@ type UpdateTenantSettings struct {}type UpdateTenantAdvancedSettings struct {- CustomCSS string+ CustomCSS string+ AllowedSchemes string}type ActivateTenant struct {diff --git a/app/models/entity/tenant.go b/app/models/entity/tenant.goindex 58b56b148..98c92e2a6 100644--- a/app/models/entity/tenant.go+++ b/app/models/entity/tenant.go@@ -15,6 +15,7 @@ type Tenant struct {IsPrivate bool `json:"isPrivate"`LogoBlobKey string `json:"logoBlobKey"`CustomCSS string `json:"-"`+ AllowedSchemes string `json:"allowedSchemes"`IsEmailAuthAllowed bool `json:"isEmailAuthAllowed"`IsFeedEnabled bool `json:"isFeedEnabled"`PreventIndexing bool `json:"preventIndexing"`diff --git a/app/pkg/env/env.go b/app/pkg/env/env.goindex 3d3bc1aa0..8c95ecd5f 100644--- a/app/pkg/env/env.go+++ b/app/pkg/env/env.go@@ -54,6 +54,7 @@ type config struct {Locale string `env:"LOCALE,default=en"`JWTSecret string `env:"JWT_SECRET,required"`PostCreationWithTagsEnabled bool `env:"POST_CREATION_WITH_TAGS_ENABLED,default=false"`+ AllowAllowedSchemes bool `env:"ALLOW_ALLOWED_SCHEMES,default=true"`Paddle struct {IsSandbox bool `env:"PADDLE_SANDBOX,default=false"`VendorID string `env:"PADDLE_VENDOR_ID"`diff --git a/app/pkg/web/renderer.go b/app/pkg/web/renderer.goindex 4edbe4ff9..c4c8bba8f 100644--- a/app/pkg/web/renderer.go+++ b/app/pkg/web/renderer.go@@ -204,18 +204,19 @@ func (r *Renderer) Render(w io.Writer, statusCode int, props Props, ctx *Contextpublic["tenant"] = tenantpublic["props"] = props.Datapublic["settings"] = &Map{- "mode": env.Config.HostMode,- "locale": locale,- "localeDirection": localeDirection,- "environment": env.Config.Environment,- "googleAnalytics": env.Config.GoogleAnalytics,- "domain": env.MultiTenantDomain(),- "hasLegal": env.HasLegal(),- "isBillingEnabled": env.IsBillingEnabled(),- "baseURL": ctx.BaseURL(),- "assetsURL": AssetsURL(ctx, ""),- "oauth": oauthProviders.Result,- "postWithTags": env.Config.PostCreationWithTagsEnabled,+ "mode": env.Config.HostMode,+ "locale": locale,+ "localeDirection": localeDirection,+ "environment": env.Config.Environment,+ "googleAnalytics": env.Config.GoogleAnalytics,+ "domain": env.MultiTenantDomain(),+ "hasLegal": env.HasLegal(),+ "isBillingEnabled": env.IsBillingEnabled(),+ "baseURL": ctx.BaseURL(),+ "assetsURL": AssetsURL(ctx, ""),+ "oauth": oauthProviders.Result,+ "postWithTags": env.Config.PostCreationWithTagsEnabled,+ "allowAllowedSchemes": env.Config.AllowAllowedSchemes,}if ctx.IsAuthenticated() {diff --git a/app/pkg/web/testdata/basic.html b/app/pkg/web/testdata/basic.htmlindex 00dab5f09..4f7cd1fd1 100755--- a/app/pkg/web/testdata/basic.html+++ b/app/pkg/web/testdata/basic.html@@ -45,7 +45,7 @@<script id="server-data" type="application/json">- {"contextID":"CONTEXT_ID","page":"","props":{},"sessionID":"","settings":{"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"Fider"}+ {"contextID":"CONTEXT_ID","page":"","props":{},"sessionID":"","settings":{"allowAllowedSchemes":true,"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"Fider"}</script>diff --git a/app/pkg/web/testdata/canonical.html b/app/pkg/web/testdata/canonical.htmlindex fd2d20b80..f6334b1e2 100755--- a/app/pkg/web/testdata/canonical.html+++ b/app/pkg/web/testdata/canonical.html@@ -47,7 +47,7 @@<script id="server-data" type="application/json">- {"contextID":"CONTEXT_ID","page":"","props":{},"sessionID":"","settings":{"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"Fider"}+ {"contextID":"CONTEXT_ID","page":"","props":{},"sessionID":"","settings":{"allowAllowedSchemes":true,"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"Fider"}</script>diff --git a/app/pkg/web/testdata/chunk.html b/app/pkg/web/testdata/chunk.htmlindex 510685805..dfdf17bc3 100644--- a/app/pkg/web/testdata/chunk.html+++ b/app/pkg/web/testdata/chunk.html@@ -45,7 +45,7 @@<script id="server-data" type="application/json">- {"contextID":"CONTEXT_ID","page":"Test.page","props":{},"sessionID":"","settings":{"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"Fider"}+ {"contextID":"CONTEXT_ID","page":"Test.page","props":{},"sessionID":"","settings":{"allowAllowedSchemes":true,"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"Fider"}</script>diff --git a/app/pkg/web/testdata/home.html b/app/pkg/web/testdata/home.htmlindex 9a7a61308..3297e64d6 100755--- a/app/pkg/web/testdata/home.html+++ b/app/pkg/web/testdata/home.html@@ -45,7 +45,7 @@<script id="server-data" type="application/json">- {"contextID":"CONTEXT_ID","description":"My Page Description","page":"","props":{"countPerStatus":{},"posts":[],"tags":[]},"sessionID":"","settings":{"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"My Page Title · Fider"}+ {"contextID":"CONTEXT_ID","description":"My Page Description","page":"","props":{"countPerStatus":{},"posts":[],"tags":[]},"sessionID":"","settings":{"allowAllowedSchemes":true,"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"My Page Title · Fider"}</script>diff --git a/app/pkg/web/testdata/home_ssr.html b/app/pkg/web/testdata/home_ssr.htmlindex 6138dee0a..32c2823bf 100755--- a/app/pkg/web/testdata/home_ssr.html+++ b/app/pkg/web/testdata/home_ssr.html@@ -45,7 +45,7 @@<script id="server-data" type="application/json">- {"contextID":"CONTEXT_ID","description":"My Page Description","page":"Test.page","props":{"countPerStatus":{},"posts":[],"tags":[]},"sessionID":"","settings":{"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":{"id":0,"name":"","subdomain":"","invitation":"","welcomeMessage":"","cname":"","status":0,"locale":"en","isPrivate":false,"logoBlobKey":"","isEmailAuthAllowed":false,"isFeedEnabled":false,"preventIndexing":false},"title":"My Page Title · "}+ {"contextID":"CONTEXT_ID","description":"My Page Description","page":"Test.page","props":{"countPerStatus":{},"posts":[],"tags":[]},"sessionID":"","settings":{"allowAllowedSchemes":true,"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":{"id":0,"name":"","subdomain":"","invitation":"","welcomeMessage":"","cname":"","status":0,"locale":"en","isPrivate":false,"logoBlobKey":"","allowedSchemes":"","isEmailAuthAllowed":false,"isFeedEnabled":false,"preventIndexing":false},"title":"My Page Title · "}</script>diff --git a/app/pkg/web/testdata/oauth.html b/app/pkg/web/testdata/oauth.htmlindex 321f304e5..7019812c2 100755--- a/app/pkg/web/testdata/oauth.html+++ b/app/pkg/web/testdata/oauth.html@@ -45,7 +45,7 @@<script id="server-data" type="application/json">- {"contextID":"CONTEXT_ID","page":"","props":{},"sessionID":"","settings":{"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[{"provider":"google","displayName":"Google","clientID":"1234","url":"https://demo.test.fider.io:3000/oauth/google","callbackURL":"https://demo.test.fider.io:3000/oauth/google/callback","logoBlobKey":"google.png","isCustomProvider":false,"isEnabled":true}],"postWithTags":true},"tenant":null,"title":"Fider"}+ {"contextID":"CONTEXT_ID","page":"","props":{},"sessionID":"","settings":{"allowAllowedSchemes":true,"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[{"provider":"google","displayName":"Google","clientID":"1234","url":"https://demo.test.fider.io:3000/oauth/google","callbackURL":"https://demo.test.fider.io:3000/oauth/google/callback","logoBlobKey":"google.png","isCustomProvider":false,"isEnabled":true}],"postWithTags":true},"tenant":null,"title":"Fider"}</script>diff --git a/app/pkg/web/testdata/tenant.html b/app/pkg/web/testdata/tenant.htmlindex fee3ae671..3792b4999 100755--- a/app/pkg/web/testdata/tenant.html+++ b/app/pkg/web/testdata/tenant.html@@ -45,7 +45,7 @@<script id="server-data" type="application/json">- {"contextID":"CONTEXT_ID","page":"","props":{},"sessionID":"","settings":{"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":{"id":0,"name":"Game of Thrones","subdomain":"","invitation":"","welcomeMessage":"","cname":"","status":0,"locale":"","isPrivate":false,"logoBlobKey":"","isEmailAuthAllowed":false,"isFeedEnabled":false,"preventIndexing":false},"title":"Game of Thrones"}+ {"contextID":"CONTEXT_ID","page":"","props":{},"sessionID":"","settings":{"allowAllowedSchemes":true,"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":{"id":0,"name":"Game of Thrones","subdomain":"","invitation":"","welcomeMessage":"","cname":"","status":0,"locale":"","isPrivate":false,"logoBlobKey":"","allowedSchemes":"","isEmailAuthAllowed":false,"isFeedEnabled":false,"preventIndexing":false},"title":"Game of Thrones"}</script>diff --git a/app/pkg/web/testdata/user.html b/app/pkg/web/testdata/user.htmlindex 1d248d090..2a7455e5d 100755--- a/app/pkg/web/testdata/user.html+++ b/app/pkg/web/testdata/user.html@@ -45,7 +45,7 @@<script id="server-data" type="application/json">- {"contextID":"CONTEXT_ID","description":"My Page Description","page":"","props":{},"sessionID":"","settings":{"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"My Page Title · Fider","user":{"avatarBlobKey":"","avatarType":"gravatar","avatarURL":"https://demo.test.fider.io:3000/static/avatars/gravatar/5/Jon%20Snow","email":"[email protected]","id":5,"isAdministrator":true,"isCollaborator":true,"name":"Jon Snow","role":"administrator","status":"active"}}+ {"contextID":"CONTEXT_ID","description":"My Page Description","page":"","props":{},"sessionID":"","settings":{"allowAllowedSchemes":true,"assetsURL":"https://demo.test.fider.io:3000","baseURL":"https://demo.test.fider.io:3000","domain":".test.fider.io","environment":"test","googleAnalytics":"","hasLegal":true,"isBillingEnabled":false,"locale":"en","localeDirection":"ltr","mode":"multi","oauth":[],"postWithTags":true},"tenant":null,"title":"My Page Title · Fider","user":{"avatarBlobKey":"","avatarType":"gravatar","avatarURL":"https://demo.test.fider.io:3000/static/avatars/gravatar/5/Jon%20Snow","email":"[email protected]","id":5,"isAdministrator":true,"isCollaborator":true,"name":"Jon Snow","role":"administrator","status":"active"}}</script>diff --git a/app/services/sqlstore/postgres/tenant.go b/app/services/sqlstore/postgres/tenant.goindex d2e9ac5e4..63e7c10cd 100644--- a/app/services/sqlstore/postgres/tenant.go+++ b/app/services/sqlstore/postgres/tenant.go@@ -28,6 +28,7 @@ type dbTenant struct {IsPrivate bool `db:"is_private"`LogoBlobKey string `db:"logo_bkey"`CustomCSS string `db:"custom_css"`+ AllowedSchemes string `db:"allowed_schemes"`IsEmailAuthAllowed bool `db:"is_email_auth_allowed"`IsFeedEnabled bool `db:"is_feed_enabled"`PreventIndexing bool `db:"prevent_indexing"`@@ -50,6 +51,7 @@ func (t *dbTenant) toModel() *entity.Tenant {IsPrivate: t.IsPrivate,LogoBlobKey: t.LogoBlobKey,CustomCSS: t.CustomCSS,+ AllowedSchemes: t.AllowedSchemes,IsEmailAuthAllowed: t.IsEmailAuthAllowed,IsFeedEnabled: t.IsFeedEnabled,PreventIndexing: t.PreventIndexing,@@ -168,13 +170,19 @@ func updateTenantSettings(ctx context.Context, c *cmd.UpdateTenantSettings) errofunc updateTenantAdvancedSettings(ctx context.Context, c *cmd.UpdateTenantAdvancedSettings) error {return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {- query := "UPDATE tenants SET custom_css = $1 WHERE id = $2"- _, err := trx.Execute(query, c.CustomCSS, tenant.ID)+ AllowedSchemes := c.AllowedSchemes+ if !env.Config.AllowAllowedSchemes {+ AllowedSchemes = ""+ }++ query := "UPDATE tenants SET custom_css = $1, allowed_schemes = $2 WHERE id = $3"+ _, err := trx.Execute(query, c.CustomCSS, AllowedSchemes, tenant.ID)if err != nil {return errors.Wrap(err, "failed update tenant advanced settings")}tenant.CustomCSS = c.CustomCSS+ tenant.AllowedSchemes = AllowedSchemesreturn nil})}@@ -238,8 +246,8 @@ func createTenant(ctx context.Context, c *cmd.CreateTenant) error {var id interr := trx.Get(&id,- `INSERT INTO tenants (name, subdomain, created_at, cname, invitation, welcome_message, status, is_private, custom_css, logo_bkey, locale, is_email_auth_allowed, is_feed_enabled, prevent_indexing)- VALUES ($1, $2, $3, '', '', '', $4, false, '', '', $5, true, true, true)+ `INSERT INTO tenants (name, subdomain, created_at, cname, invitation, welcome_message, status, is_private, custom_css, logo_bkey, locale, is_email_auth_allowed, is_feed_enabled, prevent_indexing)+ VALUES ($1, $2, $3, '', '', '', $4, false, '', '', $5, true, true, true)RETURNING id`, c.Name, c.Subdomain, now, c.Status, env.Config.Locale)if err != nil {return err@@ -248,7 +256,7 @@ func createTenant(ctx context.Context, c *cmd.CreateTenant) error {if env.IsBillingEnabled() {trialEndsAt := time.Now().AddDate(0, 0, 15) // 15 days_, err := trx.Execute(- `INSERT INTO tenants_billing (tenant_id, trial_ends_at, status, paddle_subscription_id, paddle_plan_id)+ `INSERT INTO tenants_billing (tenant_id, trial_ends_at, status, paddle_subscription_id, paddle_plan_id)VALUES ($1, $2, $3, '', '')`, id, trialEndsAt, enum.BillingTrial)if err != nil {return err@@ -267,7 +275,7 @@ func getFirstTenant(ctx context.Context, q *query.GetFirstTenant) error {tenant := dbTenant{}err := trx.Get(&tenant, `- SELECT id, name, subdomain, cname, invitation, locale, welcome_message, status, is_private, logo_bkey, custom_css, is_email_auth_allowed, is_feed_enabled, prevent_indexing+ SELECT id, name, subdomain, cname, invitation, locale, welcome_message, status, is_private, logo_bkey, custom_css, allowed_schemes, is_email_auth_allowed, is_feed_enabled, prevent_indexingFROM tenantsORDER BY id LIMIT 1`)@@ -286,9 +294,9 @@ func getTenantByDomain(ctx context.Context, q *query.GetTenantByDomain) error {tenant := dbTenant{}err := trx.Get(&tenant, `- SELECT id, name, subdomain, cname, invitation, locale, welcome_message, status, is_private, logo_bkey, custom_css, is_email_auth_allowed, is_feed_enabled, prevent_indexing+ SELECT id, name, subdomain, cname, invitation, locale, welcome_message, status, is_private, logo_bkey, custom_css, allowed_schemes, is_email_auth_allowed, is_feed_enabled, prevent_indexingFROM tenants t- WHERE subdomain = $1 OR subdomain = $2 OR cname = $3+ WHERE subdomain = $1 OR subdomain = $2 OR cname = $3ORDER BY cname DESC`, env.Subdomain(q.Domain), q.Domain, q.Domain)if err != nil {diff --git a/app/services/sqlstore/postgres/tenant_test.go b/app/services/sqlstore/postgres/tenant_test.goindex db71668c1..b1f887b05 100644--- a/app/services/sqlstore/postgres/tenant_test.go+++ b/app/services/sqlstore/postgres/tenant_test.go@@ -223,6 +223,7 @@ func TestTenantStorage_AdvancedSettings(t *testing.T) {err := bus.Dispatch(demoTenantCtx, &cmd.UpdateTenantAdvancedSettings{CustomCSS: ".primary { color: red; }",+ AllowedSchemes: "^monero:[48]\n^bitcoin:(1|3|bc1)",})Expect(err).IsNil()@@ -230,6 +231,7 @@ func TestTenantStorage_AdvancedSettings(t *testing.T) {err = bus.Dispatch(demoTenantCtx, getByDomain)Expect(err).IsNil()Expect(getByDomain.Result.CustomCSS).Equals(".primary { color: red; }")+ Expect(getByDomain.Result.AllowedSchemes).Equals("^monero:[48]\n^bitcoin:(1|3|bc1)")}func TestTenantStorage_SaveFindSet_VerificationKey(t *testing.T) {diff --git a/migrations/202507161200_add_allowed_schemes.sql b/migrations/202507161200_add_allowed_schemes.sqlnew file mode 100644index 000000000..192568de0--- /dev/null+++ b/migrations/202507161200_add_allowed_schemes.sql@@ -0,0 +1 @@+ALTER TABLE tenants ADD allowed_schemes TEXT NOT NULL DEFAULT '';diff --git a/public/hooks/use-fider.ts b/public/hooks/use-fider.tsindex 31e57a270..a6e74cb3c 100644--- a/public/hooks/use-fider.ts+++ b/public/hooks/use-fider.ts@@ -2,3 +2,5 @@ import { useContext } from "react"import { FiderContext } from "@fider/services"export const useFider = () => useContext(FiderContext)+// useContext() doesn't work in tests+export const fiderAllowedSchemes = { get: () => useFider().session.tenant.allowedSchemes }diff --git a/public/models/identity.ts b/public/models/identity.tsindex acb686cd1..4e724f9e2 100644--- a/public/models/identity.ts+++ b/public/models/identity.ts@@ -9,6 +9,7 @@ export interface Tenant {status: TenantStatusisPrivate: booleanlogoBlobKey: string+ allowedSchemes: stringisEmailAuthAllowed: booleanisFeedEnabled: boolean}diff --git a/public/models/settings.ts b/public/models/settings.tsindex 183d708d9..98c129440 100644--- a/public/models/settings.ts+++ b/public/models/settings.ts@@ -21,6 +21,7 @@ export interface SystemSettings {assetsURL: stringoauth: OAuthProviderOption[]postWithTags: boolean+ allowAllowedSchemes: boolean}export interface UserSettings {diff --git a/public/pages/Administration/pages/AdvancedSettings.page.tsx b/public/pages/Administration/pages/AdvancedSettings.page.tsxindex e8e0a6a18..c7c893ec6 100644--- a/public/pages/Administration/pages/AdvancedSettings.page.tsx+++ b/public/pages/Administration/pages/AdvancedSettings.page.tsx@@ -6,10 +6,12 @@ import { AdminBasePage } from "../components/AdminBasePage"interface AdvancedSettingsPageProps {customCSS: string+ allowedSchemes: string}interface AdvancedSettingsPageState {customCSS: string+ allowedSchemes: stringerror?: Failure}@@ -24,6 +26,7 @@ export default class AdvancedSettingsPage extends AdminBasePage<AdvancedSettingsthis.state = {customCSS: this.props.customCSS,+ allowedSchemes: this.props.allowedSchemes,}}@@ -31,8 +34,12 @@ export default class AdvancedSettingsPage extends AdminBasePage<AdvancedSettingsthis.setState({ customCSS })}+ private setAllowedSchemes = (allowedSchemes: string): void => {+ this.setState({ allowedSchemes })+ }+private handleSave = async (): Promise<void> => {- const result = await actions.updateTenantAdvancedSettings(this.state.customCSS)+ const result = await actions.updateTenantAdvancedSettings(this.state.customCSS, this.state.allowedSchemes)if (result.ok) {location.reload()} else {@@ -70,6 +77,26 @@ export default class AdvancedSettingsPage extends AdminBasePage<AdvancedSettings</ul></TextArea>+ {Fider.settings.allowAllowedSchemes && (+ <TextArea+ field="allowedSchemes"+ label="Allowed URL Schemes"+ disabled={!Fider.session.user.isAdministrator}+ minRows={3}+ value={this.state.allowedSchemes}+ onChange={this.setAllowedSchemes}+ >+ <p className="text-muted">+ By default, uncommon URL schemes are forbidden in links.+ <br />+ If you want to allow linking monero or bitcoin addresses, you should add <code>^monero:[48]</code> or <code>^bitcoin:(1|3|bc1)</code> here.+ </p>+ <p className="text-muted">+ These are regular expressions, one per line, matched against the link address. <code>^javascript</code> is always rejected.+ </p>+ </TextArea>+ )}+{Fider.session.user.isAdministrator && (<div className="field"><Button variant="primary" onClick={this.handleSave}>diff --git a/public/services/actions/tenant.ts b/public/services/actions/tenant.tsindex 19a827466..7fb4979fc 100644--- a/public/services/actions/tenant.ts+++ b/public/services/actions/tenant.ts@@ -36,8 +36,8 @@ export const updateTenantSettings = async (request: UpdateTenantSettingsRequest)return await http.post("/_api/admin/settings/general", request)}-export const updateTenantAdvancedSettings = async (customCSS: string): Promise<Result> => {- return await http.post("/_api/admin/settings/advanced", { customCSS })+export const updateTenantAdvancedSettings = async (customCSS: string, allowedSchemes: string): Promise<Result> => {+ return await http.post("/_api/admin/settings/advanced", { customCSS, allowedSchemes })}export const updateTenantPrivacy = async (request: PrivacySettingsPageState): Promise<Result> => {diff --git a/public/services/markdown.spec.ts b/public/services/markdown.spec.tsindex f75d5a2a9..ecd27f279 100644--- a/public/services/markdown.spec.ts+++ b/public/services/markdown.spec.ts@@ -1,4 +1,6 @@import * as markdown from "./markdown"+import { fiderAllowedSchemes } from "@fider/hooks"+fiderAllowedSchemes.get = () => "^monero:[48]\n^bitcoin:(1|3|bc1)"const testCases = [{@@ -51,6 +53,13 @@ How are you?`,expectedFull: "<p>-123<br>-456<br>-789</p>",expectedPlainText: "-123 -456 -789",},+ {+ input:+ "[monero](monero:83zJ2jMbBoxJkhtpaRLk6fQVrvfmGbd8gYUL7FSdLxU91JSpiWXoLUtAMGqmfvfq3qRS5gJUvMY7oLFSx71wxhKRGG6ypMt) [bitcoin](bitcoin:1CgLs6CxXMAY4Pj4edQq5vyaFoP9NdqVKH) [litecoin](litecoin:ltc1qg0elpp0hxguwlsapl68gvklt5ngemj8k8lu0f5)",+ expectedFull:+ '<p><a href="monero:83zJ2jMbBoxJkhtpaRLk6fQVrvfmGbd8gYUL7FSdLxU91JSpiWXoLUtAMGqmfvfq3qRS5gJUvMY7oLFSx71wxhKRGG6ypMt" target="_blank" rel="noopener nofollow" class="text-link">monero</a> <a href="bitcoin:1CgLs6CxXMAY4Pj4edQq5vyaFoP9NdqVKH" target="_blank" rel="noopener nofollow" class="text-link">bitcoin</a> <a target="_blank" rel="noopener nofollow" class="text-link">litecoin</a></p>',+ expectedPlainText: "monero bitcoin litecoin",+ },]testCases.forEach((x) => {diff --git a/public/services/markdown.ts b/public/services/markdown.tsindex b2e023bb1..32817fccd 100644--- a/public/services/markdown.ts+++ b/public/services/markdown.ts@@ -1,5 +1,6 @@import { marked } from "marked"import DOMPurify from "dompurify"+import { fiderAllowedSchemes } from "@fider/hooks"marked.setOptions({headerIds: false,@@ -16,6 +17,21 @@ if (DOMPurify.isSupported) {},ADD_ATTR: ["target"],})++ let allow: RegExp[] | undefined+ DOMPurify.addHook("uponSanitizeAttribute", (currentNode, hookEvent) => {+ if (allow === undefined)+ allow = fiderAllowedSchemes+ .get()+ .split("\n")+ .filter((s) => s)+ .map((s) => new RegExp(s, "i"))++ if (allow && hookEvent.attrName === "href") {+ const href = currentNode.getAttribute("href")+ if (href !== null && !/^javascript/i.test(href)) hookEvent.forceKeepAttr = allow.some((r) => r.test(href))+ }+ })}const link = (href: string, title: string, text: string) => {