Skip to content

Commit 32c8c8b

Browse files
Merge pull request #2553 from keboola/jt-linter
2 parents 6a80d52 + 89778c8 commit 32c8c8b

31 files changed

Lines changed: 100 additions & 69 deletions

File tree

cmd/templates-api/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const (
3838
ENVPrefix = "TEMPLATES_"
3939
ErrorNamePrefix = "templates."
4040
ExceptionIdPrefix = "keboola-templates-"
41+
42+
//nolint:gosec // G101: This is a header name constant, not hardcoded credentials
43+
StorageTokenHeader = "X-StorageAPI-Token"
4144
)
4245

4346
func main() {
@@ -126,7 +129,7 @@ func run(ctx context.Context, cfg config.Config, _ []string) error {
126129
ExceptionIDPrefix: ExceptionIdPrefix,
127130
EnableGzip: true, // Enable gzip compression for responses
128131
MiddlewareOptions: []middleware.Option{
129-
middleware.WithRedactedHeader("X-StorageAPI-Token"),
132+
middleware.WithRedactedHeader(StorageTokenHeader),
130133
middleware.WithPropagators(propagation.TraceContext{}),
131134
middleware.WithFilter(func(req *http.Request) bool {
132135
return req.URL.Path != "/health-check"
@@ -152,7 +155,7 @@ func run(ctx context.Context, cfg config.Config, _ []string) error {
152155
middleware.ProjectScope(middleware.ProjectScopeConfig{
153156
ProjectScopeCtxKey: dependencies.ProjectRequestScopeCtxKey,
154157
PublicScopeCtxKey: dependencies.PublicRequestScopeCtxKey,
155-
TokenHeader: "X-StorageAPI-Token",
158+
TokenHeader: StorageTokenHeader,
156159
CreateProjectScope: func(ctx context.Context, publicScope any, token string) (any, error) {
157160
pubScp, ok := publicScope.(dependencies.PublicRequestScope)
158161
if !ok {

internal/pkg/project/ignore/ignore.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ func (f *File) parseIgnoredPatterns() []string {
3737
// applyIgnorePattern applies a single ignore pattern, marking the appropriate config or row as ignored.
3838
func (f *File) applyIgnorePattern(ignoreConfig string) error {
3939
// Branch pattern: "branch/<name>" — name may itself contain "/".
40-
if strings.HasPrefix(ignoreConfig, "branch/") {
41-
branchName := strings.TrimPrefix(ignoreConfig, "branch/")
40+
if after, ok := strings.CutPrefix(ignoreConfig, "branch/"); ok {
41+
branchName := after
4242
f.state.IgnoreBranch(branchName)
4343
return nil
4444
}
4545

4646
// Field-level ignore: "componentID/configID:fieldName"
47-
if colonIdx := strings.Index(ignoreConfig, ":"); colonIdx != -1 {
48-
objectPath := ignoreConfig[:colonIdx]
49-
fieldName := ignoreConfig[colonIdx+1:]
47+
if before, after, ok := strings.Cut(ignoreConfig, ":"); ok {
48+
objectPath := before
49+
fieldName := after
5050
if fieldName == "" || strings.HasPrefix(fieldName, ".") || strings.HasSuffix(fieldName, ".") {
5151
return errors.Errorf("invalid field-ignore format %q, expected componentID/configID:fieldName", ignoreConfig)
5252
}

internal/pkg/service/appsproxy/dataapps/api/rule_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestRule_Match(t *testing.T) {
129129
}
130130

131131
for _, tc := range cases {
132-
matched, err := tc.Rule.Match(httptest.NewRequest(http.MethodGet, tc.URL, nil))
132+
matched, err := tc.Rule.Match(httptest.NewRequestWithContext(t.Context(), http.MethodGet, tc.URL, nil))
133133
assert.Equal(t, tc.ExpectedMatch, matched, tc.Description)
134134
if tc.ExpectedErr == "" {
135135
require.NoError(t, err, tc.Description)

internal/pkg/service/appsproxy/dataapps/appconfig/middleware_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestAppConfigMiddleware(t *testing.T) {
4848

4949
// Send logged request
5050
rec := httptest.NewRecorder()
51-
req := httptest.NewRequest(http.MethodGet, "https://app-1.example.com/api/action", nil)
51+
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://app-1.example.com/api/action", nil)
5252
req.Header.Set("User-Agent", "my-user-agent")
5353
handler.ServeHTTP(rec, req)
5454
assert.Equal(t, http.StatusOK, rec.Code)

internal/pkg/service/appsproxy/dataapps/k8sapp/appinfo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type AppInfo struct {
5050

5151
type appStatus struct {
5252
CurrentState AppActualState `json:"currentState"`
53-
AppsProxy appsProxy `json:"appsProxy,omitempty"`
53+
AppsProxy appsProxy `json:"appsProxy"`
5454
}
5555

5656
type appsProxy struct {

internal/pkg/service/appsproxy/proxy/apphandler/authproxy/basicauth/handler.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,18 @@ func (h *Handler) ServeHTTPOrError(w http.ResponseWriter, req *http.Request) err
101101
return nil
102102
}
103103

104+
// Limit request body size to prevent memory exhaustion attacks
105+
req.Body = http.MaxBytesReader(w, req.Body, 1<<20) // 1MB limit
104106
if err := req.ParseForm(); err != nil {
105-
return err
107+
var maxBytesErr *http.MaxBytesError
108+
if errors.As(err, &maxBytesErr) {
109+
// Request body exceeded the configured limit
110+
h.pageWriter.WriteErrorPage(w, req, &h.app, http.StatusRequestEntityTooLarge, "Request body too large", "")
111+
return nil
112+
}
113+
// Any other form parsing error is treated as a bad request
114+
h.pageWriter.WriteErrorPage(w, req, &h.app, http.StatusBadRequest, "Invalid form data", "")
115+
return nil
106116
}
107117

108118
// CSRF token validation

internal/pkg/service/appsproxy/proxy/server_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,36 +86,36 @@ func TestAppProxyHandler(t *testing.T) {
8686

8787
// Get robots.txt
8888
rec := httptest.NewRecorder()
89-
req := httptest.NewRequest(http.MethodGet, "https://hub.keboola.local/robots.txt", nil)
89+
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://hub.keboola.local/robots.txt", nil)
9090
handler.ServeHTTP(rec, req)
9191
require.Equal(t, http.StatusOK, rec.Code)
9292
assert.Contains(t, rec.Body.String(), "Disallow: /")
9393

9494
// Get missing asset
9595
rec = httptest.NewRecorder()
96-
req = httptest.NewRequest(http.MethodGet, "https://hub.keboola.local/_proxy/assets/foo.bar", nil)
96+
req = httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://hub.keboola.local/_proxy/assets/foo.bar", nil)
9797
handler.ServeHTTP(rec, req)
9898
require.Equal(t, http.StatusNotFound, rec.Code)
9999

100100
// Invalid host
101101
rec = httptest.NewRecorder()
102-
req = httptest.NewRequest(http.MethodGet, "https://public-123.foo.bar.local/path", nil)
102+
req = httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://public-123.foo.bar.local/path", nil)
103103
req.Header.Set("User-Agent", "my-user-agent")
104104
handler.ServeHTTP(rec, req)
105105
require.Equal(t, http.StatusBadRequest, rec.Code)
106106
assert.Contains(t, rec.Body.String(), "Unexpected domain, missing application ID.")
107107

108108
// Send logged request
109109
rec = httptest.NewRecorder()
110-
req = httptest.NewRequest(http.MethodGet, "https://public-123.hub.keboola.local/path", nil)
110+
req = httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://public-123.hub.keboola.local/path", nil)
111111
req.Header.Set("User-Agent", "my-user-agent")
112112
handler.ServeHTTP(rec, req)
113113
require.Equal(t, http.StatusOK, rec.Code)
114114
assert.Equal(t, "Hello, client", rec.Body.String())
115115

116116
// Send ignored request
117117
rec = httptest.NewRecorder()
118-
req = httptest.NewRequest(http.MethodGet, "https://hub.keboola.local/health-check", nil)
118+
req = httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://hub.keboola.local/health-check", nil)
119119
req.Header.Set("User-Agent", "my-user-agent")
120120
handler.ServeHTTP(rec, req)
121121
require.Equal(t, http.StatusOK, rec.Code)

internal/pkg/service/cli/cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (root *RootCommand) listAliases() string {
293293
var out strings.Builder
294294
for i, cmd := range root.aliases.Keys() {
295295
tmpl := fmt.Sprintf(" %%-%ds %%s\n", maxLength)
296-
out.WriteString(fmt.Sprintf(tmpl, cmd, lines[i]))
296+
fmt.Fprintf(&out, tmpl, cmd, lines[i])
297297
}
298298
return strings.TrimRight(out.String(), "\n")
299299
}

internal/pkg/service/cli/cmd/template/create/dialog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ Do not edit lines starting with "#"!
360360
var lines strings.Builder
361361
lines.WriteString(fileHeader)
362362
for _, c := range d.configs {
363-
lines.WriteString(fmt.Sprintf("## Config \"%s\" %s:%s\n%s\n\n", c.Name, c.ComponentID, c.ID, idByKey[c.Key().String()]))
363+
fmt.Fprintf(&lines, "## Config \"%s\" %s:%s\n%s\n\n", c.Name, c.ComponentID, c.ID, idByKey[c.Key().String()])
364364
for _, r := range c.Rows {
365-
lines.WriteString(fmt.Sprintf("### Row \"%s\" %s:%s:%s\n%s\n\n", r.Name, r.ComponentID, r.ConfigID, r.ID, idByKey[r.Key().String()]))
365+
fmt.Fprintf(&lines, "### Row \"%s\" %s:%s:%s\n%s\n\n", r.Name, r.ComponentID, r.ConfigID, r.ID, idByKey[r.Key().String()])
366366
}
367367
}
368368

internal/pkg/service/cli/dialog/allowed_branches.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (d *branchesDialog) unique(items model.AllowedBranches) model.AllowedBranch
194194
m.Set(string(item), true)
195195
}
196196

197-
unique := model.AllowedBranches{}
197+
unique := make(model.AllowedBranches, 0, len(m.Keys()))
198198
for _, item := range m.Keys() {
199199
unique = append(unique, model.AllowedBranch(item))
200200
}

0 commit comments

Comments
 (0)