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

Conversation

ankur22
Copy link
Collaborator

@ankur22 ankur22 commented Mar 13, 2024

What?

Removing the support for setInputFiles to read off of the filesystem with the os package.

Why?

We do not want k6 to be able to read anything off of a filesystem that it shouldn't be reading. Instead anyone wishing to work with files on the filesystem should look to use the built in experimental fs module. The fs module abstracts away the detail of how it loads the required file for when working in a remote environment. It also reduces the risk of OOMs. A simple example can be found below:

Example working with `setInputFiles` and the fs module
import { browser } from 'k6/experimental/browser';
import encoding from 'k6/encoding';
import { open } from 'k6/experimental/fs';

export const options = {
  scenarios: {
    ui: {
      executor: 'shared-iterations',
      iterations: 1,
      vus: 1,
      options: {
        browser: {
          type: 'chromium'
        },
      },
    },
  },
}

let file;
(async function () {
  file = await open('/abs/path/to/file.txt');
})();

export default async function () {
  const page = browser.newPage();

  await page.goto(url)

  const buffer = await readAll(file);

  page.setInputFiles('input[id="upload"]', { name: 'file.txt', mimetype: 'text/plain', buffer: encoding.b64encode(buffer) })
  
  const submitButton = page.locator('input[type="submit"]')
  await Promise.all([page.waitForNavigation(), submitButton.click()])

  page.close();
}

async function readAll(file) {
  const fileInfo = await file.stat();
  const buffer = new Uint8Array(fileInfo.size);

  const bytesRead = await file.read(buffer);
  if (bytesRead !== fileInfo.size) {
    throw new Error(
      'unexpected number of bytes read; expected ' +
      fileInfo.size +
      ' but got ' +
      bytesRead +
      ' bytes'
    );
  }

  return buffer;
}

Checklist

  • I have performed a self-review of my code
  • I have added tests for my changes
  • I have commented on my code, particularly in hard-to-understand areas

Related PR(s)/Issue(s)

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.
@ankur22 ankur22 force-pushed the remove/setInputFiles-os-read-support branch from c2d6a04 to ee3b263 Compare March 14, 2024 15:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants