Skip to content

Ensure options as passed to plugins is not modified #2582

Open

Description

@eahefnawy

Within our core plugins, we frequently change the this.option object to pass data around. Instead we should do that in another variable and keep this.option read only & immutable to avoid conflicts between plugins.


Further clarifications:

Passed options is CLI parameters, as resolved on sls.processedInput.options. They are passed as second argument to initialized plugin.

Activity

  1. added this to the 1.2 milestone on Nov 1, 2016
  2. modified the milestones: 1.3, 1.2 on Nov 2, 2016
  3. removed this from the 1.3 milestone on Nov 24, 2016
  4. changed the title [-]Make sure this.options object is read only[/-] [+]Ensure options as passed to plugins is not modified[/+] on Aug 20, 2020
  5. medikoo commented on Aug 20, 2020

    @medikoo
    Contributor

    Freezing this object may not be great idea. As there are rare (corner case) scenarios where patching/hacking this object could be helpful, and also it's not a convention in JS to freeze such input objects (If we would to freeze that, then technically by convention we should freeze most of the internal structure, as plugins should also not interfere with it).

    Still, it's definitely a valid concern, that this object should not be modified, either by internal plugins or external plugins, and indeed we repeat a quite bad pattern in internal plugins where we first assign options to this.options and then extend this.options with some useful data to be used across methods. That's quite error-prone, and collisions are likely


    Proposed solution: I'd address this issue by picking up on all places where this.options.x = .. assignment happen, and ensuring that this.options is a copy of options that was passed via arguments.

  6. added
    bug/design
    on Aug 20, 2020
  7. sleepwithcoffee commented on Mar 10, 2023

    @sleepwithcoffee
    Contributor

    hi @medikoo
    I think to avoid modifying the original options object, we need to make a clone whenever assigning it to this.options

    # this can both clone and avoid this.options to be null
    this.options = Object.assign({}, options);
    
  8. medikoo commented on Mar 10, 2023

    @medikoo
    Contributor

    @duytrandev exactly, that's the way to go 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions