Skip to content

Conversation

@millerf
Copy link
Contributor

@millerf millerf commented Jan 12, 2023

Proposed changes:

Prevent access to WPCOM endpoint

Testing instructions

On your sandbox, use

./bin/jetpack-downloader test jetpack  trunk

Make sure the AI blocks work from a WPCOM site ( sandbox your site and test ).
Following this comment, test that the blocks WORK on an Atomic site (or any jetpack site)

Now apply this patch:

./bin/jetpack-downloader reset jetpack fix/prevent-access-to-endpoint &&  ./bin/jetpack-downloader test jetpack fix/prevent-access-to-endpoint

Make sure the AI blocks work from a WPCOM site ( sandbox your site and test ).
But now you should not be able to make the blocks work on Atomic or Jetpack site.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 12, 2023

Are you an Automattician? You can now test your Pull Request on WordPress.com. On your sandbox, run bin/jetpack-downloader test jetpack fix/prevent-access-to-endpoint to get started. More details: p9dueE-5Nn-p2

@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label Jan 12, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jan 12, 2023

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • 🔴 Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


🔴 Action required: We would recommend that you add a section to the PR description to specify whether this PR includes any changes to data or privacy, like so:

## Does this pull request change what data or activity we track or use?

My PR adds *x* and *y*.

Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.


Jetpack plugin:

  • Next scheduled release: February 7, 2023.
  • Scheduled code freeze: January 31, 2023.

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Jan 12, 2023
@millerf millerf marked this pull request as draft January 12, 2023 07:50
@millerf millerf requested review from fgiannar and jeherve January 12, 2023 07:50
@millerf millerf self-assigned this Jan 12, 2023
@jeherve jeherve added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Block] AI Image [Block] AI Paragraph labels Jan 12, 2023
@jeherve
Copy link
Member

jeherve commented Jan 12, 2023

Related discussion: p1673452156937829-slack-CBG1CP4EN

}

$response = Client::wpcom_json_api_request_as_user(
$response = Client::wpcom_json_api_request_as_blog(
Copy link
Contributor

Choose a reason for hiding this comment

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

No, we want to be able to pinpoint loggedin users for security purposes I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I followed those instructions: p1673510335525029/1673452156.937829-slack-CBG1CP4EN
But I am happy to revert...

Copy link
Contributor

Choose a reason for hiding this comment

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

If you prefer to make this feature available to only Jetpack connected users (aka excluding admins that haven't connected their WP.com account) then you might take this into account in the permission checks (non WPCOM ones) as well (also the UI but this is out of this PR's scope).

In general, this decision also affects the permission checks so it would be nice to reach a decision before further reviewing.

cc @jeherve

Copy link
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

Many thanks for working on this!

Left a couple of inline comments :)

}

$response = Client::wpcom_json_api_request_as_user(
$response = Client::wpcom_json_api_request_as_blog(
Copy link
Contributor

Choose a reason for hiding this comment

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

If you prefer to make this feature available to only Jetpack connected users (aka excluding admins that haven't connected their WP.com account) then you might take this into account in the permission checks (non WPCOM ones) as well (also the UI but this is out of this PR's scope).

In general, this decision also affects the permission checks so it would be nice to reach a decision before further reviewing.

cc @jeherve

*/
public function __construct() {
$this->is_wpcom = false;
$this->is_wpcom = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we actually use/need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I checked and it does not seem to be used anywhere...

CC @artpi

Copy link
Contributor

Choose a reason for hiding this comment

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

I think when @jeherve introduced this he meant to use wpcom_is_wpcom_only_endpoint. I think we could remove it

*/
public function permissions_check() {

if ( ! current_user_can( 'edit_posts' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Assuming we agree to make the request to WPCOM using wpcom_json_api_request_as_blog this wouldn't work as in such cases (blog token used) we'd get the rest_forbidden error

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that is why we are not wpcom_json_api_request_as_blog


if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
// Only allow WPCOM simple sites and Atomic
if ( get_current_site()->id !== 1 ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You might want to check the suggested implementation in Slack, in terms of allowing site level authentication (blog token) for AT site on WPCOM: p1673521187444489/1673452156.937829-slack-CBG1CP4EN

if ( get_current_site()->id !== 1 ) {
return true;
}
} elseif ( ( new Automattic\Jetpack\Status\Host() )->is_woa_site() ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

To ensure backwards compatibility you might prefer to only keep the WoA check on WPCOM context only. This way, when you decide to make the feature available to self-hosted sites, it will also be available in previous versions of the Jetpack plugin. Not a blocker though!

Copy link
Contributor

@artpi artpi left a comment

Choose a reason for hiding this comment

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

I am not a fan of this approach at all.

  1. This prevention check is something that Jetpack code should not even be aware of
  2. We need a way to quickly and dynamically change the value of it.

#28338 introduced the jetpack_ai_enabled filter which we can use to implement this check on the WPCOM end just by overriding the value to false for Jetpack sites (unless they have a special sticker?)

@artpi
Copy link
Contributor

artpi commented Jan 18, 2023

I would close this PR and make a diff on wpcom end.

@jeherve
Copy link
Member

jeherve commented Jan 18, 2023

#28338 introduced the jetpack_ai_enabled filter which we can use to implement this check on the WPCOM end just by overriding the value to false for Jetpack sites (unless they have a special sticker?)

I'll let @fgiannar weigh in on that, but I don't believe the filter is enough for our needs here.

I am not a fan of this approach at all.

This prevention check is something that Jetpack code should not even be aware of
We need a way to quickly and dynamically change the value of it.

This is indeed what we were discussing in p1673452156937829-slack-CBG1CP4EN

@artpi
Copy link
Contributor

artpi commented Jan 18, 2023

I don't believe the filter is enough for our needs here.

Stepping through code:

  1. Endpoint is called on the JP end, jetpack_ai_enabled is forced by the users to return true, proxies to WPCOM.
  2. WPCOM code is loaded, jetpack_ai_enabled has a default value of true because of IS_WPCOM, but we hook into this to return false for JP sites
  3. WPCOM_REST_API_V2_Endpoint_AI constructor returns before hooking into rest_api_init
  4. WPCOM endpoint returns 404
  5. User sees 404 in the editor instead of 403. Still acceptable

This is indeed what we were discussing in p1673452156937829-slack-CBG1CP4EN

Yes, I know, that would be a vote for decoupling syncing of this endpoint :D.
What I would love is to introduce the capability for Jetpack users to supply their own OpenAI token if they prefer to use it that way, but that would require some business plan discussion.
Then we could see if decoupling still makes sense.

@fgiannar
Copy link
Contributor

#28338 introduced the jetpack_ai_enabled filter which we can use to implement this check on the WPCOM end just by overriding the value to false for Jetpack sites (unless they have a special sticker?)

I think that would be easier to decide once we reach a decision on blog vs user tokens used to proxy the request to WPCOM

@artpi
Copy link
Contributor

artpi commented Jan 18, 2023

I think that would be easier to decide once

I am deciding here that we are not going to wpcom_json_api_request_as_blog, for multiple reasons:

  1. It messes up with caching
  2. Vast majority of Atomic sites have 1 admin anyway
  3. When we introduce billing than the site owner will get billed for the use generated by authors/editors. This may be unexpected and very troubling.

It is safer for now to request as user, so that we can see the usage in the wild. Once we see actual usage, we can revisit this decision of course.

@fgiannar
Copy link
Contributor

I am deciding here that we are not going to wpcom_json_api_request_as_blog

Thank you, Artur!

That will indeed simplify the permission callback on both envs. Just making sure here that on Atomic and self hosted (future) sites, we need to make sure the current user is connected in order to provide this feature.

@millerf
Copy link
Contributor Author

millerf commented Jan 25, 2023

Closing in favor of a WPCOM-based filter. See previous comments

@millerf millerf closed this Jan 25, 2023
@github-actions github-actions bot removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Status] In Progress labels Jan 25, 2023
@fgiannar
Copy link
Contributor

Closng in favor of a WPCOM-based filter. See previous comments

@millerf Can we please make sure to address the part of sanitizing request parameters in a follow up PR?

@millerf
Copy link
Contributor Author

millerf commented Jan 26, 2023

@millerf Can we please make sure to address the part of sanitizing request parameters in a follow up PR?

@fgiannar I have a PR here

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

Labels

[Block] AI Image [Block] AI Paragraph [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants