Add redirect url validation

This commit is contained in:
2025-10-03 09:32:04 +08:00
parent 4f564da7b3
commit 9bf5149cff
2 changed files with 20 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ services:
- DB_USER=nysoure - DB_USER=nysoure
- DB_PASSWORD=nysoure_password - DB_PASSWORD=nysoure_password
- DB_NAME=nysoure - DB_NAME=nysoure
- BANNED_REDIRECT_DOMAINS=example.com,example.org
restart: unless-stopped restart: unless-stopped
db: db:

View File

@@ -5,8 +5,10 @@ import (
"context" "context"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt"
"io" "io"
"net/http" "net/http"
"net/url"
"nysoure/server/config" "nysoure/server/config"
"nysoure/server/dao" "nysoure/server/dao"
"nysoure/server/model" "nysoure/server/model"
@@ -15,6 +17,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"sync/atomic" "sync/atomic"
"time" "time"
@@ -28,6 +31,8 @@ const (
MinUnrequireVerifyFileSize = 10 * 1024 * 1024 // 10MB MinUnrequireVerifyFileSize = 10 * 1024 * 1024 // 10MB
) )
var bannedRedirectDomains []string
func getUploadingSize() int64 { func getUploadingSize() int64 {
return dao.GetStatistic("uploading_size") return dao.GetStatistic("uploading_size")
} }
@@ -46,6 +51,10 @@ func getTempDir() (string, error) {
} }
func init() { func init() {
domains := os.Getenv("BANNED_REDIRECT_DOMAINS")
if domains != "" {
bannedRedirectDomains = strings.Split(domains, ",")
}
go func() { go func() {
// Wait for 1 minute to ensure the database is ready // Wait for 1 minute to ensure the database is ready
time.Sleep(time.Minute) time.Sleep(time.Minute)
@@ -301,6 +310,16 @@ func CancelUploadingFile(uid uint, fid uint) error {
} }
func CreateRedirectFile(uid uint, filename string, description string, resourceID uint, redirectUrl string) (*model.FileView, error) { func CreateRedirectFile(uid uint, filename string, description string, resourceID uint, redirectUrl string) (*model.FileView, error) {
u, err := url.Parse(redirectUrl)
if err != nil {
return nil, model.NewRequestError("URL is not valid")
}
for _, domain := range bannedRedirectDomains {
if u.Host == domain {
return nil, model.NewRequestError(fmt.Sprintf("Domain '%s' is not allowed", domain))
}
}
canUpload, err := checkUserCanUpload(uid) canUpload, err := checkUserCanUpload(uid)
if err != nil { if err != nil {
log.Error("failed to check user permission: ", err) log.Error("failed to check user permission: ", err)