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

fix: secret function #3707

Merged
merged 5 commits into from
Jun 30, 2023
Merged

fix: secret function #3707

merged 5 commits into from
Jun 30, 2023

Conversation

kobenguyent
Copy link
Collaborator

@kobenguyent kobenguyent commented Jun 22, 2023

Motivation/Description of the PR

fix: secret function

  type redactedMyObj = { password: CodeceptJS.Secret };
  const myObj: any = secret({ password: 'pass123', pwd: 'hi' }, 'password') as redactedMyObj;
  console.log(myObj.password);

>>> Secret { _secret: 'pass123' }

  type redactedMyObj = { password: CodeceptJS.Secret };
  const myObj: any = secret({ password: 'pass123', pwd: 'hi' }, 'password') as redactedMyObj;
  console.log(myObj);
>>>> { password: Secret { _secret: 'pass123' }, pwd: 'hi' }



await I.sendPostRequest('/auth', secret({ name: 'jon', password: '123456' }, 'password'));
// console log
  Verify a successful call
    I send post request "/auth", {"name":"jon","password":"*****"}
    › [Request] {"baseURL":"https://reqres.in/auth","method":"POST","data":{"name":"jon","password":"*****"},"headers":{}}


Type of change

  • 🐛 Bug fix

Checklist:

  • Tests have been added
  • Documentation has been added (Run npm run docs)
  • Lint checking (Run npm run lint)
  • Local tests are passed (Run npm test)

Copy link

@roycollings roycollings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kobenguyent I think you can solve all of this by just changing the return value in the get handler in secret.js to return fieldsToHide.includes(prop) ? new Secret(obj[prop]) : obj[prop];:

function secretObject(obj, fieldsToHide = []) {
  const handler = {
    get(obj, prop) {
      if (prop === 'toString') {
        return function () {
          const maskedObject = deepClone(obj);
          fieldsToHide.forEach(f => maskedObject[f] = maskStr);
          return JSON.stringify(maskedObject);
        };
      }

      // Just this change ...
      return fieldsToHide.includes(prop) ? new Secret(obj[prop]) : obj[prop];
    },
  };

  return new Proxy(obj, handler);
}

This makes the extracted item a secret - in my local tests this seems to work well (my tests can run login scripts etc.. with this approach):

simple helper fn to demo:

  useMyFunc(secret_string, secret_object, secret_item_from_object) {
    console.log(">>>> types", {
      secret_string: typeof secret_string,
      secret_object: typeof secret_object,
      secret_item_from_object: typeof secret_item_from_object,
    });

    console.log(">>>> values", {
      secret_string,
      secret_item_from_object: secret_item_from_object,
    });
  }

Send the secrets to see what's logged...

    const secret_string = secret("password");
    const secret_object = secret({ a: "pass123", b: "bob" }, "a");

    I.useMyFunc(secret_string, secret_object, secret_object.a);

OUTPUT

    I use my func *****, {"a":"****","b":"bob"}, *****
>>>> types {
  secret_string: 'object',
  secret_object: 'object',
  secret_item_from_object: 'object'
}
>>>> values {
  secret_string: Secret { _secret: 'password' },
  secret_item_from_object: Secret { _secret: 'pass123' }
}

@kobenguyent
Copy link
Collaborator Author

@kobenguyent I think you can solve all of this by just changing the return value in the get handler in secret.js to return fieldsToHide.includes(prop) ? new Secret(obj[prop]) : obj[prop];:

function secretObject(obj, fieldsToHide = []) {
  const handler = {
    get(obj, prop) {
      if (prop === 'toString') {
        return function () {
          const maskedObject = deepClone(obj);
          fieldsToHide.forEach(f => maskedObject[f] = maskStr);
          return JSON.stringify(maskedObject);
        };
      }

      // Just this change ...
      return fieldsToHide.includes(prop) ? new Secret(obj[prop]) : obj[prop];
    },
  };

  return new Proxy(obj, handler);
}

This makes the extracted item a secret - in my local tests this seems to work well (my tests can run login scripts etc.. with this approach):

simple helper fn to demo:

  useMyFunc(secret_string, secret_object, secret_item_from_object) {
    console.log(">>>> types", {
      secret_string: typeof secret_string,
      secret_object: typeof secret_object,
      secret_item_from_object: typeof secret_item_from_object,
    });

    console.log(">>>> values", {
      secret_string,
      secret_item_from_object: secret_item_from_object,
    });
  }

Send the secrets to see what's logged...

    const secret_string = secret("password");
    const secret_object = secret({ a: "pass123", b: "bob" }, "a");

    I.useMyFunc(secret_string, secret_object, secret_object.a);

OUTPUT

    I use my func *****, {"a":"****","b":"bob"}, *****
>>>> types {
  secret_string: 'object',
  secret_object: 'object',
  secret_item_from_object: 'object'
}
>>>> values {
  secret_string: Secret { _secret: 'password' },
  secret_item_from_object: Secret { _secret: 'pass123' }
}

Thanks @roycollings such an elegant fix than mine.

Copy link

@roycollings roycollings left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of non-blockers :)

lib/helper/REST.js Outdated Show resolved Hide resolved
lib/step.js Outdated Show resolved Hide resolved
@kobenguyent kobenguyent merged commit a5f0a4b into 3.x Jun 30, 2023
8 checks passed
@kobenguyent kobenguyent deleted the fix-secret-function branch August 28, 2023 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants