Skip to content

ETT-477 Update under_server references in imgsrv#196

Open
moseshll wants to merge 3 commits intomainfrom
ETT-477_under_server
Open

ETT-477 Update under_server references in imgsrv#196
moseshll wants to merge 3 commits intomainfrom
ETT-477_under_server

Conversation

@moseshll
Copy link
Contributor

  • Remove code that de-restricts when not under server (e.g., $self->restricted(0) unless ( Debug::DUtils::under_server() );)
    • SRV/Article/EPUB.pm
    • SRV/Article/PDF.pm
    • SRV/Volume/Base.pm
    • SRV/Volume/EPUB.pm
    • SRV/Volume/HTML.pm
    • SRV/Volume/PDF.pm
    • SRV/Volume/Remediated/Bundle.pm
  • Remove conditional use of SRV::Utils::Stream for output (e.g., if ( ! $self->output_filename || Debug::DUtils::under_server() ))
    • SRV/Article/PDF.pm
    • SRV/Volume/PDF.pm
  • Completely de-restrict SRV/Cover.pm
  • Always translate to character entities in DEBUG output
    • SRV/SearchUtils.pm
  • Explicitly initialize super to undef in _default_params() since I believe the existing code is wrong
    • SRV/Volume/Base.pm
  • Miscellaneous logic simplifications
    • SRV/Image.pm
    • SRV/Utils.pm
  • Update psgi apps to use SRV::Utils::under_server
    • FIXME: do we even need that?
  • Remove Article/JATS support

- Remove code that de-restricts when not under server (e.g., `$self->restricted(0) unless ( Debug::DUtils::under_server() );`)
  - `SRV/Article/EPUB.pm`
  - `SRV/Article/PDF.pm`
  - `SRV/Volume/Base.pm`
  - `SRV/Volume/EPUB.pm`
  - `SRV/Volume/HTML.pm`
  - `SRV/Volume/PDF.pm`
  - `SRV/Volume/Remediated/Bundle.pm`
- Remove conditional use of `SRV::Utils::Stream` for output (e.g., `if ( ! $self->output_filename || Debug::DUtils::under_server() )`)
  - `SRV/Article/PDF.pm`
  - `SRV/Volume/PDF.pm`
- Completely de-restrict SRV/Cover.pm
- Always translate to character entities in DEBUG output
  - `SRV/SearchUtils.pm`
- Explicitly initialize `super` to `undef` in `_default_params()` since I believe the existing code is wrong
  - `SRV/Volume/Base.pm`
- Miscellaneous logic simplifications
  - `SRV/Image.pm`
  - `SRV/Utils.pm`
- Update psgi apps to use `SRV::Utils::under_server`
  - FIXME: do we even need that?
builder {

if ( under_server() ) {
if ( SRV::Utils::under_server() ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The question is, do we even need these at all? Applies to this file and imgsrv.psgi as well.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how I feel about URLFixer in general. If there are percent-escaped ';' or '=' characters in the URL, then it will unescape the entire URL and redirect to that. That's a problem we see with the ChatGPT-created queries to ssd.

I could imagine some kind of redirection attack though -- basically tricking imgsrv to redirecting to something unexpected - but I'm not sure exactly what the threat model would be.

It may be worthwhile seeing if there's evidence of legit requests to imgsrv that would trigger this (i.e. requests to imgsrv containing %3B or %3D), and if we can't find any, we just disable URLFixer entirely.

# this can only be passed from the command line
$params{super} = undef;
}

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 believe the existing code should be if instead of unless because it makes no sense otherwise based on the comment. I don't know if this change is more secure big picture wise, but it is simpler.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this doesn't make any sense. We should check if we remove it that debug=super still works & works only for logged-in users with appropriate permissions.

@moseshll moseshll marked this pull request as ready for review March 20, 2026 16:14
@moseshll moseshll requested a review from aelkiss March 20, 2026 16:14
Copy link
Member

@aelkiss aelkiss left a comment

Choose a reason for hiding this comment

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

I have a number of questions, mostly if we should get rid of some of the other under_server checks entirely, and making sure my understanding is the same.

builder {

if ( under_server() ) {
if ( SRV::Utils::under_server() ) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how I feel about URLFixer in general. If there are percent-escaped ';' or '=' characters in the URL, then it will unescape the entire URL and redirect to that. That's a problem we see with the ChatGPT-created queries to ssd.

I could imagine some kind of redirection attack though -- basically tricking imgsrv to redirecting to something unexpected - but I'm not sure exactly what the threat model would be.

It may be worthwhile seeing if there's evidence of legit requests to imgsrv that would trigger this (i.e. requests to imgsrv containing %3B or %3D), and if we can't find any, we just disable URLFixer entirely.

enable "PopulateENV", app_name => 'imgsrv';

enable_if { (under_server() && $ENV{HT_DEV}) } 'StackTrace';
enable_if { (SRV::Utils::under_server() && $ENV{HT_DEV}) } 'StackTrace';
Copy link
Member

Choose a reason for hiding this comment

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

Can we think of any reason not to enable solely based on HT_DEV or (maybe better) the plack environment?

enable_if { (SRV::Utils::under_server() && ! $ENV{HT_DEV}) } "HTTPExceptions", rethrow => 0;

if ( under_server() ) {
if ( SRV::Utils::under_server() ) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd be inclined to just always enable these -- given we do local development in docker, I can't think of a case where we wouldn't be under_server.

# this can only be passed from the command line
$params{super} = undef;
}

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this doesn't make any sense. We should check if we remove it that debug=super still works & works only for logged-in users with appropriate permissions.

my $ext = shift;
my $output_filename =
SRV::Utils::generate_output_filename($env, [ $self->file, $self->mode, 'full', $self->size, '0', $self->quality, $self->restricted, 'ZZZ' ], $ext);
SRV::Utils::generate_output_filename($env, [ $self->file, $self->mode, 'full', $self->size, '0', $self->quality, 'ZZZ' ], $ext);
Copy link
Member

Choose a reason for hiding this comment

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

Just removing $self->restricted here threw me for a loop, but it looks like generate_output_filename just makes a hash of all the options, and if covers are never restricted then having it in the hash doesn't matter. It does change the cache key for existing cover images, though, so I might be inclined to keep it in the hash?

my $restricted = $self->restricted;
unless ( defined $restricted ) {
# $restricted = $C->get_object('Access::Rights')->assert_final_access_status($C, $gId) ne 'allow';
$restricted = $$env{'psgix.restricted'};
Copy link
Member

Choose a reason for hiding this comment

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

Could there be other ways (throttling, etc) where psgix.restricted gets set, even though in general covers should be unrestricted?

Copy link
Member

Choose a reason for hiding this comment

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

Or I guess the point is that restricted could be set, but it doesn't matter because we serve the cover anyway?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants