-1

I am creating a Firefox extension which posts some data to a database. I made all parts in a modular fashion and am now combining everything piece by piece.

As such I know that my code to POST data to the database works.

Now here is the part that stumps me : When I then add this code to my firefox extension I get the following error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3003/timed_shot_create. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 400.

Now ofcourse CORS was nothing new and to be expected when dealing with Cross Origin Resource Sharing, it is even in the name.

But the reason why I am here is because this pertains only to the response of the POST request. The request itself is fine and allowed with the following piece of config in the server:

app.use(
  cors({
    //todo change to proper origin when live
    origin: "moz-extension://d07f1e99-96a0-4934-8ff4-1ce222c06d0d",
    method: ["GET", "POST"],
  })
);

Which was later changed to:

app.use(
  cors({
    origin: "*",
    method: ["GET", "POST"],
  })
);

And then simplified even more to:

app.use(cors())

This is in Nodejs btw using cors middleware.

But none of this seems to work when it is used inside a firefox extension, as a local client page works as intended but as soon as I add this to a firefox extension I get a CORS error specifically pertaining to the reponse message.

The client side post (in the background script of the extension) is:

async function postTimedShot(post_options) {
  const response = await fetch(post_endpoint, post_options);
  //console.log(response);
  const json_response = await response.json();
  console.log(json_response);
}

let post_options = {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify(response_data),
};

postTimedShot(post_options);

And the api looks like this:

app.post("/timed_shot_create", (req, res) => {
  console.log("Received POST request");
  const data = req.body;
  console.log(data);

  const timeStamp = data.time_stamp;
  //TODO add screenshot and Description text maybe??
  //const lastName = data.last_name

  const queryString =
    "INSERT INTO " + timed_shots_database + " (time_stamp) VALUES (?)";
  getConnection().query(queryString, [timeStamp], (err, results, fields) => {
    if (err) {
      console.log("Failed to insert new user: " + err);
      res.sendStatus(500);
      return;
    }

    //Todo change this message when adding more data in body
    //res.header("Access-Control-Allow-Origin", "moz-extension://d07f1e99-96a0-4934-8ff4-1ce222c06d0d");
    res.json({
      status: "Success!!!",
      time_stamp: timeStamp,
    });

    console.log("Inserted a new user with id: ", results.insertId);
  });
});

Furthermore, this extension is only for personal use and will work with a local server under my complete control so complications due to security or cloud usage that people want to mention are appreciated but not necessary (I think, I am a bit of novice).

I will be happy to clarify anything that is unclear, or change this post if necessary, but I think it is a unique question as far as I could see on SO. Additionally if I need to provide more of the codebase I will.

I will also update this post if I find out more about this problem.

Thank you for reading :3.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
  • What is the actual response body for the failing request? The one with the 400 status – Phil Aug 15 '22 at 23:20
  • Well, I am sadly unable to print it due to CORS, but it in the client version outside of the extension it is: res.json({ status: "Success!!!", time_stamp: timeStamp, }); Or as received in the browser console: Object { status: "Success!!!", time_stamp: "Testing here some more 6:09" } ​ status: "Success!!!" ​ time_stamp: "Testing here some more 6:09" ​ : Object { … } – Pinkwin Dev Aug 15 '22 at 23:24
  • Can you not inspect the extension's requests in the dev-tools _Network_ panel? – Phil Aug 15 '22 at 23:25
  • The requests when running in the extension or when its running in an HTML page, or both? – Pinkwin Dev Aug 15 '22 at 23:29
  • My apologies Phil, I have to pick this up tomorrow again. My plan was to post this right before sleep because last time I had a question it took 14 hours or so before I got a reponse. Thank you for the quick reponse however :3 – Pinkwin Dev Aug 15 '22 at 23:41

1 Answers1

1

After reading about this post https://stackoverflow.com/a/53025865/5055963 on SO I found out that it had to do with the permissions in the manifest of the extension.

Adding this line: "://.localhost/*".

Solved the issue for me.