Skip to content

Embedded-image base paths do not contain auto-resolved resources #678

Closed

Description

@bbottema

Summary

Embedded-image auto-resolution can read files, classpath resources, or URLs referenced from HTML and attach their contents to an outgoing email. When a base directory, classpath, or URL is configured and the corresponding allowingEmbeddedImageOutsideBase*(false) default is retained, callers reasonably expect resolution to stay inside that base.

The current implementation uses string-prefix checks and unnormalized path construction. Those checks do not provide a reliable containment boundary.

Actual behavior

  • Disk references are joined with new File(baseDir, srcLocation) without canonical containment, so ../, sibling-prefix paths, and symlinks can escape the configured base.
  • Classpath references are concatenated and compared as strings, allowing traversal and segment-boundary ambiguity.
  • Absolute URL references are accepted when their path starts with the base path; scheme, host, and effective port are not compared.
  • URL dot segments, encoded traversal, and redirects are not constrained to the configured base.
  • Existing tests cover ordinary inside/outside resources but not adversarial references.

Auto-resolution is disabled by default. The security impact applies when an application enables it and processes HTML that an untrusted party can influence.

Expected behavior

With a base configured and the matching allowingEmbeddedImageOutsideBase*(false) setting:

  • relative and absolute disk resources must resolve to a real path inside the configured real base path;
  • classpath resource paths must normalize to a segment inside the configured classpath base;
  • URLs must use the same scheme, host, and effective port and remain inside the normalized base path;
  • redirects must not escape the permitted URL boundary;
  • traversal or an outside resource is treated as unresolved.

With no base configured, resolution remains unrestricted by design. Explicit allowingEmbeddedImageOutsideBase*(true) settings retain their documented broad behavior.

Compatibility impact

This is a security-hardening behavior change for applications that currently rely on traversal, symlinks outside the base, sibling-prefix paths, alternate URL authorities, or escaping redirects while the relevant “allow outside” setting is false.

Applications that deliberately need those resources can opt in with the corresponding allowingEmbeddedImageOutsideBase*(true) setting, or move the resource under the configured base.

Historical context

  • #260 introduced dynamic embedded-image resolution in 6.2.0 for templates that users could edit without changing Java code. Commit 416399fd made resolution opt-in and added the base and “allow outside” controls, explicitly identifying outside-base access as a security surface. Its tests covered ordinary inside/outside examples but not traversal or URL-origin attacks.
  • #617 replaced live-site URL tests with deterministic file: URL coverage in 9.0.0. That made the suite reliable and preserved base-path behavior, but did not exercise HTTP authorities, ports, redirects, or encoded traversal.

Proposed fix

  • Normalize and validate each resource before loading it.
  • Use real filesystem paths so symlink targets are included in containment checks.
  • Normalize classpath segments and reject encoded or decoded traversal outside the base.
  • Resolve URLs with URI semantics; compare scheme, host, effective port, and path segment boundaries.
  • Follow only redirects that remain inside the URL boundary.
  • Add adversarial unit/integration tests and update Javadocs and website guidance.
  • Feature the compatibility and security warning in the v9.2.0 migration notes, repository release notes, and GitHub draft release.

Acceptance criteria

  • Disk ../, sibling-prefix, absolute-outside, and symlink escapes are blocked by default.
  • Classpath traversal and sibling-prefix escapes are blocked by default.
  • Alternate schemes, hosts, ports, paths, encoded traversal, and escaping redirects are blocked by default.
  • Valid resources inside each base continue to resolve.
  • Explicit “allow outside” options retain their documented behavior.
  • No-base behavior is documented as unrestricted convenience mode.
  • Javadocs, website documentation, migration notes, and release material describe the same contract.
  • Targeted Java 8 tests pass.

Activity

  1. added this to the v9.2.0 milestone on Aug 5, 2026
  2. bbottema commented on Aug 5, 2026

    @bbottema
    OwnerAuthor

    Implemented.

    What changed

    • Filesystem resolution now checks the resolved real path against the resolved real base, including symbolic-link targets.
    • Classpath resolution normalizes complete path segments instead of relying on string prefixes.
    • URL resolution now requires the base scheme, host, effective port and normalized path, and rechecks the boundary after every redirect.
    • No-base behavior remains unrestricted; the explicit allowingEmbeddedImageOutsideBase...(true) options retain the broad compatibility behavior.
    • Javadocs, the feature guide, the v9.2 migration guide, repository release notes and the v9.2 draft GitHub release all describe the same contract.

    Commits

    • 7cc0fef5 — implementation, Javadocs and adversarial tests
    • 08495bb — website and migration guidance
    • fa122d9d — repository release notes and completed audit-plan item

    Historical breadcrumbs and backlinks are recorded in #260 and #617.

  3. bbottema commented on Aug 8, 2026

    @bbottema
    OwnerAuthor

    This issue's change is now available in Simple Java Mail 9.2.0 and from Maven Central.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions