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

Ensure no double escaping or unescaping in log file parsing #408

Closed
mountaindude opened this issue Apr 25, 2022 · 0 comments · Fixed by #409 or #407
Closed

Ensure no double escaping or unescaping in log file parsing #408

mountaindude opened this issue Apr 25, 2022 · 0 comments · Fixed by #409 or #407
Milestone

Comments

@mountaindude
Copy link
Collaborator

Example

The following example shows a pair of hand-written HTML encoding and decoding functions:

module.exports.encode = function(s) {
  return s.replace(/&/g, "&")
          .replace(/"/g, """)
          .replace(/'/g, "'");
};

module.exports.decode = function(s) {
  return s.replace(/&/g, "&")
          .replace(/"/g, "\"")
          .replace(/'/g, "'");
};

The encoding function correctly handles ampersand before the other characters. For example, the string me & "you" is encoded as me & "you", and the string " is encoded as ".

The decoding function, however, incorrectly decodes & into & before handling the other characters. So while it correctly decodes the first example above, it decodes the second example (") to " (a single double quote), which is not correct.

Instead, the decoding function should decode the ampersand last:

module.exports.encode = function(s) {
  return s.replace(/&/g, "&")
          .replace(/"/g, """)
          .replace(/'/g, "'");
};

module.exports.decode = function(s) {
  return s.replace(/"/g, "\"")
          .replace(/'/g, "'")
          .replace(/&/g, "&");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant