Skip to content

Commit 7a08609

Browse files
authored
fix: potentially unsafe quoting (#232)
Signed-off-by: Ales Verbic <[email protected]>
1 parent 6683d4c commit 7a08609

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

api/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func (a *APIv1) Engine() *gin.Engine {
8585
// @contact.url https://blinklabs.io
8686
// @contact.email [email protected]
8787

88-
// @license.name Apache 2.0
89-
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
88+
// @license.name Apache 2.0
89+
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
9090
func (a *APIv1) Start() error {
9191
address := fmt.Sprintf("%s:%d", a.Host, a.Port)
9292
// Use buffered channel to not block goroutine

output/push/qr_generator.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7-
"text/template"
87

98
"github.com/gin-gonic/gin"
109
)
@@ -15,9 +14,10 @@ type QRValue struct {
1514

1615
func generateQRPage(apiEndpoint string) gin.HandlerFunc {
1716
return func(c *gin.Context) {
18-
apiEndpoint := c.Request.Host + apiEndpoint
17+
fullApiEndpoint := c.Request.Host + apiEndpoint
18+
// Create QRValue and marshal to JSON
1919
qrValue, err := json.Marshal(QRValue{
20-
ApiEndpoint: apiEndpoint,
20+
ApiEndpoint: fullApiEndpoint,
2121
})
2222
if err != nil {
2323
c.JSON(http.StatusInternalServerError, gin.H{
@@ -26,40 +26,39 @@ func generateQRPage(apiEndpoint string) gin.HandlerFunc {
2626
return
2727
}
2828

29-
qrValueEscaped := template.JSEscapeString(string(qrValue))
30-
29+
// Generate HTML content
3130
htmlContent := fmt.Sprintf(`
3231
<!DOCTYPE html>
3332
<html lang="en">
3433
<head>
35-
<meta charset="UTF-8">
36-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
37-
<title>QR Code</title>
38-
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
39-
<script src="https://cdn.jsdelivr.net/npm/qrious@latest/dist/qrious.min.js"></script>
34+
<meta charset="UTF-8">
35+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
36+
<title>QR Code</title>
37+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
38+
<script src="https://cdn.jsdelivr.net/npm/qrious@latest/dist/qrious.min.js"></script>
4039
</head>
4140
<body class="bg-gray-100 h-screen flex items-center justify-center">
4241
<!-- QR Code Container -->
4342
<div class="bg-white p-8 rounded-lg shadow-md text-center">
44-
<p class="text-xl mb-4">Scan QR code with Adder Mobile to connect to the Adder Server on <span class="font-semibold">%s</span></p>
45-
<canvas id="qrCanvas" class="mx-auto"></canvas>
43+
<p class="text-xl mb-4">Scan QR code with Adder Mobile to connect to the Adder Server on <span class="font-semibold">%s</span></p>
44+
<canvas id="qrCanvas" class="mx-auto"></canvas>
4645
</div>
4746
4847
<!-- Generate QR Code using JavaScript -->
4948
<script>
50-
window.onload = function() {
51-
const canvas = document.getElementById('qrCanvas');
52-
const qrValue = "%s";
53-
const qr = new QRious({
54-
element: canvas,
55-
value: qrValue,
56-
size: 250
57-
});
58-
}
49+
window.onload = function() {
50+
const canvas = document.getElementById('qrCanvas');
51+
const qrValue = %s; // Directly embed the JSON object
52+
const qr = new QRious({
53+
element: canvas,
54+
value: JSON.stringify(qrValue),
55+
size: 250
56+
});
57+
}
5958
</script>
6059
</body>
6160
</html>
62-
`, apiEndpoint, qrValueEscaped)
61+
`, fullApiEndpoint, qrValue)
6362

6463
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(htmlContent))
6564
}

0 commit comments

Comments
 (0)