-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscalargo.go
More file actions
344 lines (294 loc) · 9.37 KB
/
scalargo.go
File metadata and controls
344 lines (294 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package scalargo
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"net/url"
"strings"
"github.com/bdpiprava/scalar-go/loader"
"github.com/bdpiprava/scalar-go/model"
"github.com/bdpiprava/scalar-go/sanitizer"
)
// defaultTitle when title is not specified this default is used
const defaultTitle = "API Reference"
// validateURL validates that a URL uses a safe scheme (http or https only)
// Returns an error if the URL is invalid or uses a dangerous scheme
func validateURL(rawURL, fieldName string) error {
if strings.TrimSpace(rawURL) == "" {
return nil // Empty URLs are allowed (will use defaults or be omitted)
}
// Parse the URL
parsedURL, err := url.Parse(rawURL)
if err != nil {
return fmt.Errorf("invalid %s: %w", fieldName, err)
}
// Check that scheme is http or https only
// This prevents javascript:, data:, file:, vbscript:, and other dangerous schemes
scheme := strings.ToLower(parsedURL.Scheme)
if scheme != "http" && scheme != "https" {
return fmt.Errorf("invalid %s: scheme must be http or https, got %q", fieldName, parsedURL.Scheme)
}
return nil
}
// New generates the HTML for the Scalar UI
func New(apiFilesDir string, opts ...Option) (string, error) {
return NewV2(append(opts, WithSpecDir(apiFilesDir))...)
}
// NewV2 generate the HTML for the Scalar UI
func NewV2(opts ...Option) (string, error) {
options := buildOptions(opts...)
// Validate CDN URL to prevent XSS via dangerous URL schemes
if err := validateURL(options.CDN, "CDN"); err != nil {
return "", err
}
specScript, err := options.GetSpecScript()
if err != nil {
return "", err
}
title := extractTitle(options.Configurations, defaultTitle)
return renderHTML(
title,
options.OverrideCSS,
specScript,
options.CDN,
options.CustomHeadJS,
options.CustomBodyJS,
options.RenderMode,
), nil
}
// buildOptions build Options from applying OptionFn to defaults
func buildOptions(opts ...Option) *Options {
options := &Options{
Configurations: map[string]any{
keyTheme: ThemeDefault,
keyLayout: LayoutModern,
keyShowToolbar: string(ShowToolbarNever), // Default to never (Scalar defaults to localhost)
keyMetaData: MetaData{
"title": "API Reference",
},
},
CDN: DefaultCDN,
BaseFileName: "api.yaml",
RenderMode: RenderModeJavaScriptAPI, // Default to JavaScript API (recommended)
}
for _, opt := range opts {
opt(options)
}
return options
}
// htmlTemplateDataAttr is the template for data-attribute rendering mode (legacy)
var htmlTemplateDataAttr = template.Must(template.New("scalar-data-attr").Parse(`<!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>{{.CSS}}</style>
{{.CustomHeadJS}}
</head>
<body>
{{.SpecScript}}
<script src="{{.CDN}}"></script>
{{.CustomBodyJS}}
</body>
</html>`))
// htmlTemplateJSAPI is the template for JavaScript API rendering mode (recommended)
var htmlTemplateJSAPI = template.Must(template.New("scalar-js-api").Parse(`<!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>{{.CSS}}</style>
{{.CustomHeadJS}}
</head>
<body>
<div id="app"></div>
<script src="{{.CDN}}"></script>
<script>
{{.InitScript}}
</script>
{{.CustomBodyJS}}
</body>
</html>`))
// renderHTML generates HTML from the provided options with proper escaping to prevent XSS
func renderHTML(title, cssOverride, specScript, cdn, customHeadJS, customBodyJS string, renderMode RenderMode) string {
var buf bytes.Buffer
// Sanitize CSS to remove HTML tags while preserving CSS content
sanitizedCSS := sanitizer.CSS(cssOverride)
// Select template based on render mode
var tmpl *template.Template
if renderMode == RenderModeJavaScriptAPI {
tmpl = htmlTemplateJSAPI
} else {
tmpl = htmlTemplateDataAttr
}
// Wrap custom JS in <script> tags if provided
headJS := ""
if customHeadJS != "" {
headJS = fmt.Sprintf("<script>%s</script>", customHeadJS)
}
bodyJS := ""
if customBodyJS != "" {
bodyJS = fmt.Sprintf("<script>%s</script>", customBodyJS)
}
// Execute template with proper type conversions for context-aware escaping
data := map[string]interface{}{
"Title": title, // Auto-escaped for HTML context
"CSS": template.CSS(sanitizedCSS), // #nosec G203 -- CSS sanitized, consumer's responsibility
"CDN": cdn, // Auto-escaped for attribute context
"CustomHeadJS": template.HTML(headJS), // #nosec G203 -- User-provided script injection point
"CustomBodyJS": template.HTML(bodyJS), // #nosec G203 -- User-provided script injection point
}
// Add appropriate script field based on render mode
if renderMode == RenderModeJavaScriptAPI {
data["InitScript"] = template.JS(specScript) // #nosec G203 -- JS-safe for <script> tag content
} else {
data["SpecScript"] = template.HTML(specScript) // #nosec G203 -- Already validated JSON in script tag
}
err := tmpl.Execute(&buf, data)
if err != nil {
// Template execution should never fail with our static template
// If it does, return a safe error page instead of panicking
return fmt.Sprintf("<!DOCTYPE html><html><head><title>Error</title></head><body>Template error: %s</body></html>",
template.HTMLEscapeString(err.Error()))
}
return buf.String()
}
// BuildInitScript generates JavaScript initialization code for JavaScript API mode
// Returns: Scalar.createApiReference('#api-reference', {config});
func (o *Options) BuildInitScript() (string, error) {
// Build configuration object
config := make(map[string]any)
// Copy all configurations
for k, v := range o.Configurations {
config[k] = v
}
// Handle spec source
if strings.TrimSpace(o.SpecURL) != "" {
// Validate SpecURL to prevent XSS via dangerous URL schemes
if err := validateURL(o.SpecURL, "SpecURL"); err != nil {
return "", err
}
config["url"] = o.SpecURL
} else {
// Load spec from directory or bytes
var spec *model.Spec
var err error
switch {
case o.SpecDirectory != "":
spec, err = loader.LoadFromDir(o.SpecDirectory, o.BaseFileName)
if err != nil {
return "", err
}
case o.SpecBytes != nil:
spec, err = loader.LoadFromBytes(o.SpecBytes)
if err != nil {
return "", err
}
default:
return "", fmt.Errorf("one of SpecURL, SpecDirectory or SpecBytes must be configured")
}
// Apply spec modifier if provided
if o.SpecModifier != nil {
spec = o.SpecModifier(spec)
}
// Update metadata title from spec if needed
metadata, ok := config[keyMetaData].(MetaData)
if !ok {
metadata = MetaData{}
config[keyMetaData] = metadata
}
if title, ok := metadata["title"]; !ok || title == defaultTitle {
metadata["title"] = spec.Info.Title
config[keyMetaData] = metadata
}
// Marshal spec to JSON
content, err := json.Marshal(spec)
if err != nil {
return "", err
}
config["content"] = string(content)
}
// Marshal config to JSON
configJSON, err := json.Marshal(config)
if err != nil {
return "", err
}
// Generate JavaScript initialization code
return fmt.Sprintf(
"Scalar.createApiReference('#app', %s);",
string(configJSON),
), nil
}
// GetSpecScript prepares and returns the spec script, routing to appropriate method based on RenderMode
func (o *Options) GetSpecScript() (string, error) {
if o.RenderMode == RenderModeJavaScriptAPI {
return o.BuildInitScript()
}
return o.buildDataAttributeScript()
}
// buildDataAttributeScript generates the data-attribute script (legacy mode)
func (o *Options) buildDataAttributeScript() (string, error) {
configAsBytes, err := json.Marshal(o.Configurations)
if err != nil {
return "", err
}
configJSON := strings.ReplaceAll(string(configAsBytes), `"`, `"`)
if strings.TrimSpace(o.SpecURL) != "" {
// Validate SpecURL to prevent XSS via dangerous URL schemes
if err := validateURL(o.SpecURL, "SpecURL"); err != nil {
return "", err
}
return fmt.Sprintf(
`<script id="api-reference" data-url="%s" data-configuration="%s"></script>`,
o.SpecURL,
configJSON,
), nil
}
var spec *model.Spec
switch {
case o.SpecDirectory != "":
spec, err = loader.LoadFromDir(o.SpecDirectory, o.BaseFileName)
if err != nil {
return "", err
}
case o.SpecBytes != nil:
spec, err = loader.LoadFromBytes(o.SpecBytes)
if err != nil {
return "", err
}
default:
return "", fmt.Errorf("one of SpecURL, SpecDirectory or SpecBytes must be configured")
}
if o.SpecModifier != nil {
spec = o.SpecModifier(spec)
}
metadata, ok := o.Configurations[keyMetaData].(MetaData)
if !ok {
metadata = MetaData{}
o.Configurations[keyMetaData] = metadata
}
if title, ok := metadata["title"]; !ok || title == defaultTitle {
metadata["title"] = spec.Info.Title
}
content, err := json.Marshal(spec)
if err != nil {
return "", err
}
return fmt.Sprintf(
`<script id="api-reference" type="application/json" data-configuration="%s">%s</script>`,
configJSON,
string(content),
), nil
}
// extractTitle safely extracts the title from metadata with fallback to default
func extractTitle(configurations map[string]any, fallback string) string {
if metadata, ok := configurations[keyMetaData].(MetaData); ok {
if titleVal, exists := metadata["title"]; exists {
return fmt.Sprintf("%v", titleVal)
}
}
return fallback
}