Skip to content

Commit beef618

Browse files
sfairchildnywilken
authored andcommitted
Prevent enormous files from being submitted (#725)
closes #717
1 parent 5185642 commit beef618

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

cmd/submit.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
161161
if err != nil {
162162
return err
163163
}
164+
const maxFileSize int64 = 65535
165+
if info.Size() >= maxFileSize {
166+
msg :=`
167+
168+
The submitted file is larger than the max allowed file size of %d bytes. Please reduce the size of the file and try again.
169+
170+
`
171+
return fmt.Errorf(msg, maxFileSize)
172+
}
164173
if info.Size() == 0 {
165174

166175
msg := `

cmd/submit_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,52 @@ func TestSubmitWithEmptyFile(t *testing.T) {
295295
assert.Equal(t, "This is file 2.", submittedFiles["file-2.txt"])
296296
}
297297

298+
func TestSubmitWithEnormousFile(t *testing.T) {
299+
oldOut := Out
300+
oldErr := Err
301+
Out = ioutil.Discard
302+
Err = ioutil.Discard
303+
defer func() {
304+
Out = oldOut
305+
Err = oldErr
306+
}()
307+
308+
// The fake endpoint will populate this when it receives the call from the command.
309+
submittedFiles := map[string]string{}
310+
ts := fakeSubmitServer(t, submittedFiles)
311+
defer ts.Close()
312+
313+
tmpDir, err := ioutil.TempDir("", "enormous-file")
314+
defer os.RemoveAll(tmpDir)
315+
assert.NoError(t, err)
316+
317+
dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise")
318+
os.MkdirAll(dir, os.FileMode(0755))
319+
320+
writeFakeSolution(t, dir, "bogus-track", "bogus-exercise")
321+
322+
v := viper.New()
323+
v.Set("token", "abc123")
324+
v.Set("workspace", tmpDir)
325+
v.Set("apibaseurl", ts.URL)
326+
327+
cfg := config.Config{
328+
Persister: config.InMemoryPersister{},
329+
UserViperConfig: v,
330+
}
331+
332+
file := filepath.Join(dir, "file.txt")
333+
err = ioutil.WriteFile(file, make([]byte, 65535), os.FileMode(0755))
334+
if err != nil {
335+
t.Fatal(err)
336+
}
337+
338+
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), []string{file})
339+
340+
assert.Error(t, err)
341+
assert.Regexp(t, "Please reduce the size of the file and try again.", err.Error())
342+
}
343+
298344
func TestSubmitFilesForTeamExercise(t *testing.T) {
299345
oldOut := Out
300346
oldErr := Err

0 commit comments

Comments
 (0)