Skip to content

Switch to proxy URL to handle API requests to demo, #PG-5029#876

Open
lachiebol wants to merge 4 commits intolivefrom
PG-5029-fix-cors
Open

Switch to proxy URL to handle API requests to demo, #PG-5029#876
lachiebol wants to merge 4 commits intolivefrom
PG-5029-fix-cors

Conversation

@lachiebol
Copy link
Copy Markdown
Contributor

Description

Please include a description of this change and which issue it fixes. If no issue exists yet please include context and what problem it solves.

Checklist

  • [✔] I have understood, reviewed, and tested all AI outputs before use
  • [✔] All AI instructions respect security, IP, and privacy rules

Review

window.onload = function () {
let pluginSpec = {{ pluginSpecJson|raw }};
let pluginSpecJson = {{ pluginSpecJson|raw }};
let proxyBaseUrl = window.location.origin + '/demo-proxy';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let proxyBaseUrl = window.location.origin + '/demo-proxy';
let proxyBaseUrl = '/demo-proxy';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made that change, it also collapses the server field down to this
image

I changed it to 'demo' rather than 'demo-proxy', proxy felt too technical for a user facing page.


$proxiedResponse = DemoProxy::get($targetUrl, $request->getHeaderLine('Authorization'));

$response->getBody()->write($proxiedResponse['body']);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a try/catch here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I also realised we return errors with a 200 OK? At the moment it doesn't break anything in Swagger, technically we could catch in the proxy and throw something else

});

$app->get('/demo-proxy/{path:.*}', function (Request $request, Response $response, $args) {
$path = ltrim($args['path'] ?? '', '/');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main concern is that it now becomes a proxy to query demo.matomo.cloud without any CORS error

We can add a check that requestURL path should be index.php and Module should be API along with CSRF token.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have added some URL validation so only the API can be queried.

Demo is a public instance, are we exposing anything that isn't already available with this proxy? For the CSRF token, we don't have a session on developer docs, so not sure where we can store that

Copy link
Copy Markdown
Contributor

@AltamashShaikh AltamashShaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct ?

Image

Copy link
Copy Markdown
Contributor

@AltamashShaikh AltamashShaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left few comments

@lachiebol
Copy link
Copy Markdown
Contributor Author

Is this correct ?

Image

@AltamashShaikh That's the new proxy endpoint that swagger will make requests against, do you think it should still show demo.matomo.cloud? I might check if I can change the text but not the URL

Copy link
Copy Markdown
Contributor

@AltamashShaikh AltamashShaikh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lachiebol Good to add some tests with the help of AI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants