[FIX] Enable CORS for Restivus#8671
Conversation
d65806d to
c9b8283
Compare
(cherry picked from commit cf7d37f)
c9b8283 to
8180f8b
Compare
|
@graywolf336 I'm not really knowledgable about CORS and what the option in Restivus is. |
|
@mrsimpson only issue with this is that if you change it after the server has started, it won't be reflected until you restart the server. 🤔 so we need to figure out how to dynamically have this setting applied, I will take a look. |
|
@graywolf336 ah, Nö server side reactivity... how about getting the setting to a variable and set this variable additionally in a setting.load()-callback? |
| auth: getUserAuth() | ||
| RocketChat.settings.onload('API_Enable_CORS', (key, value) => { | ||
| enableCors = value; | ||
| createApi(); |
There was a problem hiding this comment.
@graywolf336 that's what I meant writing y'days comment.
Is that the proper way to do it?
Note to myself: I really should get an IDE on my mobile...
There was a problem hiding this comment.
You're extremely close! Honestly, I would leave the function the way it is and just change the RocketChat.settings.onload handler to be RocketChat.settings.get('API_Enabled_CORS', () => {}) as that will be called everytime the setting is changed. I have no clue what onload will do 🤔
There was a problem hiding this comment.
So, after doing some review it seems that get with a callback makes a call to onload and also immediately runs the callback..someone feel free to correct me if I'm misunderstanding things
There was a problem hiding this comment.
Is get() really being triggered after update of the setting? onLoad() surely is
There was a problem hiding this comment.
@rodrigok which one is the preferred way to do what is being discussed here?
There was a problem hiding this comment.
@graywolf336 Yes, get with callback will be called everytime the setting changes
|
Did you guys tested what happens if you create the same API twice? |
|
@rodrigok you mean whther we tested that changing the CORS-Setting creates a working api? |
|
@mrsimpson Yes, now the same API could be create multiple times, is that working ok? Restivus replace the old v1 namespace by the new one correctly? |
|
@rodrigok I logged in via API, changed the setting and logged out via API again. However, when looking at the headers themselves, I noticed that the Thus, I did a quick where-used for So imho, this PR has been tested and does only make things better. @graywolf336, @rodrigok wdyt? |
|
The CI is showing a problem that could help by the delayed API creation: Any ideas? |
…-Restivus' into core/RocketChat#7915-enable-CORS-Restivus
|
@rodrigok I can't really understand this, to be honest. However, I noted that the delayed instantiation was done onLoad(), you recommended get(callback) some time back, so I changed that. let's see what the CI says now. |
…7915-enable-CORS-Restivus
…-Restivus' into core/RocketChat#7915-enable-CORS-Restivus
|
@mrsimpson That's why I said The solution for that, IMO, is register the normal API as before, without CORS enabled and then use the settings callback to register it again if different from previous registration, in this first case if the setting is true cuz the default registration was false. You could use a control variable to do that or check if you can access the config like |
|
@rodrigok you seem to have been right with the failure being caused by the delayed API creation: |
|
Oh, I just read your changes, that is almost what I said, can you change to only create the api if the setting value is different of the current value? And check if the first time you get the value from the setting in a sync way if the value is not undefined (set to false if undefined)? |
|
Amazing, how much |
| auth: getUserAuth() | ||
| }); | ||
| const createApi = function(enableCors) { | ||
| if (!RocketChat.API.v1 || RocketChat.API.v1._config.enableCors !== enableCors) { |
There was a problem hiding this comment.
I don't like accessing a "private" property for deciding about the buffering, particularly in an untyped world, but I preferred this to buffering the property in some other place.
|
Thanks @mrsimpson It looks good 😄 |


@RocketChat/core
Closes #7915
Previously, the setting to enable CORS on the API (
Admin > General > REST Api) added the CORS-header, but the information was not propagated to Restivus.