Skip to content

feat: make it possible to restrict control server commands to stats#3787

Merged
MSP-Greg merged 2 commits intopuma:masterfrom
stanhu:sh-control-server-enabled-commands
Oct 18, 2025
Merged

feat: make it possible to restrict control server commands to stats#3787
MSP-Greg merged 2 commits intopuma:masterfrom
stanhu:sh-control-server-enabled-commands

Conversation

@stanhu
Copy link
Copy Markdown
Contributor

@stanhu stanhu commented Oct 3, 2025

In production, we want to make the /stats and /gc-stats endpoints available, but we don't want to expose the ability to manipulate the state of the processes.

This commit adds a data_only option. For example:

activate_control_app "tcp://127.0.0.1:8182", { data_only: true }

Description

Thank you for contributing! You're the best.

We can read your code, so consider leaving some comments here that are more about your motivations and decision making. Some things that may be helpful to address in your description:

  • What original problem led to this PR?
  • Are there related issues / prior discussions?
  • What alternatives have been tried? Does this supersede previous attempts?
  • Why do you make the choices you did? What are the tradeoffs?

Your checklist for this pull request

  • I have reviewed the guidelines for contributing to this repository.
  • I have added (or updated) appropriate tests if this PR fixes a bug or adds a feature.
  • My pull request is 100 lines added/removed or less so that it can be easily reviewed.
  • If this PR doesn't need tests (docs change), I added [ci skip] to the title of the PR.
  • If this closes any issues, I have added "Closes #issue" to the PR description or my commit messages.
  • I have updated the documentation accordingly.
  • All new and existing tests passed, including Rubocop.

@github-actions github-actions Bot added the waiting-for-review Waiting on review from anyone label Oct 3, 2025
@stanhu
Copy link
Copy Markdown
Contributor Author

stanhu commented Oct 3, 2025

@schneems What do you think of this pull request?

@MSP-Greg
Copy link
Copy Markdown
Member

MSP-Greg commented Oct 3, 2025

I have thought about this previously. I was thinking more a 'data_only' switch, so the command strings weren't needed. Another option would be two passwords, one for 'admin/all' access, another (or nil) for 'data only'.

@stanhu stanhu changed the title feat: make it possible to selectively enable control server commands feat: make it possible to restrict control server commands to stats Oct 3, 2025
@stanhu stanhu force-pushed the sh-control-server-enabled-commands branch from a86df44 to b6fced0 Compare October 3, 2025 23:39
@stanhu
Copy link
Copy Markdown
Contributor Author

stanhu commented Oct 3, 2025

@MSP-Greg Ok, I updated this pull request accordingly. I didn't include backtraces because this might expose details only an admin should know.

@stanhu stanhu force-pushed the sh-control-server-enabled-commands branch from b6fced0 to d865c6d Compare October 3, 2025 23:43
@MSP-Greg
Copy link
Copy Markdown
Member

MSP-Greg commented Oct 4, 2025

@stanhu

To the above message I should have added "Let's see if @schneems has any thoughts". Otherwise, LGTM.

Nit pick:

      data_only = @options[:control_data_only]
      app = Puma::App::Status.new @launcher, token, data_only

Since data_only is only used on the next line, maybe:

      app = Puma::App::Status.new @launcher, token, @options[:control_data_only]

@stanhu stanhu force-pushed the sh-control-server-enabled-commands branch from d865c6d to 32ff524 Compare October 4, 2025 04:55
@github-actions github-actions Bot added waiting-for-merge and removed waiting-for-review Waiting on review from anyone labels Oct 4, 2025
@stanhu stanhu force-pushed the sh-control-server-enabled-commands branch from 32ff524 to 5943b48 Compare October 6, 2025 17:30
@stanhu
Copy link
Copy Markdown
Contributor Author

stanhu commented Oct 6, 2025

Just pushed a slight correction to fix the commit message to data_only.

Comment thread lib/puma/app/status.rb Outdated
# @param data_only [Boolean] if true, restrict to read-only data commands
#
def initialize(launcher, token = nil)
def initialize(launcher, token = nil, data_only = false)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use kwargs instead of positional args here for the newly added data_only.

@dentarg @MSP-Greg this class doesn't have any docs regarding public/private. I'm unsure if people use it directly, you think it's okay to change token to also be a kwarg or is that breaking?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm unsure if people use it directly

I've got several "Don't break" bookmarks, and I don't recall any using this directly.

you think it's okay to change token to also be a kwarg or is that breaking?

Do you really think these need to change to kwargs? Adding an additional positional at the end, where the proceeding arg also has a default value shouldn't be a problem. Also, I don't see the arg list growing. Granted, kwargs are 'identifying', it not like the init code is used frequently...

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.

I refactored this method using kwargs. I think it would be rare for projects to instantiate Puma::App::Status.new directly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comes down to personal preference. I have ADHD and kwargs help offload some memory of “what is this art supposed to be representing” to the name. Makes it easier for me to spot mistakes.

the downside is that it’s slightly slower than an all-positional method call. But for object instantiation, especially one that’s not in a hot loop, it’s more than worth it (and maybe even in a hot loop might not move the needle, I’ve not actually benchmarked.

Don't break" bookmarks

If not already in existing docs, it would be a good PR to note this in the code.

Comment thread lib/puma/app/status.rb Outdated
@github-actions github-actions Bot added waiting-for-review Waiting on review from anyone and removed waiting-for-merge labels Oct 16, 2025
In production, we want to make the `/stats` and `/gc-stats` endpoints
available, but we don't want to expose the ability to manipulate the
state of the processes.

This commit adds a `data_only` option. For example:

```
activate_control_app "tcp://127.0.0.1:8182", { data_only: true }
```
@stanhu stanhu force-pushed the sh-control-server-enabled-commands branch from 5943b48 to ad53342 Compare October 17, 2025 05:22
@stanhu
Copy link
Copy Markdown
Contributor Author

stanhu commented Oct 17, 2025

@schneems Thanks for the review! Could you take a look again?

@stanhu stanhu requested a review from schneems October 17, 2025 16:00
@schneems
Copy link
Copy Markdown
Member

Thanks! Approved the change and restarted the failing test.

@github-actions github-actions Bot added waiting-for-merge and removed waiting-for-review Waiting on review from anyone labels Oct 17, 2025
@MSP-Greg MSP-Greg merged commit 1183dc9 into puma:master Oct 18, 2025
129 of 130 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants