Skip to content

bodyParser.text() renders Parse unable to process requests (outputs "Unauthorized") #4681

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

Closed
iop280 opened this issue Mar 27, 2018 · 9 comments

Comments

@iop280
Copy link

iop280 commented Mar 27, 2018

Issue Description

I'm attempting to log request payloads for monitoring reasons and it seems that everytime I use the bodyParser.text() in my express server (I'm logging Parse JS SDK calls, and it appears that only the bodyParser.text places the payload in req.body), the parse-server is not able to use the rest of the request.

When bodyParser.text() is in place, all requests respond with:
ParseError {code: undefined, message: "unauthorized"}

Do you know why this is the case?

app.use(bodyParser.text());
app.use('/parse/classes/:className/:id?', function (req, res, next) {
  var params = req.params;
  var className = params.className;
  var objectId = params.id;
  var originalURL = req.originalUrl;

  if (objectId && !seenParseModifyClassRules.className) {
    nr.addNamingRule('^\/parse\/classes\/' + className + '\/.+', 'Create|Modify/' + className);
    console.log(req.body);
    seenParseModifyClassRules[className] = true;
  } else if (!objectId && !seenParseGeneralClassRules.className) {
    nr.addNamingRule('^\/parse\/classes\/' + className + '$', 'Query|General/' + className);
    console.log(req.body);
    seenParseGeneralClassRules[className] = true;
  }
  next();
});

var mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, api);

Steps to reproduce

On the JS side, I merely use a call with the following code:

const userQuery = new Parse.Query('_User');
userQuery.contains('firstName', 'Tom');
userQuery.find().then((asdf) => {
   console.log(asdf);
 }, (error) => {
   console.log(error);
});

Expected Results

I expect the Parse-Server to console log the request payload, and the response to return the Parse Query.

Actual Outcome

Parse-Server logs the request payload, but Parse Query is responded with "unauthorized"

Environment Setup

  • Server
    • parse-server version (Be specific! Don't say 'latest'.) : ^2.7.4
    • Operating System: Windows 10
    • Localhost or remote server? (AWS, Heroku, Azure, Digital Ocean, etc): Local
@iop280 iop280 changed the title bodyParser.text() renders Parse unable to process requests bodyParser.text() renders Parse unable to process requests (outputs "Unauthorized") Mar 27, 2018
@flovilmart
Copy link
Contributor

Your body parser is conflicting with the JSON body parser that parse-server internally use.

@iop280
Copy link
Author

iop280 commented Mar 27, 2018

@flovilmart - what solution would you suggest to allow the requests to be parsed by both the Express server as well as Parse-Server?

@Koosmann
Copy link

@flovilmart - We are also looking for a solution for this, we just need some mechanism to inspect the request before allowing to proceed to parse-server (note: we'd be using this as a workaround for a hook like this: #4524)

@flovilmart
Copy link
Contributor

Feel free to open a pull request if you find a solution, but it’s unlikely we’ll look for one given the very narrow use case you’re describing.

@Koosmann
Copy link

@flovilmart Sounds good! I'm wondering if parse-server could default to using req.body if it exists, so it doesn't try to re-read the stream. I could look into submitting this, but I imagine there might be wide implications to a change like this too.

@flovilmart
Copy link
Contributor

As you mention, there may be other implications. I’m not sure we’d want to slow the processing for everyone else just for your case. Also, you could always restore the contents of the body after inspecting it.

@Koosmann
Copy link

That makes sense, and thanks for the input @flovilmart! I dug into this a little more and realized it's probably more in the scope of the bodyParser module versus parse-server itself and gained some insight here (https://github.com/expressjs/body-parser/issues/207) where it says there's a flag that can be set req._body = true to tell instances of bodyParser down the line that req.body is already available.

Lastly, I think I may have diverged a bit from the original author's topic, sorry for not opening a new thread!

@flovilmart
Copy link
Contributor

It’s fine, it complements the original concern with a potential way to workaround!

@iop280
Copy link
Author

iop280 commented Apr 2, 2018

@Koosmann thanks for digging into this - super helpful, thanks!

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

No branches or pull requests

3 participants