Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 83d3942

Browse files
committed
Remove the setInputFiles os.read support
We do not want k6 to be able to read anything off of a filesystem that it shouldn't be reading. The support for reading the file directly from the local filesystem in setInputFiles has been removed.
1 parent fe717a8 commit 83d3942

File tree

3 files changed

+1
-120
lines changed

3 files changed

+1
-120
lines changed

common/element_handle.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ package common
22

33
import (
44
"context"
5-
"encoding/base64"
65
"errors"
76
"fmt"
87
"math"
9-
"mime"
10-
"os"
11-
"path/filepath"
128
"reflect"
139
"strings"
1410
"time"
@@ -1273,22 +1269,6 @@ func (h *ElementHandle) SetInputFiles(files goja.Value, opts goja.Value) error {
12731269
return nil
12741270
}
12751271

1276-
func (h *ElementHandle) resolveFiles(payload []*File) error {
1277-
for _, file := range payload {
1278-
if strings.TrimSpace(file.Path) != "" {
1279-
buffer, err := os.ReadFile(file.Path)
1280-
if err != nil {
1281-
return fmt.Errorf("reading file: %w", err)
1282-
}
1283-
file.Buffer = base64.StdEncoding.EncodeToString(buffer)
1284-
file.Name = filepath.Base(file.Path)
1285-
file.Mimetype = mime.TypeByExtension(filepath.Ext(file.Path))
1286-
}
1287-
}
1288-
1289-
return nil
1290-
}
1291-
12921272
func (h *ElementHandle) setInputFiles(apiCtx context.Context, payload []*File) error {
12931273
fn := `
12941274
(node, injected, payload) => {
@@ -1299,10 +1279,6 @@ func (h *ElementHandle) setInputFiles(apiCtx context.Context, payload []*File) e
12991279
forceCallable: true,
13001280
returnByValue: true,
13011281
}
1302-
err := h.resolveFiles(payload)
1303-
if err != nil {
1304-
return err
1305-
}
13061282
result, err := h.evalWithScript(apiCtx, evalOpts, fn, payload)
13071283
if err != nil {
13081284
return err

common/element_handle_options.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ type ElementHandleHoverOptions struct {
7878

7979
// File is the descriptor of a single file.
8080
type File struct {
81-
Path string `json:"-"`
8281
Name string `json:"name"`
8382
Mimetype string `json:"mimeType"`
8483
Buffer string `json:"buffer"`
@@ -204,7 +203,7 @@ func NewElementHandleSetInputFilesOptions(defaultTimeout time.Duration) *Element
204203
}
205204
}
206205

207-
// addFile to the struct. Input value can be a path, or a file descriptor object.
206+
// addFile to the struct. Input value can only be a file descriptor object.
208207
func (f *Files) addFile(ctx context.Context, file goja.Value) error {
209208
if !gojaValueExists(file) {
210209
return nil
@@ -218,10 +217,6 @@ func (f *Files) addFile(ctx context.Context, file goja.Value) error {
218217
return fmt.Errorf("parsing file descriptor: %w", err)
219218
}
220219
f.Payload = append(f.Payload, &parsedFile)
221-
case reflect.String: // file path
222-
if v, ok := file.Export().(string); ok {
223-
f.Payload = append(f.Payload, &File{Path: v})
224-
}
225220
default:
226221
return fmt.Errorf("invalid parameter type : %s", fileType.Kind().String())
227222
}

tests/setinputfiles_test.go

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package tests
22

33
import (
4-
"os"
5-
"path/filepath"
64
"testing"
75

86
"github.com/dop251/goja"
@@ -92,76 +90,6 @@ func TestSetInputFiles(t *testing.T) {
9290
assert.Equal(t, "text/xml", getFilePropFn(1, propType))
9391
},
9492
},
95-
{
96-
name: "set_one_file_with_path",
97-
setup: func(tb *testBrowser) (goja.Value, func()) {
98-
tempFile, err := os.CreateTemp("", "*.json")
99-
assert.NoError(t, err)
100-
n, err := tempFile.Write([]byte("0123456789"))
101-
assert.Equal(t, 10, n)
102-
assert.NoError(t, err)
103-
cleanupFunc := func() {
104-
err := os.Remove(tempFile.Name())
105-
assert.NoError(t, err)
106-
}
107-
return tb.toGojaValue(tempFile.Name()), cleanupFunc
108-
},
109-
tests: []testFn{defaultTestPage, defaultTestElementHandle},
110-
check: func(t *testing.T, getFileCountFn func() interface{}, getFilePropFn indexedFn, err error) {
111-
t.Helper()
112-
assert.NoError(t, err)
113-
// check if input has 1 file
114-
assert.Equal(t, float64(1), getFileCountFn())
115-
// check added file is correct
116-
filename, ok := getFilePropFn(0, propName).(string)
117-
assert.True(t, ok)
118-
assert.Equal(t, ".json", filepath.Ext(filename))
119-
assert.Equal(t, float64(10), getFilePropFn(0, propSize))
120-
assert.Equal(t, "application/json", getFilePropFn(0, propType))
121-
},
122-
},
123-
{
124-
name: "set_two_files_with_path",
125-
setup: func(tb *testBrowser) (goja.Value, func()) {
126-
tempFile1, err := os.CreateTemp("", "*.json")
127-
assert.NoError(t, err)
128-
n, err := tempFile1.Write([]byte("0123456789"))
129-
assert.Equal(t, 10, n)
130-
assert.NoError(t, err)
131-
132-
tempFile2, err := os.CreateTemp("", "*.xml")
133-
assert.NoError(t, err)
134-
n, err = tempFile2.Write([]byte("012345678901234"))
135-
assert.Equal(t, 15, n)
136-
assert.NoError(t, err)
137-
cleanupFunc := func() {
138-
err := os.Remove(tempFile1.Name())
139-
assert.NoError(t, err)
140-
err = os.Remove(tempFile2.Name())
141-
assert.NoError(t, err)
142-
}
143-
144-
return tb.toGojaValue([]string{tempFile1.Name(), tempFile2.Name()}), cleanupFunc
145-
},
146-
tests: []testFn{defaultTestPage, defaultTestElementHandle},
147-
check: func(t *testing.T, getFileCountFn func() interface{}, getFilePropFn indexedFn, err error) {
148-
t.Helper()
149-
assert.NoError(t, err)
150-
// check if input has 2 files
151-
assert.Equal(t, float64(2), getFileCountFn())
152-
// check added files are correct
153-
filename1, ok := getFilePropFn(0, propName).(string)
154-
assert.True(t, ok)
155-
assert.Equal(t, ".json", filepath.Ext(filename1))
156-
assert.Equal(t, float64(10), getFilePropFn(0, propSize))
157-
assert.Equal(t, "application/json", getFilePropFn(0, propType))
158-
filename2, ok := getFilePropFn(1, propName).(string)
159-
assert.True(t, ok)
160-
assert.Equal(t, ".xml", filepath.Ext(filename2))
161-
assert.Equal(t, float64(15), getFilePropFn(1, propSize))
162-
assert.Equal(t, "text/xml; charset=utf-8", getFilePropFn(1, propType))
163-
},
164-
},
16593
{
16694
name: "set_nil",
16795
setup: func(tb *testBrowser) (goja.Value, func()) {
@@ -188,24 +116,6 @@ func TestSetInputFiles(t *testing.T) {
188116
assert.Equal(t, float64(0), getFileCountFn())
189117
},
190118
},
191-
{
192-
name: "set_file_not_exists",
193-
setup: func(tb *testBrowser) (goja.Value, func()) {
194-
tempFile, err := os.CreateTemp("", "*.json")
195-
assert.NoError(t, err)
196-
err = os.Remove(tempFile.Name())
197-
assert.NoError(t, err)
198-
return tb.toGojaValue(tempFile.Name()), nil
199-
},
200-
tests: []testFn{defaultTestPage, defaultTestElementHandle},
201-
check: func(t *testing.T, getFileCountFn func() interface{}, getFilePropFn indexedFn, err error) {
202-
t.Helper()
203-
assert.ErrorContains(t, err, "reading file:")
204-
assert.ErrorContains(t, err, "setting input files")
205-
// check if input has 0 file
206-
assert.Equal(t, float64(0), getFileCountFn())
207-
},
208-
},
209119
{
210120
name: "test_injected_script_notinput",
211121
setup: func(tb *testBrowser) (goja.Value, func()) {

0 commit comments

Comments
 (0)