Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the setInputFiles os.read support #1244

Merged
merged 1 commit into from
Mar 14, 2024

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
@ankur22 ankur22 merged commit 83d3942 into main Mar 14, 2024
17 checks passed
@ankur22 ankur22 deleted the remove/setInputFiles-os-read-support branch March 14, 2024 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants