Node version: 10.19.0
Sails version _(sails)_: 1.2.3
Hi,
I have a controller which serves an image. I am trying to get this route to work
/content/26/image.png
It responses 404 but work with
/content/26/image.png/
I got it to work with a custom inline route ( fn: async function (req, res)) but I want access control for this route.
Many thanks in advance and kindest regards
@dailez Thanks for posting! We'll take a look as soon as possible.
In the mean time, there are a few ways you can help speed things along:
Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.
For help with questions about Sails, click here.
Hey @dailez , You could put the image in the assets folder. Sails automatically generates an asset route for the image. Read more about it here
Thanks for your message but I need to add a parameter to the route. I can't use the route as it is.
Please can you share a screenshot of your route address inconfig/route.js
@dailez You probably want to use the skipAssets options in your routes file (I think set to false)..
Please find below an extract of the route.js:
'GET /temp-files/:fileName': {
skipAssets: false,
fn: async function (req, res) {
console.log(req.user); // undefined
//...
}
},
I'm currently implementing the functionality inline. The problem is that req.user is undefined but I need this information because the route is not /temp-files/:fileName but /temp-files/userId/:fileName. I can not add the userId to the route, I want to use it from the logged-in user. I tried to do it with a dedicated file (also actions2) but it always fails with not authorized.
As I said above, not inline gives an error:
'GET /temp-files/:fileName': {action: 'download-temp-file', skipAssets: false},
http://localhost:1337/temp-files/image.jpg response with 401 (Unauthorized)
Okay, from what I understand, GET /temp-files/:fileName can only be accessed by an authorized user right?
GET /api/v1/temp-files/:filename': { action: 'download-temp-file', skipAssets: false }download-temp-file action by running sails generate action download-temp-file on your terminalfriendlyName: 'Download temp file',
description: '',
inputs: {
filename: {
required: true,
type: 'string'
}
},
exits: {
},
fn: async function (inputs) {
// You can access the filename in your action with inputs.filename
// All done.
return {
}
}
GET /api/v1/temp-files/:filename, you need to create a policyapi/policies/isLoggedIn.jsmodule.exports = async function (req, res, proceed) {
const authorizationHeader = req.headers.authorization;
if (!authorizationHeader) {
return res.unauthorized();
}
try {
const requestToken = authorizationHeader.split('Bearer').pop().trim();
const payload = await sails.helpers.decodeAuthToken(requestToken); //Extracting the data from the token
// Get the user
const user = await Users.findOne({
id: payload.id,
deactivated: false,
});
if (!user) {
return res.unauthorized();
}
// Add the user to the request object/dictionary
req.user = user;
return proceed();
} catch (error) {
sails.log.error(error);
switch (error.name) {
case 'JsonWebTokenError':
return res.unauthorized();
case 'TokenExpiredError':
return res.unauthorized();
default:
return res.serverError();
}
}
};
Then you need to modify your config/policies.js file to add the isLoggedIn policy to the download-temp-file action.
You can do it like so:
module.exports.policies = {
'download-temp-file': 'isLoggedIn'
}
download-temp-file action using this.req.userHope this helps.
Thanks a lot
Thanks for the detailed explanation @georgeben!
Most helpful comment
Thanks for the detailed explanation @georgeben!