2024-05-15 11:10:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
2025-02-26 15:25:26 +00:00
|
|
|
"fmt"
|
2024-05-15 11:10:49 +00:00
|
|
|
)
|
|
|
|
|
|
2025-02-27 10:13:52 +00:00
|
|
|
// As the frontend environment is read at build time, we need to reprocess the values
|
|
|
|
|
// at container runtime.
|
|
|
|
|
// This app will search and replace the values set at build time from this build environment: Dockerfiles/frontend_build.env
|
|
|
|
|
// with values taken from the container environment.
|
|
|
|
|
|
2024-05-15 11:10:49 +00:00
|
|
|
func main() {
|
2025-02-27 10:13:52 +00:00
|
|
|
// Define the build env variables to be replaced at container run time
|
|
|
|
|
// url config variables are expected to be a valid URL in the container environment
|
2025-02-27 16:29:37 +00:00
|
|
|
url_config := []string{"VITE_OBP_API_HOST", "VITE_OBP_API_MANAGER_HOST", "VITE_OBP_API_PORTAL_HOST", "VITE_OBP_LOGO_URL"}
|
2025-02-27 10:13:52 +00:00
|
|
|
// DANGERZONE: The following strings will be replaced by container environment variables without any checking of whatever!!!
|
2025-03-06 11:34:51 +00:00
|
|
|
config := []string{"VITE_OBP_API_VERSION", "VITE_OBP_LINKS_COLOR", "VITE_OBP_HEADER_LINKS_COLOR", "VITE_OBP_HEADER_LINKS_HOVER_COLOR", "VITE_OBP_HEADER_LINKS_BACKGROUND_COLOR", "VITE_OBP_API_DEFAULT_RESOURCE_DOC_VERSION", "VITE_CHATBOT_ENABLED", "VITE_CHATBOT_URL"}
|
2024-05-15 11:10:49 +00:00
|
|
|
configMap := make(map[string]string)
|
|
|
|
|
|
|
|
|
|
for _, key := range config {
|
2025-02-26 15:21:46 +00:00
|
|
|
value := os.Getenv(key)
|
|
|
|
|
if value == "" {
|
|
|
|
|
fmt.Printf("Skipping: Environment variable %s is not set\n", key)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
configMap[key] = value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, key := range url_config {
|
2024-05-15 11:10:49 +00:00
|
|
|
rawURL := os.Getenv(key)
|
|
|
|
|
if rawURL == "" {
|
2025-02-26 15:21:46 +00:00
|
|
|
fmt.Printf("Skipping: Environment variable %s is not set\n", key)
|
2024-05-15 11:10:49 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
cleanURL := checkURL(rawURL)
|
|
|
|
|
configMap[key] = cleanURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dir := "/opt/app-root/src/assets"
|
|
|
|
|
pattern := "index-.*\\.js$"
|
|
|
|
|
|
|
|
|
|
re, err := regexp.Compile(pattern)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
files, err := os.ReadDir(dir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
if re.MatchString(file.Name()) {
|
|
|
|
|
filePath := filepath.Join(dir, file.Name())
|
|
|
|
|
content, err := os.ReadFile(filePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
modifiedContent := string(content)
|
|
|
|
|
for old, new := range configMap {
|
|
|
|
|
modifiedContent = strings.Replace(modifiedContent, old, new, -1)
|
|
|
|
|
}
|
|
|
|
|
err = os.WriteFile(filePath, []byte(modifiedContent), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkURL(rawURL string) string {
|
|
|
|
|
|
|
|
|
|
parsedURL, err := url.Parse(rawURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validURL := regexp.MustCompile(`^https?:\/\/[^\s/$.?#].[^\s]*$`)
|
|
|
|
|
if !validURL.MatchString(rawURL) {
|
|
|
|
|
log.Fatal("Invalid URL or potential code injection detected")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cleanURL := &url.URL{
|
|
|
|
|
Scheme: parsedURL.Scheme,
|
|
|
|
|
Host: parsedURL.Host,
|
|
|
|
|
Path: parsedURL.Path,
|
|
|
|
|
}
|
|
|
|
|
return cleanURL.String()
|
|
|
|
|
}
|