jsbundle

package
v0.140.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsbundle recovers JavaScript source maps from minified bundles. Used by error-reporting services to de-obfuscate stack traces.

The bundle URLs come from pages and error reports, so they are untrusted. By default (RecoverOptions.Client nil) every fetch goes through a safehttp client that refuses internal addresses; supply a client only when you need to reach one on purpose, such as an httptest server in a test.

Package jsbundle recovers original JavaScript source from minified production bundles using source maps, then scans the recovered source. It is the shared pipeline used by go_jsbundle_secrets, go_jsbundle_route_extractor, go_postmessage_listener_finder, and go_prototype_pollution_static.

Pipeline:

  1. Parse the target HTML.
  2. Collect every <script src=...> URL (absolute + same-origin relative).
  3. For each script, GET it (subject to the caller's *http.Client).
  4. Look for a sourceMappingURL comment, or probe "<bundle>.map" as fallback.
  5. If a v3 source map is found, decode the "sourcesContent" array and use that as the recovered source (one entry per original file).
  6. If no map exists, fall back to the minified bundle body as the "source" so callers can still grep for patterns.

The package is intentionally allocation-conservative: each recovered source chunk is returned with its origin URL and file path so callers can cite findings back to a specific source.

Index

Constants

View Source
const (
	// DefaultMaxScriptBytes caps a single bundle body. Production bundles
	// over a few MB are exceptional.
	DefaultMaxScriptBytes int64 = 8 * 1024 * 1024
	// DefaultMaxScripts caps how many <script src> tags we follow per page.
	DefaultMaxScripts = 32
	// DefaultMaxConcurrency caps parallel script fetches.
	DefaultMaxConcurrency = 4
)

Variables

View Source
var ErrNotJS = errors.New("response body is not JavaScript or JSON")

ErrNotJS is returned when the response body does not look like JavaScript or JSON. Currently unused but reserved for stricter callers.

Functions

func ExtractScriptURLs

func ExtractScriptURLs(html string, base *url.URL) []string

ExtractScriptURLs returns absolute script URLs found in html, resolved against base. Duplicates removed in encounter order.

Types

type RecoverOptions

type RecoverOptions struct {
	// Client fetches bundles and source maps. If nil, a safehttp client is
	// used: it refuses loopback, private, link-local and other internal
	// addresses (also after redirects), because the URLs come from pages and
	// error reports that the caller does not control. Pass your own client to
	// opt out, for example the httptest server's client in a test.
	Client         *http.Client
	UserAgent      string
	MaxScriptBytes int64
	MaxScripts     int
	MaxConcurrency int
}

RecoverOptions tunes the pipeline.

type Source

type Source struct {
	// BundleURL is the URL of the minified bundle this came from.
	BundleURL string `json:"bundle_url"`
	// FilePath is the "sources[i]" entry from the source map (e.g.
	// "webpack:///./src/foo.js"). Empty when the map was unavailable.
	FilePath string `json:"file_path,omitempty"`
	// FromMap is true when this chunk came from a sourcesContent entry.
	// False means the chunk is the raw (likely minified) bundle body.
	FromMap bool `json:"from_map"`
	// Content is the recovered source text.
	Content string `json:"-"`
	// SizeBytes is len(Content). Exposed for telemetry without dumping body.
	SizeBytes int `json:"size_bytes"`
}

Source is one chunk of recovered source code.

func Recover

func Recover(ctx context.Context, bundleURL string, opt RecoverOptions) ([]Source, error)

Recover downloads bundleURL and any associated .map, returning one Source per original file when a source map is available, or a single Source with the raw bundle body otherwise. With no opt.Client it uses a shared safehttp client; if that cannot be built (FLEET_REQUIRE_PROXY=1 with no proxy variable set) it returns the error and fetches nothing.

func RecoverAll

func RecoverAll(ctx context.Context, bundleURLs []string, opt RecoverOptions) []Source

RecoverAll runs Recover across every bundleURL with bounded concurrency. Failed bundles are silently dropped — partial recovery is the common case. If the default client cannot be built (see Recover) it logs the reason and returns nil.

func RecoverFromPage

func RecoverFromPage(ctx context.Context, pageHTML string, pageURL *url.URL, opt RecoverOptions) []Source

RecoverFromPage convenience wrapper: fetches html (caller may have it already and pass via Source.Content workaround), but this variant takes a pre-fetched body and a base URL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL