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

feat: adding new scenario for the c++ check for PII responses sample plugin #136

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

pweiber
Copy link
Contributor

@pweiber pweiber commented Dec 5, 2024

As per request, this PR includes:

  • Remove the specific header to check for the PII data
  • Add a mask for a 10 digit numeric code (In addition to the already CC check)

Tests were adjusted accordingly and also added some extra scenarios

Signed-off-by: pweiber <periclesweiber@ciandt.com>
Signed-off-by: pweiber <periclesweiber@ciandt.com>
@pweiber pweiber requested a review from a team as a code owner December 5, 2024 21:09
Copy link
Contributor

@mpwarres mpwarres left a comment

Choose a reason for hiding this comment

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

A few minor comments. Thanks!

// Note that for illustrative purposes, this example is kept simple and does not
// handle the case of credit card numbers that are split across multiple
// onResponseBody() calls.
// handle cases where PII is split across multiple onResponseBody() calls.
class MyHttpContext : public Context {
public:
explicit MyHttpContext(uint32_t id, RootContext* root)
: Context(id, root), root_(static_cast<MyRootContext*>(root)) {}

Copy link
Contributor

Choose a reason for hiding this comment

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

One current limitation is that this plugin won't strip PII as intended if the server response is compressed, since in that case the bytes returned by getBufferBytes() will be compressed rather than plaintext data.

The simplest workaround is to disallow the server from using response compression. This can be done by setting "Accept-Encoding: identity" in request headers. So I'd suggest defining an onRequestHeaders() that calls replaceRequestHeader("accept-encoding", "identity"); along with a comment explaining that this is to force the response to not be compressed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the comment explaining about it, let me know if the text is fine

plugins/samples/check_pii/plugin.cc Outdated Show resolved Hide resolved
plugins/samples/check_pii/plugin.cc Outdated Show resolved Hide resolved
for (auto& p : pairs) {
std::string header_value = std::string(p.second); // mutable copy
if (maskPII(header_value)) {
replaceResponseHeader(p.first, header_value);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit of an uncommon case, but if a header appears multiple times in the request, this logic may end up collapsing the multiple occurrences into a single entry. For example, suppose the request has both of the following headers:

"credit-card: 0123-4567-8901-234"
"credit-card: 5678-9012-3456-7890"

Then after this processing, the response would only have the single header "credit-card: XXXX-XXXX-XXXX-7890".

You can avoid this problem by instead collecting the modified values in a HeaderStringPairs vector, and setting them all at once, e.g.:

const HeaderStringPairs orig_headers = getResponseHeaderPairs();
HeaderStringPairs new_headers;
bool changed = false;
for (auto [key, value] : orig_headers) {
  changed = changed || maskPII(value);
  new_headers.emplace_back(std::make_pair(std::move(key), std::move(value));
}
if (changed) {
  setResponseHeaderPairs(new_headers);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, that code snippet has a bug. The line that updates changed should be:

changed = maskPII(value) || changed;

Otherwise, boolean short-circuiting will skip the call to maskPII() when changed is already true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried using

const HeaderStringPairs orig_headers = getResponseHeaderPairs();

but I was getting

error: no viable conversion from 'WasmDataPtr' (aka 'unique_ptr<WasmData>') to 'const HeaderStringPairs' (aka 'const vector<pair<basic_string<char>, basic_string<char>>>')

so I tried a workaround using the original approach, I believe it should work as well, let me know what you think

@pweiber pweiber requested a review from mpwarres December 18, 2024 14:43
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