Added HTML sanitization for translate server
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user