Added HTML sanitization for translate server

This commit is contained in:
2025-04-19 10:43:42 -04:00
parent 17878106d4
commit 485a3efd8f
3 changed files with 17 additions and 0 deletions

View File

@@ -16,6 +16,8 @@ import (
translate "cloud.google.com/go/translate/apiv3"
"cloud.google.com/go/translate/apiv3/translatepb"
"github.com/microcosm-cc/bluemonday"
)
const project_id string = "india-translate-testing-452100"
@@ -72,6 +74,7 @@ var lang_codes []string = []string{
}
var db *sqlx.DB
var p *bluemonday.Policy
func cleanup() {
db.Close()
@@ -149,6 +152,7 @@ func translateTextHelper(projectID string, sourceLang string, targetLang string,
func handler(w http.ResponseWriter, r *http.Request) {
queries := r.URL.Query()
toTranslate := queries["query"][0]
toTranslate = p.Sanitize(toTranslate)
if ok, translation := getCachedTranslation(toTranslate); ok {
translationJson, _ := json.Marshal(translation)
@@ -182,6 +186,10 @@ func main() {
panic(err)
}
defer db.Close()
// HTML sanitization
p = bluemonday.UGCPolicy()
// Catch signal
sigs := make(chan os.Signal, 1)
signal.Notify(sigs)