Skip to content

Added simple script to fetch API specs, #PG-5043#949

Merged
AltamashShaikh merged 3 commits into
livefrom
PG-5043-fetch-api-specs
Apr 23, 2026
Merged

Added simple script to fetch API specs, #PG-5043#949
AltamashShaikh merged 3 commits into
livefrom
PG-5043-fetch-api-specs

Conversation

@lachiebol

@lachiebol lachiebol commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Description

NOTE: This OpenApiDocs change is needed to run this script matomo-org/plugin-ApiReference#30

Script to pull specs from cloud, default MATOMO_BASE_URL will be demo.matomo.cloud

MATOMO_BASE_URL="{matomo-url}" php fetch-openapi-specs.php

Checklist

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

Review

Comment thread fetch-openapi-specs.php Outdated
$params['token_auth'] = $tokenAuth;

$url = $baseUrl . '/index.php?' . http_build_query($params);
$response = @file_get_contents($url);

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 use curl ideally for remote URLs

Comment thread fetch-openapi-specs.php

$written++;
} catch (Throwable $e) {
fwrite(STDERR, "Failed for $plugin: {$e->getMessage()}\n");

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.

Here it will keep continuing unnecessarily if file_put_contents fail, the issue would be consistent for all

@AltamashShaikh AltamashShaikh left a comment

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.

@lachiebol left few comments

@lachiebol

Copy link
Copy Markdown
Contributor Author

@AltamashShaikh

From codex:

  1. High: the fetch is only atomic per file, not for the spec set, so a mid-run failure leaves the docs site serving a mixed snapshot. In fetch-openapi-specs.php:77, each plugin
     file is replaced immediately, and on the first later failure the script exits at fetch-openapi-specs.php:111. That means earlier plugins are now from the new Matomo instance/
     version, while untouched files still represent the previous run. Since the app reads whatever is present in app/public/openapi via app/helpers/OpenApiSpecRegistry.php:21, users
     can get a partially updated API reference without any indication it is inconsistent.

This doesn't seem too bad for our use case right? I know we could write to a tmp folder then move after everything is done.

Not sure if I need to address that one.

@AltamashShaikh AltamashShaikh left a comment

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.

@lachiebol AI review

High-risk issues (blocking)

  - Secret leakage in error output: fetch-openapi-specs.php:26 and fetch-openapi-specs.php:43 include the full request URL in thrown exceptions. That URL contains token_auth, so any network/API failure will print
    credentials to stderr via the top-level handlers at fetch-openapi-specs.php:71 and fetch-openapi-specs.php:112. For a script likely to run in CI or shared shells, that is a real credential exposure.
  - Stale spec files are never removed: the script only writes files for the current whitelist, but the docs app enumerates all matching files on disk via app/helpers/OpenApiSpecRegistry.php:21. If a plugin is removed
    from the whitelist or renamed, its old *_openapi_spec_v1.0.0.json file remains and will still be shown in the UI. That makes the generated docs drift from the source instance.

@lachiebol

Copy link
Copy Markdown
Contributor Author

@lachiebol AI review

High-risk issues (blocking)

  - Secret leakage in error output: fetch-openapi-specs.php:26 and fetch-openapi-specs.php:43 include the full request URL in thrown exceptions. That URL contains token_auth, so any network/API failure will print
    credentials to stderr via the top-level handlers at fetch-openapi-specs.php:71 and fetch-openapi-specs.php:112. For a script likely to run in CI or shared shells, that is a real credential exposure.
  - Stale spec files are never removed: the script only writes files for the current whitelist, but the docs app enumerates all matching files on disk via app/helpers/OpenApiSpecRegistry.php:21. If a plugin is removed
    from the whitelist or renamed, its old *_openapi_spec_v1.0.0.json file remains and will still be shown in the UI. That makes the generated docs drift from the source instance.

Secret leakage in error output: Should only be leaking token_auth anonymous but can remove that

Stale spec files are never removed: Can fix that one

@AltamashShaikh AltamashShaikh left a comment

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.

@lachiebol Looks good we can test it together after deployment and I should be able to create a cron for you

@AltamashShaikh AltamashShaikh merged commit 6518d03 into live Apr 23, 2026
1 check passed
@AltamashShaikh AltamashShaikh deleted the PG-5043-fetch-api-specs branch April 23, 2026 02:05
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