diff --git a/.changeset/pre.json b/.changeset/pre.json new file mode 100644 index 0000000..603c308 --- /dev/null +++ b/.changeset/pre.json @@ -0,0 +1,16 @@ +{ + "mode": "pre", + "tag": "rc", + "initialVersions": { + "css-rules": "1.1.0", + "extract-css": "3.0.2", + "href-content": "2.0.3", + "list-stylesheets": "2.0.2", + "mediaquery-text": "1.2.0", + "remote-content": "4.0.1", + "style-data": "2.0.2" + }, + "changesets": [ + "release-five" + ] +} diff --git a/.changeset/release-five.md b/.changeset/release-five.md new file mode 100644 index 0000000..c11c58f --- /dev/null +++ b/.changeset/release-five.md @@ -0,0 +1,11 @@ +--- +"css-rules": major +"extract-css": major +"href-content": major +"list-stylesheets": major +"mediaquery-text": major +"remote-content": major +"style-data": major +--- + +Require Node 22.12+, enforce explicit bounded stylesheet permissions, preserve CSS cascade and media conditions, and protect literal template blocks without collisions. Update standalone loader and parser contracts. See the root migration guide for compatibility changes. The root inline-css package is versioned explicitly because it is outside the workspace package list. diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4a4a1e8..c1aee93 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,26 +1,25 @@ name: CI - -on: - - push - - pull_request - +on: [push, pull_request] +permissions: + contents: read jobs: test: - name: Node.js ${{ matrix.node-version }} - runs-on: ubuntu-latest - + name: Node ${{ matrix.node }} on ${{ matrix.os }} + runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - node-version: - - 20 - - 18 - + node: ['22.12.0', '22', '24', '26'] + os: [ubuntu-latest, windows-latest] steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v7 + - uses: actions/setup-node@v7 with: - node-version: ${{ matrix.node-version }} - - run: npm install + node-version: ${{ matrix.node }} + cache: npm + - run: npm ci + - run: npx playwright install --with-deps chromium - run: npm run lint - run: npm test + - run: npm run release:audit + - run: npm run release:pack diff --git a/.gitignore b/.gitignore index efeeb7c..326f8ac 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,7 @@ node_modules coverage .nyc_output .turbo + +# Generated from shared internal sources before testing/packing. +/lib/internal/ +/packages/*/lib/internal/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2a68167 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,13 @@ +# Changelog + +## 5.0.0-rc.1 (unpublished) + +Release candidate for the 16-issue scope in [proposal #158](https://github.com/jonkemp/inline-css/issues/158). + +- Require Node.js 22.12+, explicit stylesheet permissions, and finite loading limits; built-in fetching is direct-only. +- Correct important/numeric specificity, shorthand precedence, fallback declarations, selector lists, and linked/embedded source order. +- Honor media-qualified sources and preserve requested linked/extra CSS media rules; reject selector removal when CSS is retained. +- Accept empty styles; protect literal custom template delimiters without placeholder collisions; avoid inherited CSS-key collisions. +- Update dependencies, callback/parser contracts in companion packages, and clean package-install validation. + +See [migration guidance](docs/migration-5.md). Numeric limits remain RC defaults pending representative production feedback. This candidate has not been published. diff --git a/README.md b/README.md index 756353c..5b945d2 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ Whether to remove the original `` tags after (possibly) i #### options.url Type: `String` -Default: `filePath` +Default: none How to resolve hrefs. **Required**. @@ -182,3 +182,11 @@ See the [CONTRIBUTING Guidelines](https://github.com/jonkemp/inline-css/blob/mas ## License MIT © [Jonathan Kemp](http://jonkemp.com) + +## Migrating to 5.0 + +Requires Node.js **22.12+**. External stylesheets now require explicit permissions; linked resources have finite configurable limits, and built-in fetching is direct-only. See the [5.0 migration guide](docs/migration-5.md) for examples, media/selector behavior, error codes, and standalone package changes. + +## Internal helper build + +Shared policy and template code lives in `internal/` and is bundled into the existing package tarballs. No additional helper packages are published. Run `npm run build` for direct use from a checkout; `npm test` and the package prepack hooks build automatically. Generated `lib/internal/` files are ignored by Git. diff --git a/docs/backlog-after-top-ten.md b/docs/backlog-after-top-ten.md new file mode 100644 index 0000000..1f577e4 --- /dev/null +++ b/docs/backlog-after-top-ten.md @@ -0,0 +1,74 @@ +# Backlog beyond the top ten + +Reviewed 2026-11-09 against the current local 4.0.3 checkout. These are proposed issue tickets, not GitHub issues. IDs continue the earlier review's numbering. No implementation changes were made. + +## Release recommendation + +Use release criteria rather than an issue count. Keep the existing ten as the core 5.0 work and add six narrowly scoped correctness tickets: B11, B12, B14, B16, B17, and B18. They prevent unconditional styling, broken retained CSS, or corrupted template/output data. They largely touch extraction, selector cleanup, and template handling already involved in the plan. + +B15, B19, and B20 are good additional candidates if their fixes remain contained. Defer broader at-rule preservation and option redesign rather than extending 5.0 indefinitely. Any deferred limitation needs a clear documented workaround, and release candidates must not introduce regressions in those paths. + +The suggested defaults in the existing release plan remain proposals. In particular, preserve conditional styles without evaluating an imaginary browser viewport: unconditional inlining of print/responsive styles is incorrect. + +## Confirmed additional issues + +Every row below was reproduced with a one-off call to the public `inlineCss()` API. The common options were `{url: 'file:///tmp/input.html'}`, plus the row's options. For linked cases, a temporary CSS file was created under `/tmp`; there were no network requests. Confirmed means the observed behavior was reproduced, not that every possible downstream rendering effect was browser-tested. + +| ID | Suggested target | Proposed issue | Reproduction and observed behavior | Acceptance criteria / likely files | +| --- | --- | --- | --- | --- | +| B11 | **5.0 required** | Honor `media` attributes on style and link elements | `
x
` produces unconditional inline red. A stylesheet link with `media="print"` also applies unconditionally. | Carry conditions through source extraction; do not inline conditional declarations as unconditional. Preserve them when requested, with the condition intact. Test both style/link tags, missing/empty/all media, print, and responsive conditions. `packages/style-data`, `packages/list-stylesheets`, `packages/extract-css`. | +| B12 | **5.0 required** | Preserve media queries from linked stylesheets | A linked CSS file containing normal declarations and `@media(max-width:600px){.x{color:blue}}` loses the media rule even with `preserveMediaQueries:true`. | Apply media preservation to every CSS source, including linked content; specify/test `extraCss` too. Retain source order and media conditions without a duplicate cascade. `packages/extract-css`, `packages/style-data`, `packages/mediaquery-text`. | +| B13 | Later fix | Preserve nested media content | `` with `preserveMediaQueries:true` becomes an empty `@media screen {}`. | Preserve nested rule trees rather than reconstructing only direct style/font-face children. Include supports rules nested inside media in tests. `packages/mediaquery-text/index.js`. | +| B14 | **5.0 required** | Prevent selector removal from breaking retained CSS | With `preserveMediaQueries:true, removeHtmlSelectors:true`, `x
` retains `.x` CSS but removes `class="x"`. | Define the option interaction explicitly. Recommended: reject selector stripping when CSS is retained, rather than attempt unsafe selector dependency analysis. Cover retained media, `data-embed`, retained style/link tags, and indirect descendant selectors. `lib/inline-css.js`, `lib/removeClassId.js`, entry validation. | +| B15 | 5.0 candidate; otherwise prompt follow-up | Rebase relative asset URLs when moving linked CSS | Linked CSS in `/tmp/inline-css-backlog.css` containing `background:url(assets/a.png)` is copied unchanged into the HTML. Moving CSS to a document in another directory changes the asset's base. | Resolve CSS asset URLs against the original stylesheet URL, including the final redirect URL. Preserve data URLs, absolute URLs, fragments and templates appropriately; do not fetch assets. Use a CSS value parser. `packages/remote-content`, `packages/extract-css`, CSS parsing pipeline. | +| B16 | **5.0 required** | Treat custom template delimiters as literal text | `[[ value ]]
` with `codeBlocks:{X:{start:'[[',end:']]'}}` throws an invalid-RegExp SyntaxError. | Escape literal delimiter text or use a literal scanner; validate empty/non-string delimiters and define overlap behavior. Test brackets, parentheses, dollar signs, backslashes, multiline blocks and multiple block kinds. `lib/inline-css.js`, `packages/list-stylesheets/index.js`, `packages/style-data/index.js`. | +| B17 | **5.0 required** | Prevent template placeholder collisions | `EXCS_CODE_BLOCK_0_ {{value}}
` becomes `{{value}} {{value}}
`. A literal string in the input is overwritten. | Choose collision-free placeholders after checking the input, restore only generated tokens, and share protection logic across stages. Test literal token-shaped strings in text and attributes, multiple blocks, repeated processing, and token-like content inside a block. Same three template-protection files as B16. | +| B18 | **5.0 required** | Avoid inherited object keys in CSS property storage | `x
` loses all styling, including valid blue. `styleProps={}` treats inherited `constructor` as an existing property, throws during comparison, and the broad selector catch hides it. | Use own-key-safe storage or the planned declaration arrays. Unknown property names must not suppress valid declarations or alter object prototypes. Test `constructor`, `toString`, `__proto__`, custom properties, and a valid declaration before/after each. `lib/handleRule.js`, `lib/addProps.js`, `lib/inline-css.js`. This is a confirmed key-collision/output bug, not a demonstrated prototype-pollution exploit. | +| B19 | 5.0 candidate | Only create valid numeric width attributes | `x
` retains the font-family declaration but deletes the font definition under defaults. | Define an explicit preservation policy for font-face and other non-inlineable at-rules, with tests and asset rebasing where applicable. Preserve a documented `data-embed` workaround in the meantime. Do not bundle general support for all modern CSS into this ticket. `packages/style-data`, `packages/mediaquery-text`. | + +### Standards behind the expected behavior + +- Media attributes qualify when styles apply: [HTML style/link semantics](https://html.spec.whatwg.org/multipage/semantics.html#attr-style-media). +- Relative CSS URLs use their stylesheet's base, which matters when declarations are relocated: [CSS relative URLs](https://www.w3.org/TR/css-values-4/#relative-urls). +- Boolean attributes are true by presence, so assigning a value does not disable them: [HTML boolean attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes). + +## Additional reproductions already covered by the 5.0 plan + +Avoid counting these as new release workstreams, but add their exact regressions: + +- **Fallback declarations disappear:** `p{display:-webkit-box;display:flex}` retains only flex. Existing Tasks 5–6 already require duplicate declaration preservation. +- **Quoted CSS values can become invalid:** `p{font-family:"A'B"}` serializes to `font-family: 'A'B';`. Task 6 already removes global quote replacement; pin this exact case with a test. +- **HTTP success depends on the reason phrase:** the remote loader checks `statusText === 'OK'`. Existing Task 4 already calls for numeric HTTP status handling. +- **Release gates and root package discovery:** the existing plan already calls out Turbo tasks without workspace scripts, Changesets root inclusion, and the npm `version` lifecycle conflict. Treat these as release gates rather than newly discovered runtime issues. + +## Investigation tickets, not confirmed defects + +| ID | Priority | Investigation | Completion evidence | +| --- | --- | --- | --- | +| B23 | Before 5.0 RC | Bound synchronous processing cost as well as I/O | Benchmark many nodes/selectors, pathological selector shapes, large inline CSS, and template blocks. Network budgets do not bound HTML/CSS size or CPU work. Define supported input sizes and choose additional limits only from measured evidence. | +| B24 | Later, unless regression appears | Audit forwarded Cheerio options | `decodeEntities:false` still normalizes entities in HTML mode; determine which forwarded options are honored by the actual parser. Test case-sensitive tags/attributes, entities, XML mode, CDATA, and fragments; distinguish harmless serialization changes from corruption. Document or remove ineffective options in a major only when justified. | +| B25 | Before 5.0 RC for internal errors; later for new public diagnostics | Separate invalid selectors from internal failures | `lib/inline-css.js` catches every error from `handleRule` as an invalid selector. B18 demonstrates the visibility problem, but a general diagnostics API is new scope. Narrow the catch and test genuine internal errors propagating. Consider optional skipped-selector diagnostics later. | + +## Suggested additions to the existing implementation plan + +1. Extend **Task 1** with B11, B12, B14, B16, B17 and B18 regressions before refactoring. +2. Add one focused shared-template-helper task for **B16–B17**, with direct tests for all three consumers. Avoid three separate fixes that drift apart. +3. Extend **Tasks 5–6** with B18 and narrow internal-error handling from B25. Declaration arrays should eliminate the unsafe property dictionary, but verify it rather than assuming. +4. Extend **Task 7** with B11–B12 and condition-bearing source records. Specify where preserved conditional CSS is emitted after link removal; extraction must retain conditions and source provenance. +5. Add **B14** option validation after extraction reports whether CSS is retained. Update migration documentation with the new rejection and a supported alternative. +6. If time permits, fold **B19–B20** into attribute-helper validation and **B15** into source provenance/URL handling. Otherwise record exact limitations and regression coverage before declaring 5.0 ready. +7. Run **B23** measurements before the RC; use them to decide whether synchronous input budgets are release blockers. Do not equate I/O limits with complete denial-of-service protection. + +## Definition of enough for 5.0 + +- The original ten have passing regressions and independent package-install validation. +- The six required additions above are fixed, covered, and documented. +- No known template corruption or unconditional application of conditional CSS remains in the supported paths. +- Every default change and invalid option combination has a migration example. +- Remaining work is explicitly listed here, with scope and workarounds, rather than silently implied to be supported. +- A release candidate passes realistic email/template fixtures and clean consumer installs; release success is not measured by reaching a ticket total. + +Implementation remains separate from this backlog. No GitHub issues were created and no package files were changed. diff --git a/docs/migration-5.md b/docs/migration-5.md new file mode 100644 index 0000000..7b69134 --- /dev/null +++ b/docs/migration-5.md @@ -0,0 +1,132 @@ +# Migrating to inline-css 5.0 + +5.0 requires Node.js 22.12.0 or later. CommonJS and the Promise API remain available. `options.url` is still required, including inline-only conversions. Corrected CSS output may contain repeated declarations and different ordering; compare rendered behavior rather than exact style strings. + +## Inline-only conversion + +```js +const inlineCss = require('inline-css'); +const result = await inlineCss('Hello
', { + url: 'https://example.com/email.html' +}); +``` + +## Explicit external stylesheet permissions + +External files and HTTP(S) resources are denied by default. A base URL is not a permission grant. Denied requested resources reject the conversion with an Error; they are not silently discarded. + +```js +const result = await inlineCss(html, { + url: 'file:///srv/templates/email.html', + resourcePolicy: { fileRoot: '/srv/templates' } +}); +``` + +The root must be an existing absolute directory. Canonical paths, including symlinks, must remain beneath it. File resources referenced from an HTTP document also need this permission. + +```js +const result = await inlineCss(html, { + url: 'https://example.com/email.html', + resourcePolicy: { allowedOrigins: ['https://cdn.example.com'] } +}); +``` + +Origins are exact, including scheme and nondefault port. Credentials in URLs and unsupported schemes are rejected. Redirect destinations require permission too. Nonpublic IP destinations, including loopback and link-local, are denied even for an allowed origin. Intentional intranet fetching requires both its origin and `allowPrivateNetworks: true`: + +```js +const result = await inlineCss(html, { + url: 'https://intranet.example/email.html', + resourcePolicy: { + allowedOrigins: ['https://intranet.example'], + allowPrivateNetworks: true + } +}); +``` + +Supply permissions only for sources the application intends to trust. These controls do not sanitize HTML or bound every synchronous HTML/CSS computation. + +## Finite resource limits + +Current RC defaults, pending production workload validation: + +| Option | Default | +| --- | --- | +| `timeoutMs` | 10,000 ms for all resource loading, including queued work | +| `maxStylesheetBytes` | 1,048,576 decoded bytes per loaded stylesheet | +| `maxTotalBytes` | 10,485,760 decoded loaded bytes per conversion | +| `maxStylesheets` | 32 loads, counting repeated links | +| `concurrency` | 4 active loads | +| `maxRedirects` | 5 per load | + +Set finite overrides inside `resourcePolicy` for legitimate larger workloads. Positive integers are required except `maxRedirects`, which may be zero. There is no unlimited value; zero is not a way to disable a deadline. Inline/embedded CSS and application-prefetched CSS are outside these loading byte budgets. First failure aborts active loading and cancels queued work. + +## Proxies + +The built-in loader is direct-only. It explicitly ignores HTTP_PROXY, HTTPS_PROXY, ALL_PROXY, NO_PROXY and their lowercase variants without changing application environment variables. It has no custom proxy/agent option in 5.0. + +For proxy-dependent applications, fetch each authorized stylesheet through your application's existing proxy-aware client. Enforce destination, redirect, decoded-byte and deadline controls in that client. Replace each link with the fetched style content at the same document position, retaining its media attribute; resolve relative asset URLs before relocation. Then use: + +```js +const result = await inlineCss(prefetchedHtml, { + url: 'https://example.com/email.html', + applyLinkTags: false, + preserveMediaQueries: true +}); +``` + +The application must construct style elements safely; untrusted CSS must not become executable markup. `extraCss` is appropriate only when applying those declarations after all source styles preserves the intended cascade. Prefetching is application-owned I/O and is not automatically covered by the library's loader budgets. + +## Media and retained selectors + +CSS from `media="print"`, responsive media attributes, or other conditional sources never becomes unconditional inline declarations. Missing, empty and `all` media attributes are unconditional. `preserveMediaQueries: true` retains conditional CSS from embedded styles, fetched stylesheets and `extraCss`; tag-removal flags still control original sources. + +```js +const result = await inlineCss( + '', + { url: 'https://example.com/email.html', preserveMediaQueries: true } +); +``` + +`removeHtmlSelectors: true` rejects when nonempty CSS or a stylesheet link remains, including media queries, `data-embed` or explicitly retained tags. Keep selectors when preserving CSS, or remove that CSS before requesting cleanup. Empty retained style blocks alone do not conflict. + +## Template blocks + +EJS (`<%` / `%>`) and HBS (`{{` / `}}`) remain defaults. Custom definitions extend them. Delimiters are literal nonempty strings rather than regular expressions: + +```js +const result = await inlineCss('[[ name ]] {{fallback}}
', { + url: 'https://example.com/email.html', + codeBlocks: { CUSTOM: { start: '[[', end: ']]' } } +}); +``` + +The earliest opener wins; ties prefer the longest opener, then block-key order. Blocks end at their first closing delimiter and do not nest. Unmatched opening delimiters remain unchanged. Literal placeholder-shaped strings are preserved alongside real blocks. + +## Errors and corrected CSS + +Recognized options are validated; unknown keys are tolerated for wrappers. Rejections use Error objects, not strings. Handle `error.code` rather than matching messages: + +- `ERR_INLINE_CSS_INVALID_OPTIONS` +- `ERR_INLINE_CSS_RESOURCE_DENIED` +- `ERR_INLINE_CSS_RESOURCE_TIMEOUT` +- `ERR_INLINE_CSS_RESOURCE_LIMIT` +- `ERR_INLINE_CSS_SELECTOR_RETENTION_CONFLICT` + +Filesystem, network, parser and other internal errors propagate with their original identity/cause where available. Unsupported selectors retain skip behavior; arbitrary internal failures are not silently classified as selectors. + +Important declarations retain specificity, inline important declarations win appropriately, specificity components compare numerically, and linked/embedded rules retain document order. Shorthand resets and repeated fallback values remain in cascade order. For example, `margin-left:10px; margin:0` now resets the left margin as a browser would. Property names that resemble inherited JavaScript fields no longer hide valid declarations. + +## Independently published packages + +- `css-rules` returns `{selector, declarations}` records; declarations contain `property`, `value`, `important`, and `order`. +- `href-content` becomes `(destHref, sourceHref, options, callback)`. +- `remote-content` becomes `(url, options, callback)`. +- Both loaders require explicit `options.resourcePolicy` permissions for external access. +- `extract-css` keeps its entry signature; its callback adds fourth argument `{hasRetainedCss}` after `(err, html, css)`. Its returned CSS includes only unconditional sources; conditional rules can remain in the returned HTML. Its loaders share one conversion budget. +- Resource policy and template protection are internal helpers bundled inside the existing packages. There are no new helper packages to install, own, or publish. + +Each package receives its own appropriate version. No npm publication or stable promotion occurs merely by running the verification scripts. + +## Building from this repository + +Run `npm run build` before requiring entry points directly from a checkout. `npm test` builds automatically, and normal `npm pack`/`npm publish` hooks rebuild the internal files for the selected existing package. `release:pack` explicitly builds before its lifecycle-disabled packaging checks. Edit `internal/`, not generated `lib/internal/` copies; the copies are ignored by Git and included only in the existing tarballs. diff --git a/docs/proposal-5.0-github-issue.md b/docs/proposal-5.0-github-issue.md new file mode 100644 index 0000000..280f58d --- /dev/null +++ b/docs/proposal-5.0-github-issue.md @@ -0,0 +1,54 @@ +## Proposal + +Release `inline-css` 5.0.0 to improve resource access controls, CSS correctness, template integrity, and runtime compatibility. This proposal covers 16 issues. The maintainer has selected the compatibility direction below; numeric resource limits remain release-candidate defaults pending representative workload validation and RC feedback. + +## Proposed task list + +- [ ] Restrict stylesheet file/network access through explicit permissions, including redirect and destination checks. +- [ ] Update vulnerable dependencies and review remaining security advisories. +- [ ] Bound stylesheet loading with deadlines, byte limits, resource counts, concurrency limits, and cancellation. +- [ ] Preserve selector specificity for `!important` declarations, including existing inline styles. +- [ ] Preserve shorthand/longhand precedence and fallback declarations. +- [ ] Respect document order when combining linked and embedded stylesheets. +- [ ] Align declared Node.js support with the actual runtime requirements. +- [ ] Compare specificity components numerically. +- [ ] Accept empty style elements without errors or repeated callbacks. +- [ ] Parse selector lists correctly around brackets, parentheses, quoted strings, and escapes. +- [ ] Honor media attributes on style and link elements without applying conditional CSS unconditionally. +- [ ] Preserve media queries from linked stylesheets and extra CSS when requested. +- [ ] Prevent selector removal from breaking retained CSS. +- [ ] Treat custom template delimiters as literal strings and validate their definitions. +- [ ] Prevent template placeholder collisions from replacing literal input. +- [ ] Handle inherited object-key names safely so they cannot suppress valid CSS declarations. + +## Proposed compatibility changes + +- Require **Node.js 22.12.0 or later**. +- Deny external stylesheet access by default. Allow file access through an explicit root directory and HTTP(S) access through an explicit origin allowlist. Private-network access requires a separate opt-in. +- Add finite configurable resource limits. RC candidate defaults: a 10-second whole resource-loading deadline, 1 MiB decoded CSS per stylesheet, 10 MiB total loaded CSS, 32 stylesheet loads, four concurrent loads, and five redirects. Values remain provisional pending production workload validation; inline/embedded CSS is outside these loading budgets. +- Keep the built-in loader direct-only: explicitly disable automatic environment proxies. Configurable proxy support is deferred. Proxy-dependent applications can fetch authorized CSS through their own proxy-aware client and supply it as embedded styles in source order, with media conditions preserved and library link fetching disabled. Applications own the controls for that prefetch path. +- Reject `removeHtmlSelectors: true` when CSS or stylesheet links are retained, rather than removing selectors those styles may require. +- Validate recognized options while tolerating unknown keys. Reject invalid known options with `Error` objects and stable error codes, and propagate internal failures. +- Allow corrected CSS output to differ from 4.x, including declaration order and retained fallback declarations. Byte-identical serialization is not a compatibility goal. +- Retain the CommonJS `inlineCss(html, options) -> Promisex
` retains blue, with equivalent own-key cases and unchanged object prototypes. +- [x] Run `npx mocha test/release-5.test.js packages/css-rules/test packages/style-data/test`; record expected failures, then commit the tests. Avoid external network dependencies in new tests. + +### Task 1a: Share literal, collision-free template protection (B16–B17) + +**Files:** `internal/template-blocks.js`, `test/internal/template-blocks.test.js`, `scripts/build-internals.js`; modify `lib/inline-css.js`, `packages/list-stylesheets/index.js`, `packages/style-data/index.js`, their manifests and tests, and the lockfile. Retain existing task numbers for stable references. + +**Consumes/produces:** Implements `protect(html, codeBlocks)` above; each consumer restores the HTML returned by its own parsing stage. Generate package-local copies before packaging; import only relative files inside each published boundary. Do not publish an additional helper package. + +- [x] Write helper tests for literal brackets, parentheses, dollar signs, backslashes, multiline blocks, default/custom block merging, invalid definitions, unmatched openings, and the specified overlapping-delimiter precedence. Assert no caller mutation. +- [x] Test literal token-shaped text and attribute values, tokens inside protected blocks, repeated processing, multiple block kinds, and round-tripping through each of the three consumers. A literal `EXCS_CODE_BLOCK_0_` must remain literal even beside a real template block. +- [x] Run `npx mocha test/internal/template-blocks.test.js test/release-5.test.js` to establish the expected failures, then implement literal scanning and input-checked placeholder generation. Ensure generated markers survive HTML/XML serialization, including attribute-name case normalization, without matching unrelated input text. +- [x] Replace all three duplicate encoder/decoder implementations with the shared helper. Add direct standalone package tests so the root integration path cannot mask a missing migration. +- [x] Run `npx mocha test/internal/template-blocks.test.js packages/list-stylesheets/test packages/style-data/test test/release-5.test.js` and lint. B16/B17 tests must pass; identify unrelated known failures explicitly, then commit. + +### Task 2: Establish runtime and dependency support + +**Files:** `package.json`, `packages/*/package.json`, `package-lock.json`, `.github/workflows/main.yml`, existing remote tests. + +- [x] Set the proposed Node floor consistently, choose current maintained dependency versions compatible with it, and align Cheerio requirements across workspaces. Record exact resolved versions and advisory fixes when implementing; do not copy stale minimum patched versions from the review. +- [x] Update CI to the floor plus Node 22, 24, and 26 current patches, use `npm ci`, and update checkout/setup actions to supported versions. Verify the selected npm tooling runs on the exact floor. +- [x] Replace existing GitHub-hosted stylesheet tests with deterministic local servers and explicit test permissions. Ensure assertions handle request errors rather than dereferencing absent results. +- [x] Run `npm ci`, `npm ls`, `npm audit --json`, and `npm audit --omit=dev --json`. Save findings in implementation notes, separating runtime reachability from development tooling. Address high/critical findings before release; explicitly justify any remaining lower-severity finding. +- [x] Run unaffected tests and lint; distinguish known Task 1 failures from new dependency regressions. Commit manifests, lockfile, CI, and test isolation together. + +### Task 3: Implement explicit resource authorization + +**Files:** `internal/resource-policy.js` and tests; modify `index.js`, `lib/inlineContent.js`, loader files, and their manifests/README files. + +**Consumes/produces:** Implements the policy and loader signatures above. Callers pass the full options object; standalone loaders normalize their own inputs, so calling them directly cannot bypass policy. + +- [x] Write `test/internal/resource-policy.test.js/main.js` and loader regressions: deny all resources with defaults; permit an exact allowlisted public origin; reject off-origin requests, credentials, unsupported schemes, private/loopback/link-local destinations, mixed public/private DNS answers, IPv4-mapped IPv6, and redirects to denied origins. +- [x] Add file tests for an allowed temporary root, traversal, encoded traversal, a sibling with the same path prefix, a symlink to outside the root, and an HTTP base linking to `file:`. Include Windows CI coverage for file containment and URL decoding. +- [x] Add API tests for invalid limits, non-mutated options, missing `url` rejecting with an `Error` before any I/O, disabled link fetching performing no I/O, and direct standalone loader calls enforcing defaults. +- [x] Run focused tests and verify denial tests fail on the original behavior. Implement the proposed interfaces, using a maintained IP classification library with explicit IPv4/IPv6 tests. Pin each connection to authorized DNS results, manually authorize redirect hops, and disable ambient proxies. +- [x] Add direct-loader tests with proxy environment variants and a proxy sentinel; verify `proxy:false` is explicit, no proxy receives traffic, and process environment/global Axios configuration are unchanged. Remove `proxy-from-env` and document the ordered/media-preserving application-prefetch path. Do not add an uncontrolled custom transport bypass. +- [x] Test stable error codes, malformed recognized options, tolerated unknown keys, and cause preservation through both callback and Promise APIs. Internal processing failures must keep their original identity. +- [x] Update existing fixture helpers to provide `fileRoot`; use exact local origins plus `allowPrivateNetworks: true` only in tests requiring a loopback server. Document migration for direct loader consumers. +- [x] Run `npx mocha test/internal/resource-policy.test.js packages/href-content/test packages/remote-content/test test/release-5.test.js` and lint; security tests must pass while remaining known CSS failures stay identified. Commit. + +### Task 4: Bound resource loading and cancellation + +**Files:** Loader files, `packages/extract-css/index.js`, shared policy as needed, loader/extraction tests. + +**Consumes/produces:** Uses Task 3 policy. The extractor owns a per-conversion context containing one deadline, an AbortController, aggregate bytes, and active/queued work; standalone loader calls own a single-load context. Keep this context internal, not a caller-supplied bypass. + +- [ ] Review the recorded workload measurements in `docs/validation/2026-11-09-compatibility-decisions.md`. Obtain sanitized production profiles or privately supplied representative samples, including outlier sizes/counts, slow networks and proxy-only environments. Record the evidence available and gaps; do not claim illustrative public templates establish production compatibility. +- [ ] Replay representative loads with the implemented loader, compare concurrency 1/4/8, and assess the whole resource-loading deadline under queueing, DNS and redirects. Keep numeric defaults provisional until real-workload and RC feedback is reviewed; update proposal, plan, tests and migration examples together if values change. +- [x] Write tests for a never-ending response, slow headers, a stalled body, oversized file/body, compressed expansion, more than 32 links, aggregate bytes above 10 MiB, and a peak of no more than four concurrent loads under defaults. +- [x] Test exact boundaries: 1 MiB succeeds, 1 MiB + 1 byte fails; 32 stylesheet links succeed, 33 fail. Count duplicate links toward resource limits. The total budget counts bytes returned by resource loads, not inline HTML. +- [x] Verify custom limits work; a timeout covers DNS, redirects, queueing, and body reading. Use short test deadlines rather than waiting ten seconds. Assert first failure aborts active work, cancels queued loads, releases sockets, and calls back once. +- [x] Run focused tests to see failures; implement streaming byte checks, a shared deadline, bounded scheduling, and cancellation. Successful HTTP responses use numeric status validation rather than requiring the reason phrase `OK`; force CSS text decoding rather than Axios JSON inference. +- [x] Run loader/extraction tests and lint. Commit. + +### Task 5: Parse selectors and declarations without losing order + +**Files:** `packages/css-rules/{index.js,package.json,README.md}`, parser tests, root consumer adaptations as needed. + +**Consumes/produces:** Produces the proposed `parseCSS(css)` v2 rule records. Choose maintained PostCSS and selector-parser releases compatible with Task 2 runtime support. + +- [x] Extend parser tests to assert separate selectors, preserved declaration order, duplicate fallback values, important flags, quoted/escaped delimiters, and malformed-CSS errors. Preserve the current top-level rule scope: do not flatten media queries into unconditional rules. +- [x] Run parser tests and confirm failures. Replace manual selector splitting with AST selector-list parsing and parse declaration records without CSSOM's duplicate-property collapse. +- [x] Adapt consumers to rule records without temporarily claiming the cascade regressions are fixed. Update the standalone `css-rules` README with its breaking result shape. +- [x] Run `npx mocha packages/css-rules/test` and lint, then commit. Track remaining integration failures explicitly until Task 6. + +### Task 6: Correct cascade precedence and shorthand serialization + +**Files:** `lib/{handleRule,addProps,cssProperty,styleSelector,setStyleAttrs,inline-css,setWidthAttrs}.js`, `test/release-5.test.js`, affected expected fixtures. + +**Consumes/produces:** Consumes Task 5 rules; produces ordered declaration records and valid inline CSS. Existing numeric width attributes must reflect the effective winning width after serialization, not an arbitrary earlier declaration. + +- [x] Add tests for ID/class/type precedence, numeric specificity above nine, inline styles, important specificity, equal-specificity source-order ties, and selector lists where several branches match one element. +- [x] Add B18 tests with `constructor`, `toString`, and `__proto__` declarations before and after valid blue declarations, plus custom properties. Assert valid styling survives, inherited fields are never treated as declarations, and object prototypes remain unchanged. +- [x] Test a deliberately injected internal processing error propagates to the public rejection channel while known invalid/unsupported selectors follow the documented skip behavior. Narrow the existing broad catch; do not introduce a new public diagnostics API. +- [x] Add semantic shorthand tests for margin/padding, border and border sides, background, font, and important shorthand/longhand combinations. Test duplicate fallback declarations, custom properties, quoted strings, and existing inline declarations. Use a real headless browser as a development-only computed-style oracle for supported properties; keep its version pinned and installation reproducible. +- [x] Replace the old specificity calculator with a maintained selector-aware implementation compatible with the selector AST. Normalize to `[inline, id, class, type]` numeric vectors, including `:is`, `:not`, and `:where` semantics where selectors are accepted by Cheerio. +- [x] Sort records by importance, inline origin/specificity, source order, then declaration order. Retain declarations instead of collapsing by property name; serialize low precedence first. Preserve original strings and important flags without globally replacing quote characters. This lets the browser apply shorthand resets and fallbacks in the correct order. +- [x] Remove inherited-key-sensitive property storage throughout collection and serialization; ensure metadata is separate from declaration names. Pin the exact fallback and quoted-value backlog reproductions as part of the existing Task 6 tests. +- [x] Update width extraction to inspect effective serialized width. Keep table-attribute behavior covered by existing tests. Remove obsolete comparison/serialization code only after callers are migrated. +- [x] Run `npx mocha test` and the new browser semantic checks. Cascade and B18 regressions must pass; source-order, conditional-media and retained-selector cases remain assigned to Task 7. Review fixture changes by computed effect, then commit. + +### Task 7: Preserve source order and conditions; guard retained selectors (B11, B12, B14) + +**Files:** `packages/{extract-css,list-stylesheets,style-data,mediaquery-text}/index.js`, their tests, `lib/inlineContent.js`, `lib/inline-css.js`, `lib/removeClassId.js`, root integration fixtures. + +**Consumes/produces:** Implements condition-bearing source records, retained-style emission, and the additive `{hasRetainedCss}` callback metadata described above. Root validates B14 before selector cleanup. Use Task 1a protection and Task 4 bounded loading. + +- [x] Test link/style/link interleaving, reverse network completion order, repeated links, `extraCss` last, and equal-specificity ties. Reuse Task 4 scheduling; completion order must never determine cascade order. +- [x] Test all apply/remove combinations, `data-embed`, template blocks around tags, XML mode, and media preservation. Ordered enumeration must not accidentally inline embedded-only content or create additional fetches. +- [x] Add B11 tests for missing, empty, `all`, `print`, `screen`, and responsive media attributes on both style and link elements. Assert only unconditional CSS is inlined; with preservation enabled, conditions and source order survive, including media rules inside a media-qualified source. +- [x] Add B12 tests for linked and `extraCss` media rules, mixed unconditional/conditional CSS, reversed load completion, retained source tags, removal enabled/disabled, and preservation enabled/disabled. Assert one retained copy per source, and no added network requests for retained links whose apply flag is false. +- [x] Add B14 tests rejecting selector stripping for retained media, nonempty `data-embed`, explicitly retained styles and stylesheet links, and selectors depending on ancestor classes/IDs. Accept cleanup when no CSS is retained, including removed conditional CSS with preservation disabled and empty retained style blocks. Verify rejection occurs before cleanup and is an `Error`. +- [x] Run focused tests to confirm failures. Implement ordered source enumeration and condition-aware partitioning before tag removal; emit preserved conditional styles according to the placement contract above, and provide accurate retained-CSS metadata. +- [x] Implement the B14 root guard using extraction metadata before invoking the inliner. For standalone extraction consumers, expose metadata without changing their selector behavior. For direct internal inliner tests, assert retained CSS cannot be destroyed by unchecked cleanup. +- [x] Run root and extraction/list/style tests, then commit. + +### Task 8: Accept empty styles and settle once + +**Files:** `packages/style-data/index.js`, `packages/style-data/test/main.js`, root integration tests. + +- [x] Test empty and whitespace-only `').text(retained)); + } + } else { + source.el.remove(); + } +}; +const serializeSource = (source, { $, options, css }) => { + let retained = ''; - async.parallel(tasks, (err, results) => { - let stylesData; - let css; + if (source.apply) { + const text = source.css || ''; - if (err) { - return callback(err); + if (!conditional(source.media)) { + css.push(text); } + } + retained = retainedCss(source, options); + if (source.remove) { + replaceSource(source, retained, $); + } +}; - stylesData = results.shift(); +const appendExtraMedia = ($, options, extraCss) => { + if (options.preserveMediaQueries && extraCss.trim()) { + const retained = mediaRules(extraCss); - results.forEach(content => { - stylesData.css.push(content); - }); - css = stylesData.css.join('\n'); + if (retained) { + const parent = options.xmlMode ? $.root().children().first() : $('head'); - return callback(null, stylesData.html, css); - }); + parent.append($('').text(retained)); + } + } +}; +const appendExtraCss = ($, options, css) => { + const extraCss = options.extraCss || ''; + + if (extraCss) { + css.push(extraCss); + } + appendExtraMedia($, options, extraCss); +}; + +module.exports = (html, options, callback) => { + const run = async () => { + const policy = normalizePolicy(options.resourcePolicy); + const protectedHtml = protect(html, options.codeBlocks); + const $ = cheerio.load(protectedHtml.html, options); + const context = createContext(policy); + + try { + const sources = collectSources($, options); + + await loadSources(sources, options, context); + const css = []; + + for (const source of sources) { + serializeSource(source, { $, options, css }); + } + appendExtraCss($, options, css); + const hasRetainedCss = $('style').toArray().some(el => ($(el).html() || '').trim()) || $('link').toArray().some(el => stylesheet($(el))); + + return { html: protectedHtml.restore($.html()), css: protectedHtml.restore(css.join('\n')), metadata: { hasRetainedCss } }; + } finally { + context.close(); + } + }; + + Promise.resolve().then(run).then(result => callback(null, result.html, result.css, result.metadata), cause => callback(cause)); }; diff --git a/packages/extract-css/package.json b/packages/extract-css/package.json index 622f7ec..d0fb497 100644 --- a/packages/extract-css/package.json +++ b/packages/extract-css/package.json @@ -1,10 +1,11 @@ { "name": "extract-css", - "version": "3.0.2", + "version": "4.0.0-rc.1", "description": "Extract the CSS from an HTML document.", "main": "index.js", "files": [ - "index.js" + "index.js", + "lib/internal/" ], "repository": "https://github.com/jonkemp/inline-css/tree/master/packages/extract-css", "keywords": [ @@ -15,8 +16,17 @@ "homepage": "https://github.com/jonkemp/inline-css/tree/master/packages/extract-css", "dependencies": { "async": "^3.2.6", - "href-content": "^2.0.3", - "list-stylesheets": "^2.0.1", - "style-data": "^2.0.1" + "href-content": "^3.0.0-rc.1", + "list-stylesheets": "^3.0.0-rc.1", + "style-data": "^3.0.0-rc.1", + "cheerio": "^1.2.0", + "postcss": "^8.5.32", + "ipaddr.js": "^2.5.0" + }, + "engines": { + "node": ">=22.12.0" + }, + "scripts": { + "prepack": "node ../../scripts/build-internals.js extract-css" } } diff --git a/packages/extract-css/test/main.js b/packages/extract-css/test/main.js index 4e57a53..3297a84 100644 --- a/packages/extract-css/test/main.js +++ b/packages/extract-css/test/main.js @@ -20,12 +20,16 @@ function compare(fixturePath, expectedHTML, expectedCSS, options, done) { const file = getFile(fixturePath); options.url = `file://${file.path}`; + options.resourcePolicy = { fileRoot: path.resolve(__dirname, "fixtures") }; extractCss(file.contents.toString('utf8'), options, (err, html, css) => { + if (err) return done(err); + try { beautify(html).should.be.equal(beautify(String(fs.readFileSync(expectedHTML)))); - css.should.be.equal(String(fs.readFileSync(expectedCSS))); + css.replace(/\r\n/g, '\n').should.be.equal(String(fs.readFileSync(expectedCSS)).replace(/\r\n/g, '\n')); done(); + } catch (cause) { done(cause); } }); } diff --git a/packages/href-content/CHANGELOG.md b/packages/href-content/CHANGELOG.md index 98e6fc8..6bd8617 100644 --- a/packages/href-content/CHANGELOG.md +++ b/packages/href-content/CHANGELOG.md @@ -1,5 +1,16 @@ # href-content +## 3.0.0-rc.1 + +### Major Changes + +- Require Node 22.12+, enforce explicit bounded stylesheet permissions, preserve CSS cascade and media conditions, and protect literal template blocks without collisions. Update standalone loader and parser contracts. See the root migration guide for compatibility changes. The root inline-css package is versioned explicitly because it is outside the workspace package list. + +### Patch Changes + +- Updated dependencies + - remote-content@5.0.0-rc.1 + ## 2.0.3 ### Patch Changes diff --git a/packages/href-content/README.md b/packages/href-content/README.md index ce8422d..8fce1f6 100644 --- a/packages/href-content/README.md +++ b/packages/href-content/README.md @@ -17,7 +17,9 @@ npm install --save href-content ```js var getHrefContent = require('href-content'); -getHrefContent('path/to/css', 'file://path/to/html', function (err, css) { +getHrefContent('styles.css', 'file:///srv/templates/email.html', { + resourcePolicy: { fileRoot: '/srv/templates' } +}, function (err, css) { console.log(css); }); ``` @@ -29,3 +31,9 @@ The code for this module was originally taken from the [Juice](https://github.co ## License MIT + +## 5.0 release family + +Requires Node.js 22.12 or later. See the [inline-css migration guide](https://github.com/jonkemp/inline-css/blob/master/docs/migration-5.md) for the release policy and error codes. + +The callback signature is now `getHrefContent(destHref, sourceHref, options, callback)`. Use `options.resourcePolicy.fileRoot` for file access or exact `allowedOrigins` for HTTP(S). Access is denied by default. diff --git a/packages/href-content/index.js b/packages/href-content/index.js index a508778..344f769 100644 --- a/packages/href-content/index.js +++ b/packages/href-content/index.js @@ -1,20 +1,49 @@ -const url = require('url'); -const fs = require('fs'); +const fs = require('node:fs'); +const { normalizePolicy, authorizeFile, createContext, CONTEXT } = require('./lib/internal/resource-policy'); const getRemoteContent = require('remote-content'); -module.exports = (destHref, sourceHref, callback) => { - let resolvedUrl; - let parsedUrl; - let toUrl = destHref; +const resolveUrl = (destHref, sourceHref) => new URL(destHref, URL.canParse(sourceHref) ? sourceHref : undefined); +const readFile = async (url, policy, context) => { + const target = await authorizeFile(url, policy); - if (url.parse(sourceHref).protocol === 'file:' && destHref[0] === '/') { - toUrl = destHref.slice(1); - } - resolvedUrl = url.resolve(sourceHref, toUrl); - parsedUrl = url.parse(resolvedUrl); - if (parsedUrl.protocol === 'file:') { - fs.readFile(decodeURIComponent(parsedUrl.pathname), 'utf8', callback); - } else { - getRemoteContent(resolvedUrl, callback); + context.check(); + const chunks = []; + let bytes = 0; + const stream = fs.createReadStream(target, { signal: context.signal }); + + for await (const chunk of stream) { + bytes += chunk.length; + context.charge(chunk.length, bytes); + chunks.push(chunk); } + context.check(); + return Buffer.concat(chunks).toString('utf8'); +}; + +const failureReason = (context, cause) => context.signal.aborted ? context.signal.reason : cause; + +module.exports = (destHref, sourceHref, ...[ options, callback ]) => { + const run = async () => { + const policy = normalizePolicy(options.resourcePolicy); + const ownContext = !options[CONTEXT]; + const context = options[CONTEXT] || createContext(policy); + + try { + context.check(); + const url = resolveUrl(destHref, sourceHref); + + if (url.protocol !== 'file:') { + return await new Promise((resolve, reject) => getRemoteContent(url.href, { ...options, [CONTEXT]: context }, (err, value) => err ? reject(err) : resolve(value))); + } + return await readFile(url, policy, context); + } catch (cause) { + throw failureReason(context, cause); + } finally { + if (ownContext) { + context.close(); + } + } + }; + + Promise.resolve().then(run).then(value => callback(null, value), cause => callback(cause)); }; diff --git a/packages/href-content/package.json b/packages/href-content/package.json index 75ab100..438e928 100644 --- a/packages/href-content/package.json +++ b/packages/href-content/package.json @@ -1,10 +1,11 @@ { "name": "href-content", - "version": "2.0.3", + "version": "3.0.0-rc.1", "description": "Get content from link tags in an HTML document.", "main": "index.js", "files": [ - "index.js" + "index.js", + "lib/internal/" ], "repository": "https://github.com/jonkemp/inline-css/tree/master/packages/href-content", "keywords": [ @@ -17,6 +18,13 @@ "license": "MIT", "homepage": "https://github.com/jonkemp/inline-css/tree/master/packages/href-content", "dependencies": { - "remote-content": "^4.0.0" + "remote-content": "^5.0.0-rc.1", + "ipaddr.js": "^2.5.0" + }, + "engines": { + "node": ">=22.12.0" + }, + "scripts": { + "prepack": "node ../../scripts/build-internals.js href-content" } } diff --git a/packages/href-content/test/main.js b/packages/href-content/test/main.js index 774c529..75ad7b6 100644 --- a/packages/href-content/test/main.js +++ b/packages/href-content/test/main.js @@ -22,7 +22,8 @@ function compare(fixturePath, expectedPath, options, done) { const url = `file://${file.path}`; - getHrefContent(data.hrefs[0], url, (err, css) => { + getHrefContent(data.hrefs[0], url, { resourcePolicy: { fileRoot: path.resolve("test/fixtures") } }, (err, css) => { + if (err) return done(err); css.should.be.equal(String(fs.readFileSync(expectedPath))); done(); diff --git a/packages/list-stylesheets/CHANGELOG.md b/packages/list-stylesheets/CHANGELOG.md index e9ba5b8..ea22359 100644 --- a/packages/list-stylesheets/CHANGELOG.md +++ b/packages/list-stylesheets/CHANGELOG.md @@ -1,5 +1,15 @@ # list-stylesheets +## 3.0.0-rc.1 + +### Major Changes + +- Require Node 22.12+, enforce explicit bounded stylesheet permissions, preserve CSS cascade and media conditions, and protect literal template blocks without collisions. Update standalone loader and parser contracts. See the root migration guide for compatibility changes. The root inline-css package is versioned explicitly because it is outside the workspace package list. + +### Patch Changes + +- Updated dependencies + ## 2.0.2 ### Patch Changes diff --git a/packages/list-stylesheets/README.md b/packages/list-stylesheets/README.md index c352e10..a0f46cb 100644 --- a/packages/list-stylesheets/README.md +++ b/packages/list-stylesheets/README.md @@ -59,3 +59,7 @@ The code for this module was originally taken from the [Juice](https://github.co ## License MIT © [Jonathan Kemp](http://jonkemp.com) + +## 5.0 release family + +Requires Node.js 22.12 or later. See the [inline-css migration guide](https://github.com/jonkemp/inline-css/blob/master/docs/migration-5.md) for the release policy and error codes. diff --git a/packages/list-stylesheets/index.js b/packages/list-stylesheets/index.js index 699a90e..523b745 100644 --- a/packages/list-stylesheets/index.js +++ b/packages/list-stylesheets/index.js @@ -1,47 +1,15 @@ const cheerio = require('cheerio'); const pick = require('pick-util'); -function replaceCodeBlock(html, re, block) { - return html.replace(re, () => block); -} +const { protect } = require('./lib/internal/template-blocks'); module.exports = (html, options) => { const results = {}; - const codeBlocks = { - EJS: { start: '<%', end: '%>' }, - HBS: { start: '{{', end: '}}' } - }; - const codeBlockLookup = []; - const encodeCodeBlocks = _html => { - let __html = _html; - const blocks = Object.assign(codeBlocks, options.codeBlocks); - - Object.keys(blocks).forEach(key => { - const re = new RegExp(blocks[key].start + '([\\S\\s]*?)' + blocks[key].end, 'g'); - - __html = __html.replace(re, match => { - codeBlockLookup.push(match); - return 'EXCS_CODE_BLOCK_' + (codeBlockLookup.length - 1) + '_'; - }); - }); - return __html; - }; - const decodeCodeBlocks = _html => { - let index; - let re; - let __html = _html; - - for (index = 0; index < codeBlockLookup.length; index++) { - re = new RegExp('EXCS_CODE_BLOCK_' + index + '_(="")?', 'gi'); - __html = replaceCodeBlock(__html, re, codeBlockLookup[index]); - } - return __html; - }; - const encodeEntities = _html => encodeCodeBlocks(_html); - const decodeEntities = _html => decodeCodeBlocks(_html); + const protectedHtml = protect(html, options.codeBlocks); + const decodeEntities = protectedHtml.restore; let $; - $ = cheerio.load(encodeEntities(html), Object.assign({ + $ = cheerio.load(protectedHtml.html, Object.assign({ decodeEntities: false }, pick(options, [ 'xmlMode', diff --git a/packages/list-stylesheets/package.json b/packages/list-stylesheets/package.json index 124ea33..38a5e3a 100644 --- a/packages/list-stylesheets/package.json +++ b/packages/list-stylesheets/package.json @@ -1,10 +1,11 @@ { "name": "list-stylesheets", - "version": "2.0.2", + "version": "3.0.0-rc.1", "description": "Get a list of stylesheets from an HTML document.", "main": "index.js", "files": [ - "index.js" + "index.js", + "lib/internal/" ], "repository": "https://github.com/jonkemp/inline-css/tree/master/packages/list-stylesheets", "keywords": [ @@ -14,7 +15,13 @@ "license": "MIT", "homepage": "https://github.com/jonkemp/inline-css/tree/master/packages/list-stylesheets", "dependencies": { - "cheerio": "1.0.0", + "cheerio": "^1.2.0", "pick-util": "^1.1.5" + }, + "engines": { + "node": ">=22.12.0" + }, + "scripts": { + "prepack": "node ../../scripts/build-internals.js list-stylesheets" } } diff --git a/packages/mediaquery-text/CHANGELOG.md b/packages/mediaquery-text/CHANGELOG.md new file mode 100644 index 0000000..4ea808c --- /dev/null +++ b/packages/mediaquery-text/CHANGELOG.md @@ -0,0 +1,7 @@ +# mediaquery-text + +## 2.0.0-rc.1 + +### Major Changes + +- Require Node 22.12+, enforce explicit bounded stylesheet permissions, preserve CSS cascade and media conditions, and protect literal template blocks without collisions. Update standalone loader and parser contracts. See the root migration guide for compatibility changes. The root inline-css package is versioned explicitly because it is outside the workspace package list. diff --git a/packages/mediaquery-text/README.md b/packages/mediaquery-text/README.md index 57b4485..655c825 100644 --- a/packages/mediaquery-text/README.md +++ b/packages/mediaquery-text/README.md @@ -29,3 +29,7 @@ The code for this module was originally taken from the [Juice](https://github.co ## License MIT + +## 5.0 release family + +Requires Node.js 22.12 or later. See the [inline-css migration guide](https://github.com/jonkemp/inline-css/blob/master/docs/migration-5.md) for the release policy and error codes. diff --git a/packages/mediaquery-text/index.js b/packages/mediaquery-text/index.js index 90a68fb..bf8c5bb 100644 --- a/packages/mediaquery-text/index.js +++ b/packages/mediaquery-text/index.js @@ -2,58 +2,27 @@ const cssom = require('cssom'); const os = require('os'); const CSSFontFaceRule = require('cssom/lib/CSSFontFaceRule').CSSFontFaceRule; -/** - * Returns Media Query text for a CSS source. - * - * @param {String} css source - * @api public - */ - -module.exports = css => { - const rules = cssom.parse(css).cssRules || []; - const queries = []; - let queryMedia; - let queryString; - let style; - let property; - let value; - let important; - let result; - - rules.forEach(({ type, media, cssRules }) => { - /* CSS types - STYLE: 1, - IMPORT: 3, - MEDIA: 4, - FONT_FACE: 5, - */ - - if (type === cssom.CSSMediaRule.prototype.type) { - queryMedia = Array.prototype.slice.call(media).join(', '); - queryString = []; - - queryString.push(`${os.EOL}@media ${queryMedia} {`); - - cssRules.forEach(rule => { - if (rule.type === cssom.CSSStyleRule.prototype.type || rule.type === CSSFontFaceRule.prototype.type) { - queryString.push(` ${rule.type === cssom.CSSStyleRule.prototype.type ? rule.selectorText : '@font-face'} {`); - - for (style = 0; style < rule.style.length; style++) { - property = rule.style[style]; - value = rule.style[property]; - important = rule.style._importants[property] ? ' !important' : ''; - queryString.push(` ${property}: ${value}${important};`); - } - queryString.push(' }'); - } - }); - - queryString.push('}'); - result = queryString.length ? queryString.join(os.EOL) + os.EOL : ''; - - queries.push(result); - } - }); +const supportedRule = rule => rule.type === cssom.CSSStyleRule.prototype.type || rule.type === CSSFontFaceRule.prototype.type; +const renderRule = rule => { + const lines = [ ` ${rule.type === cssom.CSSStyleRule.prototype.type ? rule.selectorText : '@font-face'} {` ]; + + for (let index = 0; index < rule.style.length; index++) { + const property = rule.style[index]; + const important = rule.style._importants[property] ? ' !important' : ''; + + lines.push(` ${property}: ${rule.style[property]}${important};`); + } + lines.push(' }'); + return lines.join(os.EOL); +}; +const renderMedia = ({ media, cssRules }) => { + const queryMedia = Array.prototype.slice.call(media).join(', '); + const lines = [ `${os.EOL}@media ${queryMedia} {`, ...cssRules.filter(supportedRule).map(renderRule), '}' ]; - return queries.join(os.EOL); + return lines.join(os.EOL) + os.EOL; }; + +/** Returns Media Query text for a CSS source. */ +module.exports = css => (cssom.parse(css).cssRules || []) + .filter(rule => rule.type === cssom.CSSMediaRule.prototype.type) + .map(renderMedia).join(os.EOL); diff --git a/packages/mediaquery-text/package.json b/packages/mediaquery-text/package.json index 97077e7..71952e9 100644 --- a/packages/mediaquery-text/package.json +++ b/packages/mediaquery-text/package.json @@ -1,6 +1,6 @@ { "name": "mediaquery-text", - "version": "1.2.0", + "version": "2.0.0-rc.1", "description": "Returns Media Query text for a CSS source.", "main": "index.js", "files": [ @@ -18,5 +18,8 @@ "homepage": "https://github.com/jonkemp/inline-css/tree/master/packages/mediaquery-text", "dependencies": { "cssom": "^0.5.0" + }, + "engines": { + "node": ">=22.12.0" } } diff --git a/packages/mediaquery-text/test/main.js b/packages/mediaquery-text/test/main.js index 104bb9d..b572be2 100644 --- a/packages/mediaquery-text/test/main.js +++ b/packages/mediaquery-text/test/main.js @@ -10,8 +10,8 @@ describe('mediaquery-text', () => { const mediaQueries = mediaQueryText( String(fs.readFileSync(path.join(__dirname, 'fixtures', 'file.css'))) ); - mediaQueries.should.be.equal( - String(fs.readFileSync(path.join(__dirname, 'expected', 'file.css'))) + mediaQueries.replace(/\r\n/g, '\n').should.be.equal( + String(fs.readFileSync(path.join(__dirname, 'expected', 'file.css'))).replace(/\r\n/g, '\n') ); done(); }); @@ -20,8 +20,8 @@ describe('mediaquery-text', () => { const mediaQueries = mediaQueryText( String(fs.readFileSync(path.join(__dirname, 'fixtures', 'font-face.css'))) ); - mediaQueries.should.be.equal( - String(fs.readFileSync(path.join(__dirname, 'expected', 'font-face.css'))) + mediaQueries.replace(/\r\n/g, '\n').should.be.equal( + String(fs.readFileSync(path.join(__dirname, 'expected', 'font-face.css'))).replace(/\r\n/g, '\n') ); done(); }); diff --git a/packages/remote-content/CHANGELOG.md b/packages/remote-content/CHANGELOG.md index 918cc19..bf7b6a7 100644 --- a/packages/remote-content/CHANGELOG.md +++ b/packages/remote-content/CHANGELOG.md @@ -1,5 +1,15 @@ # remote-content +## 5.0.0-rc.1 + +### Major Changes + +- Require Node 22.12+, enforce explicit bounded stylesheet permissions, preserve CSS cascade and media conditions, and protect literal template blocks without collisions. Update standalone loader and parser contracts. See the root migration guide for compatibility changes. The root inline-css package is versioned explicitly because it is outside the workspace package list. + +### Patch Changes + +- Updated dependencies + ## 4.0.1 ### Patch Changes diff --git a/packages/remote-content/README.md b/packages/remote-content/README.md index 7dc3c74..5775571 100644 --- a/packages/remote-content/README.md +++ b/packages/remote-content/README.md @@ -17,7 +17,9 @@ npm install --save remote-content ```js var getRemoteContent = require('remote-content'); -getRemoteContent(remotePath, function (err, contents) { +getRemoteContent('https://example.com/styles.css', { + resourcePolicy: { allowedOrigins: ['https://example.com'] } +}, function (err, contents) { console.log(contents); }); ``` @@ -29,3 +31,9 @@ The code for this module was originally taken from the [Juice](https://github.co ## License MIT + +## 5.0 release family + +Requires Node.js 22.12 or later. See the [inline-css migration guide](https://github.com/jonkemp/inline-css/blob/master/docs/migration-5.md) for the release policy and error codes. + +The callback signature is now `getRemoteContent(url, options, callback)`. Explicit `options.resourcePolicy.allowedOrigins` is required; private addresses require `allowPrivateNetworks: true` as well. Loading is direct-only, bounded and cancellable. Environment proxies are ignored. diff --git a/packages/remote-content/index.js b/packages/remote-content/index.js index b9bfadd..2471f71 100644 --- a/packages/remote-content/index.js +++ b/packages/remote-content/index.js @@ -1,27 +1,78 @@ +const http = require('node:http'); +const https = require('node:https'); const axios = require('axios'); -const getProxyForUrl = require('proxy-from-env').getProxyForUrl; +const { normalizePolicy, authorizeUrl, createLookup, createContext, CONTEXT, error } = require('./lib/internal/resource-policy'); -module.exports = (remoteUrl, callback) => { - const axiosConfig = {}; - const proxyUrl = getProxyForUrl(remoteUrl); +const readBody = async (stream, context) => { + const chunks = []; + let bytes = 0; - if (proxyUrl) { - const proxyUrlData = new URL(proxyUrl); + for await (const chunk of stream) { + bytes += chunk.length; + context.charge(chunk.length, bytes); + chunks.push(chunk); + } + context.check(); + return Buffer.concat(chunks).toString('utf8'); +}; +const redirectTarget = (response, { url, redirects, policy }) => { + if (![ 301, 302, 303, 307, 308 ].includes(response.status) || !response.headers.location) { + return null; + } + response.data.destroy(); + if (redirects >= policy.maxRedirects) { + throw error('RESOURCE_LIMIT', 'Stylesheet redirect budget exceeded'); + } + return new URL(response.headers.location, url).href; +}; +const validateStatus = response => { + if (response.status < 200 || response.status >= 300) { + response.data.destroy(); + throw new Error(`Stylesheet HTTP status ${response.status}`); + } +}; - axiosConfig.proxy = { - protocol: proxyUrlData.protocol.replace(':', ''), - host: proxyUrlData.hostname, - port: proxyUrlData.port, - }; +const fetchCss = async (remoteUrl, { client, context, policy }) => { + let target = remoteUrl; + + for (let redirects = 0; ; redirects++) { + context.check(); + const url = authorizeUrl(target, policy); + const response = await client.get(url.href, { signal: context.signal }); + + const redirect = redirectTarget(response, { url, redirects, policy }); + + if (redirect) { + target = redirect; + continue; + } + validateStatus(response); + return await readBody(response.data, context); } +}; +const failureReason = (context, cause) => context.signal.aborted ? context.signal.reason : cause; + +module.exports = (remoteUrl, options, callback) => { + const run = async () => { + const policy = normalizePolicy(options.resourcePolicy); + const ownContext = !options[CONTEXT]; + const context = options[CONTEXT] || createContext(policy); + const httpAgent = new http.Agent({ lookup: createLookup(policy), keepAlive: false }); + const httpsAgent = new https.Agent({ lookup: createLookup(policy), keepAlive: false }); + const client = new axios.Axios({ adapter: axios.getAdapter('http'), proxy: false, httpAgent, httpsAgent, maxRedirects: 0, responseType: 'stream', decompress: true, headers: { Accept: 'text/css,*/*;q=0.1' }, validateStatus: () => true }); - axios.get(remoteUrl, axiosConfig).then(response => { - if (response.statusText === 'OK') { - return callback(null, response.data); + try { + return await fetchCss(remoteUrl, { client, context, policy }); + } catch (cause) { + throw failureReason(context, cause); + } finally { + httpAgent.destroy(); + httpsAgent.destroy(); + if (ownContext) { + context.close(); + } } + }; - return callback(new Error(`GET ${remoteUrl} ${response.status}`)); - }).catch(error => { - callback(error); - }); + Promise.resolve().then(run).then(value => callback(null, value), cause => callback(cause)); }; diff --git a/packages/remote-content/package.json b/packages/remote-content/package.json index 5fb4e53..1b3e14f 100644 --- a/packages/remote-content/package.json +++ b/packages/remote-content/package.json @@ -1,10 +1,11 @@ { "name": "remote-content", - "version": "4.0.1", + "version": "5.0.0-rc.1", "description": "Get remote content.", "main": "index.js", "files": [ - "index.js" + "index.js", + "lib/internal/" ], "repository": "https://github.com/jonkemp/inline-css/tree/master/packages/remote-content", "keywords": [ @@ -20,7 +21,13 @@ "license": "MIT", "homepage": "https://github.com/jonkemp/inline-css/tree/master/packages/remote-content", "dependencies": { - "axios": "^1.7.9", - "proxy-from-env": "^1.1.0" + "axios": "^1.21.1", + "ipaddr.js": "^2.5.0" + }, + "engines": { + "node": ">=22.12.0" + }, + "scripts": { + "prepack": "node ../../scripts/build-internals.js remote-content" } } diff --git a/packages/remote-content/test/main.js b/packages/remote-content/test/main.js index 9e8c649..f45c872 100644 --- a/packages/remote-content/test/main.js +++ b/packages/remote-content/test/main.js @@ -1,22 +1,15 @@ -/*eslint-disable */ - -const should = require('should'); -const fs = require('fs'); -const path = require('path'); -const getRemoteContent = require('../index'); - -function compare(remotePath, fixturePath, done) { - getRemoteContent(remotePath, (err, css) => { - css.should.be.equal(String(fs.readFileSync(fixturePath))); - - done(); - }); -} - -describe('remote-content', function() { - this.timeout(15000); - - it('Should get remote content from link tags in an HTML document', done => { - compare('https://raw.githubusercontent.com/jonkemp/remote-content/master/test/fixtures/file.css', path.join('test', 'fixtures', 'file.css'), done); +/* eslint-disable */ +const assert = require('node:assert/strict'); +const http = require('node:http'); +const getRemoteContent = require('../'); +describe('remote-content', () => { + it('loads authorized text from a local server', async () => { + const server = http.createServer((req,res)=>res.end('p{color:red}')); + await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve)); + const origin=`http://127.0.0.1:${server.address().port}`; + try { + const css=await new Promise((resolve,reject)=>getRemoteContent(`${origin}/style.css`,{resourcePolicy:{allowedOrigins:[origin],allowPrivateNetworks:true}},(err,value)=>err?reject(err):resolve(value))); + assert.equal(css,'p{color:red}'); + } finally { server.closeAllConnections(); await new Promise(resolve=>server.close(resolve)); } }); }); diff --git a/packages/style-data/CHANGELOG.md b/packages/style-data/CHANGELOG.md index 2a9c72e..b7d68c6 100644 --- a/packages/style-data/CHANGELOG.md +++ b/packages/style-data/CHANGELOG.md @@ -1,5 +1,16 @@ # style-data +## 3.0.0-rc.1 + +### Major Changes + +- Require Node 22.12+, enforce explicit bounded stylesheet permissions, preserve CSS cascade and media conditions, and protect literal template blocks without collisions. Update standalone loader and parser contracts. See the root migration guide for compatibility changes. The root inline-css package is versioned explicitly because it is outside the workspace package list. + +### Patch Changes + +- Updated dependencies + - mediaquery-text@2.0.0-rc.1 + ## 2.0.2 ### Patch Changes diff --git a/packages/style-data/README.md b/packages/style-data/README.md index 5ecc553..b49290c 100644 --- a/packages/style-data/README.md +++ b/packages/style-data/README.md @@ -74,3 +74,7 @@ The code for this module was originally taken from the [Juice](https://github.co ## License MIT © [Jonathan Kemp](http://jonkemp.com) + +## 5.0 release family + +Requires Node.js 22.12 or later. See the [inline-css migration guide](https://github.com/jonkemp/inline-css/blob/master/docs/migration-5.md) for the release policy and error codes. diff --git a/packages/style-data/index.js b/packages/style-data/index.js index 02a9b52..c854aa5 100644 --- a/packages/style-data/index.js +++ b/packages/style-data/index.js @@ -2,54 +2,55 @@ const mediaQueryText = require('mediaquery-text'); const cheerio = require('cheerio'); const pick = require('pick-util'); -function replaceCodeBlock(html, re, block) { - return html.replace(re, () => block); -} - -module.exports = (html, options, callback) => { - const results = {}; - - const codeBlocks = { - EJS: { start: '<%', end: '%>' }, - HBS: { start: '{{', end: '}}' } - }; - - const codeBlockLookup = []; - - const encodeCodeBlocks = _html => { - let __html = _html; - const blocks = Object.assign(codeBlocks, options.codeBlocks); - - Object.keys(blocks).forEach(key => { - const re = new RegExp(`${blocks[key].start}([\\S\\s]*?)${blocks[key].end}`, 'g'); - - __html = __html.replace(re, match => { - codeBlockLookup.push(match); - return `EXCS_CODE_BLOCK_${codeBlockLookup.length - 1}_`; - }); - }); - return __html; - }; +const { protect } = require('./lib/internal/template-blocks'); + +const qualifiedMedia = media => media && media.trim() && media.trim().toLowerCase() !== 'all'; +const wrapMedia = (styleData, media) => qualifiedMedia(media) ? `@media ${media} {\n${styleData}\n}` : styleData; +const removeStyle = (el, styleData, options) => { + let mediaQueries; + + if (options.preserveMediaQueries) { + const media = el.attr('media'); + + mediaQueries = qualifiedMedia(media) ? wrapMedia(styleData, media) : mediaQueryText(styleData); + el.removeAttr('media').text(mediaQueries); + } + if (!mediaQueries) { + el.remove(); + } +}; +const collectStyle = (el, styleData, { options, results, decodeEntities }) => { + if (options.applyStyleTags) { + results.css.push(decodeEntities(wrapMedia(styleData, el.attr('media')))); + } + if (options.removeStyleTags) { + removeStyle(el, styleData, options); + } +}; - const decodeCodeBlocks = _html => { - let index; - let re; - let __html = _html; +const processStyle = (el, { options, results, decodeEntities }) => { + // Embedded styles are intentionally preserved without applying them. + if (typeof el.data('embed') !== 'undefined') { + return; + } + const styleData = el.html() || ''; - for (index = 0; index < codeBlockLookup.length; index++) { - re = new RegExp(`EXCS_CODE_BLOCK_${index}_(="")?`, 'gi'); - __html = replaceCodeBlock(__html, re, codeBlockLookup[index]); + if (!styleData.trim()) { + if (options.removeStyleTags) { + el.remove(); } - return __html; - }; + return; + } + collectStyle(el, styleData, { options, results, decodeEntities }); +}; +const extract = (html, options) => { + const results = {}; - const encodeEntities = _html => encodeCodeBlocks(_html); - const decodeEntities = _html => decodeCodeBlocks(_html); + const protectedHtml = protect(html, options.codeBlocks); + const decodeEntities = protectedHtml.restore; let $; - let styleDataList; - let styleData; - $ = cheerio.load(encodeEntities(html), Object.assign({ + $ = cheerio.load(protectedHtml.html, Object.assign({ decodeEntities: false }, pick(options, [ 'xmlMode', @@ -62,35 +63,21 @@ module.exports = (html, options, callback) => { results.css = []; - $('style').each((index, element) => { - let mediaQueries; - - // if data-embed property exists, skip inlining and removing - if (typeof $(element).data('embed') !== 'undefined') { - return; - } - - styleDataList = element.childNodes; - if (styleDataList.length !== 1) { - callback(new Error('empty style element')); - return; - } - styleData = styleDataList[0].data; - if (options.applyStyleTags) { - results.css.push(styleData); - } - if (options.removeStyleTags) { - if (options.preserveMediaQueries) { - mediaQueries = mediaQueryText(element.childNodes[0].nodeValue); - element.childNodes[0].nodeValue = mediaQueries; - } - if (!mediaQueries) { - $(element).remove(); - } - } - }); + $('style').each((index, element) => processStyle($(element), { options, results, decodeEntities })); results.html = decodeEntities($.html()); - callback(null, results); + return results; +}; + +module.exports = (html, options, callback) => { + let result; + + try { + result = extract(html, options); + } catch (cause) { + callback(cause); + return; + } + callback(null, result); }; diff --git a/packages/style-data/package.json b/packages/style-data/package.json index 90076eb..e5b65f1 100644 --- a/packages/style-data/package.json +++ b/packages/style-data/package.json @@ -1,10 +1,11 @@ { "name": "style-data", - "version": "2.0.2", + "version": "3.0.0-rc.1", "description": "Get the content of style tags.", "main": "index.js", "files": [ - "index.js" + "index.js", + "lib/internal/" ], "repository": "https://github.com/jonkemp/inline-css/tree/master/packages/style-data", "keywords": [ @@ -17,8 +18,14 @@ "license": "MIT", "homepage": "https://github.com/jonkemp/inline-css/tree/master/packages/style-data", "dependencies": { - "cheerio": "^1.0.0", - "mediaquery-text": "^1.2.0", + "cheerio": "^1.2.0", + "mediaquery-text": "^2.0.0-rc.1", "pick-util": "^1.1.5" + }, + "engines": { + "node": ">=22.12.0" + }, + "scripts": { + "prepack": "node ../../scripts/build-internals.js style-data" } } diff --git a/packages/style-data/test/main.js b/packages/style-data/test/main.js index bdcd960..45c02f6 100644 --- a/packages/style-data/test/main.js +++ b/packages/style-data/test/main.js @@ -145,3 +145,24 @@ describe('style-data', () => { ); }); }); + +describe('style-data 5.0 callbacks and conditions', () => { + it('reports invalid delimiters through its callback once', done => { + let calls = 0; + require('../')('x
', { codeBlocks: { X: { start: '', end: ']' } } }, err => { + calls++; + require('node:assert/strict').equal(err.code, 'ERR_INLINE_CSS_INVALID_OPTIONS'); + setImmediate(() => { require('node:assert/strict').equal(calls, 1); done(); }); + }); + }); + it('keeps media-qualified CSS conditional for standalone consumers', done => { + require('../')('', { applyStyleTags: true, removeStyleTags: true, preserveMediaQueries: true }, (err, result) => { + if (err) return done(err); + try { + require('node:assert/strict').match(result.css[0], /@media print/); + require('node:assert/strict').match(result.html, /@media print/); + done(); + } catch (e) { done(e); } + }); + }); +}); diff --git a/scripts/audit-release.js b/scripts/audit-release.js new file mode 100644 index 0000000..96e19f4 --- /dev/null +++ b/scripts/audit-release.js @@ -0,0 +1,25 @@ +const { spawnSync } = require('node:child_process'); +const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'; +let failed = false; + +for (const production of [ false, true ]) { + const args = [ 'audit', '--json', ...(production ? [ '--omit=dev' ] : []) ]; + const result = spawnSync(npm, args, { encoding: 'utf8', shell: process.platform === 'win32', maxBuffer: 10 * 1024 * 1024 }); + let report; + + try { + report = JSON.parse(result.stdout); + } catch (cause) { + throw new Error('Dependency audit did not return usable JSON', { cause }); + } + if (!report.metadata || report.error || result.error) { + throw new Error(`Dependency audit unavailable: ${report.error?.message || result.error || result.stderr}`); + } + const counts = report.metadata.vulnerabilities; + + console.log(`${production ? 'Production' : 'All'} dependency audit: ${JSON.stringify(counts)}`); + if (counts.high || counts.critical || (production && counts.total)) { + failed = true; + } +} +process.exitCode = failed ? 1 : 0; diff --git a/scripts/build-internals.js b/scripts/build-internals.js new file mode 100644 index 0000000..7efdad4 --- /dev/null +++ b/scripts/build-internals.js @@ -0,0 +1,31 @@ +const fs = require('node:fs'); +const path = require('node:path'); + +const root = path.resolve(__dirname, '..'); +const consumers = { + 'inline-css': [ 'resource-policy', 'template-blocks' ], + 'extract-css': [ 'resource-policy', 'template-blocks' ], + 'href-content': [ 'resource-policy' ], + 'remote-content': [ 'resource-policy' ], + 'list-stylesheets': [ 'template-blocks' ], + 'style-data': [ 'template-blocks' ] +}; + +const buildConsumer = name => { + if (!Object.hasOwn(consumers, name)) { + throw new Error(`Unknown internal-helper consumer: ${name}`); + } + const dir = name === 'inline-css' ? root : path.join(root, 'packages', name); + const output = path.join(dir, 'lib', 'internal'); + + fs.mkdirSync(output, { recursive: true }); + for (const helper of consumers[name]) { + fs.copyFileSync(path.join(root, 'internal', `${helper}.js`), path.join(output, `${helper}.js`)); + } +}; +const build = (selected = Object.keys(consumers)) => selected.forEach(buildConsumer); + +module.exports = build; +if (require.main === module) { + build(process.argv.length > 2 ? process.argv.slice(2) : undefined); +} diff --git a/scripts/publish-release.js b/scripts/publish-release.js new file mode 100644 index 0000000..c621739 --- /dev/null +++ b/scripts/publish-release.js @@ -0,0 +1,22 @@ +const { execFileSync } = require('node:child_process'); +const { version } = require('../package.json'); +const preState = require('../.changeset/pre.json'); +const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'; +const commands = [ + [ 'exec', '--', 'changeset', 'publish' ], + [ 'publish', '--tag', 'next' ] +]; + +if (!/^\d+\.\d+\.\d+-rc\.\d+$/.test(version) || preState.mode !== 'pre' || preState.tag !== 'rc') { + throw new Error('This command publishes release candidates only; stable publication requires a separate reviewed promotion.'); +} + +// Changesets forbids --tag in prerelease mode: companions use preState.tag (rc). +// The separately versioned root is published under next for consumer RC installs. +for (const args of commands) { + if (process.argv.includes('--dry-run')) { + console.log(`${npm} ${args.join(' ')}`); + } else { + execFileSync(npm, args, { stdio: 'inherit', shell: process.platform === 'win32' }); + } +} diff --git a/scripts/verify-packed-release.js b/scripts/verify-packed-release.js new file mode 100644 index 0000000..64f9386 --- /dev/null +++ b/scripts/verify-packed-release.js @@ -0,0 +1,126 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const http = require('node:http'); +const { spawn, execFileSync } = require('node:child_process'); + +const root = path.resolve(__dirname, '..'); + +require('./build-internals')(); +const scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'inline-css-packed-')); +const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'; +const run = (command, args, cwd) => new Promise((resolve, reject) => { + // npm.cmd needs a shell on Windows; Node's -e programs must stay opaque arguments. + const child = spawn(command, args, { cwd, shell: process.platform === 'win32' && command === npm, stdio: [ 'ignore', 'pipe', 'pipe' ] }); + let output = ''; + + child.stdout.on('data', data => { + output += data; + }); + child.stderr.on('data', data => { + output += data; + }); + child.on('error', reject); + child.on('exit', code => code ? reject(new Error(`${command} exited ${code}\n${output.slice(-5000)}`)) : resolve(output)); +}); + +const closeRegistry = async server => { + if (server) { + server.closeAllConnections(); + await new Promise(resolve => server.close(resolve)); + } +}; + +(async () => { + let server; + + try { + const dirs = [ root, ...fs.readdirSync(path.join(root, 'packages')).map(name => path.join(root, 'packages', name)) ]; + const packages = new Map(); + + for (const dir of dirs) { + const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')); + const result = JSON.parse(execFileSync(npm, [ 'pack', '--json', '--ignore-scripts', '--pack-destination', scratch ], { cwd: dir, encoding: 'utf8', shell: process.platform === 'win32' }))[0]; + + assert.ok(result.files.some(file => file.path === manifest.main), `${manifest.name}: missing entry point`); + assert.ok(result.files.some(file => file.path === 'LICENSE'), `${manifest.name}: missing license`); + assert.ok(!result.files.some(file => /^(test|node_modules|coverage)\//.test(file.path)), `${manifest.name}: unexpected packed files`); + packages.set(manifest.name, { manifest, result }); + } + server = http.createServer((req, res) => { + const pathname = decodeURIComponent(new URL(req.url, 'http://registry.test').pathname).slice(1); + const item = packages.get(pathname); + + if (item) { + const manifest = { ...item.manifest, dist: { tarball: `http://127.0.0.1:${server.address().port}/tarballs/${item.result.filename}`, integrity: item.result.integrity } }; + + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify({ name: manifest.name, 'dist-tags': { latest: manifest.version }, versions: { [manifest.version]: manifest } })); + } else if (pathname.startsWith('tarballs/')) { + const filename = path.basename(pathname); + + if (![ ...packages.values() ].some(pkg => pkg.result.filename === filename)) { + res.writeHead(404).end(); + return; + } + fs.createReadStream(path.join(scratch, filename)).pipe(res); + } else { + res.writeHead(307, { Location: `https://registry.npmjs.org/${pathname}` }).end(); + } + }); + await new Promise((resolve, reject) => { + server.once('error', reject); + server.listen(0, '127.0.0.1', resolve); + }); + const consumer = path.join(scratch, 'consumer'); + + fs.mkdirSync(consumer); + fs.writeFileSync(path.join(consumer, 'package.json'), JSON.stringify({ name: 'release-consumer', private: true, version: '1.0.0' })); + const registry = `http://127.0.0.1:${server.address().port}`; + const rootPackage = packages.get('inline-css'); + + // No overrides, workspace links, or pre-existing consumer lockfile: resolve published ranges. + await run(npm, [ 'install', path.join(scratch, rootPackage.result.filename), '--ignore-scripts', '--engine-strict', '--no-audit', '--no-fund', '--registry', registry, '--cache', path.join(os.tmpdir(), 'inline-css-npm-cache') ], consumer); + for (const [ name, item ] of packages) { + const installed = path.join(consumer, 'node_modules', name); + + assert.ok(!fs.lstatSync(installed).isSymbolicLink(), `${name}: workspace symlink leaked`); + assert.equal(JSON.parse(fs.readFileSync(path.join(installed, 'package.json'))).version, item.manifest.version); + } + process.stdout.write(await run(process.execPath, [ '-e', `${JSON.stringify([ ...packages.keys() ])}.forEach(name => require(name)); console.log('All standalone packed entry points load.');` ], consumer)); + const smoke = ` + const assert=require('node:assert/strict'),fs=require('node:fs'),path=require('node:path'),{pathToFileURL}=require('node:url'); + const inline=require('inline-css'); + (async()=>{ + const options={url:'https://example.test/'}; + for(const name of ['resource-policy','inline-css-template-blocks']) assert.throws(()=>require.resolve(name),{code:'MODULE_NOT_FOUND'}); + assert.match(await inline('x
',options),/color: red/); + const file=path.join(process.cwd(),'style.css');fs.writeFileSync(file,'p{color:blue}@media print{p{color:red}}'); + const html='x
'; + await assert.rejects(inline(html,options),{code:'ERR_INLINE_CSS_RESOURCE_DENIED'}); + assert.match(await inline(html,{...options,preserveMediaQueries:true,resourcePolicy:{fileRoot:process.cwd()}}),/@media print/); + const bytes=fs.statSync(file).size; + await assert.rejects(inline(html+html,{...options,resourcePolicy:{fileRoot:process.cwd(),maxTotalBytes:bytes*2-1}}),{code:'ERR_INLINE_CSS_RESOURCE_LIMIT'}); + assert.match(await inline('EXCS_CODE_BLOCK_0_ [[v]] {{x}}
',{...options,codeBlocks:{X:{start:'[[',end:']]'}}}),/EXCS_CODE_BLOCK_0_ \\[\\[v\\]\\]/); + assert.match(await inline('x
',options),/color: blue/); + await assert.rejects(inline('x
',{...options,removeHtmlSelectors:true}),{code:'ERR_INLINE_CSS_SELECTOR_RETENTION_CONFLICT'}); + console.log('Packed consumer smoke passed on '+process.version); + })().catch(e=>{console.error(e);process.exitCode=1}); + `; + + fs.writeFileSync(path.join(consumer, 'smoke.cjs'), smoke); + process.stdout.write(await run(process.execPath, [ 'smoke.cjs' ], consumer)); + console.log(`Verified ${packages.size} tarballs and no-lockfile semver resolution through a local registry.`); + if (process.env.INLINE_CSS_KEEP_CONSUMER) { + console.log(`Retained consumer: ${consumer}`); + } + } finally { + await closeRegistry(server); + if (!process.env.INLINE_CSS_KEEP_CONSUMER) { + fs.rmSync(scratch, { recursive: true, force: true }); + } + } +})().catch(cause => { + console.error(cause); process.exitCode = 1; +}); diff --git a/test/browser.test.js b/test/browser.test.js new file mode 100644 index 0000000..586cbe4 --- /dev/null +++ b/test/browser.test.js @@ -0,0 +1,27 @@ +/* eslint-disable */ +const inline=require('../'); +const browser=require('./helpers/browser'); +describe('browser cascade semantics',function(){ + this.timeout(15000); + before(()=>browser.start()); + after(()=>browser.close()); + for(const css of [ + 'p{margin-left:10px;margin:0}', + 'p{padding-bottom:10px;padding:0}', + 'p{border-left:10px solid red;border:0}', + '#x{margin-left:10px}p{margin:0}', + 'p{margin:0!important;margin-left:10px}', + 'p{font-family:"A\'B"}', + 'p{display:-webkit-box;display:flex}', + 'p{--color:red;color:var(--color)}', + ':where(#x){color:red}p{color:blue}', + ':is(#x,.a){color:red}p{color:blue}', + 'p{background-color:red;background:blue}', + 'p{font-size:30px;font:12px sans-serif}' + ]){ + it(`matches browser cascade for ${css}`,async()=>{ + const html=`hello
`; + await browser.compare(await inline(html,{url:'https://example.test/'}),html); + }); + } +}); diff --git a/test/helpers/browser.js b/test/helpers/browser.js new file mode 100644 index 0000000..cc8cf07 --- /dev/null +++ b/test/helpers/browser.js @@ -0,0 +1,42 @@ +/* eslint-disable */ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const { chromium } = require('playwright'); +let browser; +exports.start = async () => { + const localChrome = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; + const executablePath = process.env.INLINE_CSS_BROWSER || (fs.existsSync(localChrome) ? localChrome : undefined); + browser = await chromium.launch({ executablePath, headless: true }); +}; +exports.close = async () => { if (browser) await browser.close(); }; +exports.compare = async (actual, expected) => { + const page = await browser.newPage(); + try { + await page.route('**/*', route => route.abort()); + async function snapshot(html) { + await page.setContent(html); + return page.evaluate(() => { + const elements = [...document.querySelectorAll('*')].filter(el => !['STYLE','HEAD','HTML','SCRIPT','META','LINK','TITLE'].includes(el.tagName)); + return elements.map(el => ({ + tag: el.tagName, + attributes: [...el.attributes].filter(a=>a.name!=='style').map(a=>[a.name,a.value]).sort(), + text: el.childElementCount ? '' : el.textContent.replace(/\s+/g,' ').trim(), + styles: Object.fromEntries([...getComputedStyle(el)].filter(p=>!p.startsWith('animation')&&!p.startsWith('transition')).map(p=>[p,getComputedStyle(el).getPropertyValue(p)])) + })); + }); + } + const want=await snapshot(expected), got=await snapshot(actual); + assert.equal(got.length,want.length,'rendered element count differs'); + for(let i=0;i${start}a\nb${end} {{default}} EXCS_CODE_BLOCK_0_
`; + const protectedHtml = protect(html, { X: { start, end } }); + assert.equal(protectedHtml.restore(protectedHtml.html), html); + assert.ok(!protectedHtml.html.includes('{{default}}')); + }); + } + it('rejects empty delimiters', () => assert.throws(() => protect('x', { X: { start: '', end: ']' } }), { code: 'ERR_INLINE_CSS_INVALID_OPTIONS' })); + it('prefers the longest opening delimiter for overlaps', () => { + const result = protect('[[x]]', { A: { start: '[', end: ']' }, B: { start: '[[', end: ']]' } }); + assert.ok(!result.html.includes(']')); + assert.equal(result.restore(result.html), '[[x]]'); + }); + it('leaves unmatched openers unchanged without hiding other blocks', () => { + const result = protect('[[ {{x}}', { X: { start: '[[', end: ']]' } }); + assert.equal(result.restore(result.html), '[[ {{x}}'); + }); + it('does not mutate caller blocks', () => { + const input = Object.freeze({ X: Object.freeze({ start: '[[', end: ']]' }) }); + assert.equal(protect('[[x]]', input).restore(protect('x').html), 'x'); + assert.deepEqual(Object.keys(input), ['X']); + }); +}); diff --git a/test/main.js b/test/main.js index 0475abb..6eaaad8 100644 --- a/test/main.js +++ b/test/main.js @@ -7,6 +7,7 @@ const path = require('path'); const Vinyl = require('vinyl'); const beautify = require('js-beautify').html; const inlineCss = require('../index'); +const browser = require('./helpers/browser'); function getFile(filePath) { return new Vinyl({ @@ -21,15 +22,12 @@ function compare(fixturePath, expectedPath, options, done) { const file = getFile(fixturePath); options.url = `file://${file.path}`; + options.resourcePolicy = { fileRoot: path.resolve("test/fixtures") }; inlineCss(file.contents.toString('utf8'), options) - .then(html => { - const expected = beautify(String(fs.readFileSync(expectedPath)), { - "preserve-newlines": false - }); - beautify(html, { - "preserve-newlines": false - }).should.be.equal(expected); + .then(async html => { + const expected = String(fs.readFileSync(expectedPath)); + await browser.compare(html, expected); }) .then(() => { done() @@ -39,7 +37,10 @@ function compare(fixturePath, expectedPath, options, done) { }); } -describe('inline-css', () => { +describe('inline-css', function () { + this.timeout(15000); + before(async () => { await browser.start(); }); + after(async () => { await browser.close(); }); it('Should convert linked css to inline css', done => { const options = {}; compare(path.join('test', 'fixtures', 'in.html'), path.join('test', 'expected', 'out.html'), options, done); @@ -161,10 +162,15 @@ describe('inline-css', () => { compare(path.join('test', 'fixtures', 'no_css.html'), path.join('test', 'expected', 'no_css.html'), options, done); }); - it('Should inline css with remote url', function(done) { - this.timeout(10000); - const options = {}; - compare(path.join('test', 'fixtures', 'remote_url.html'), path.join('test', 'expected', 'remote_url.html'), options, done); + it('Should inline css with remote url', async () => { + const http = require('node:http'); + const server = http.createServer((req, res) => res.end('h1{color:red}')); + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); + const origin = `http://127.0.0.1:${server.address().port}`; + try { + const html = await inlineCss(`x
`)); + assert.equal($('p').css('color'),'red'); + }); + } + for(const media of ['print','screen','(max-width:600px)']){ + it(`omits removed conditional ${media} without preservation`,async()=>{ + const $=cheerio.load(await run(`x
`)); + assert.equal($('p').attr('style'),undefined); + assert.equal($('style').length,0); + }); + } + for(const html of ['x
','x
']){ + it('rejects cleanup for retained CSS even with fetching disabled',async()=>{ + await assert.rejects(run(html,{removeHtmlSelectors:true,applyLinkTags:false,removeLinkTags:false}),{code:'ERR_INLINE_CSS_SELECTOR_RETENTION_CONFLICT'}); + }); + } + it('allows cleanup with retained empty styles',async()=>{ + assert.match(await run('x
',{removeStyleTags:false,removeHtmlSelectors:true}),/x/); + }); + it('does not duplicate preserved media from a retained style tag',async()=>{ + const $=cheerio.load(await run('
x
',{removeStyleTags:false,preserveMediaQueries:true})); + assert.equal($('style').length,1); + assert.equal(($('style').text().match(/@media/g)||[]).length,1); + }); + it('keeps both media attribute and contained media conditions',async()=>{ + const $=cheerio.load(await run('x
',{preserveMediaQueries:true})); + assert.match($('style').text(),/@media print/); + assert.match($('style').text(),/@media\s*\(min-width:1px\)/); + assert.equal($('p').attr('style'),undefined); + }); + it('preserves existing embedded media even when style application is disabled', async () => { + const $=cheerio.load(await run('x
', {applyStyleTags:false,preserveMediaQueries:true})); + assert.match($('style').text(), /@media print/); + assert.equal($('p').attr('style'), undefined); + }); + it('keeps template attribute blocks and literal token text across stages',async()=>{ + const result=await run('EXCS_CODE_BLOCK_0_ [[v]]
',{codeBlocks:{X:{start:'[[',end:']]'}}}); + assert.match(result,/\{\{#if x\}\}/); + assert.match(result,/EXCS_CODE_BLOCK_0_ \[\[v\]\]/); + }); + it('tolerates unknown keys but rejects invalid recognized keys',async()=>{ + assert.match(await run('x
',{wrapperMetadata:'test'}),/x/); + await assert.rejects(run('
x
',{applyLinkTags:'false'}),{code:'ERR_INLINE_CSS_INVALID_OPTIONS'}); + await assert.rejects(inline('x
',{}),{code:'ERR_INLINE_CSS_INVALID_OPTIONS'}); + }); + for (const selector of ['p:nth-child(foo)', 'p:nth-child()', 'p:nth-of-type(2n+)']) { + it(`skips known invalid selector ${selector} without hiding valid rules`, async () => { + const $ = cheerio.load(await run(`x
`)); + assert.equal($('p').css('color'), 'blue'); + }); + } + it('propagates inline CSS parse failures instead of treating them as selectors', async () => { + await assert.rejects(run('x
'), { name: 'CssSyntaxError' }); + }); + it('standalone list extraction uses literal template protection',()=>{ + const result=list('EXCS_CODE_BLOCK_0_ [[v]]
',{codeBlocks:{X:{start:'[[',end:']]'}}}); + assert.match(result.html,/EXCS_CODE_BLOCK_0_ \[\[v\]\]/); + }); + it('standalone style extraction settles once for empty styles',async()=>{ + let calls=0; + await new Promise((resolve,reject)=>styles('x
',{removeStyleTags:true},(err,result)=>{ + calls++; + if(err) reject(err);else {try{assert.match(result.html,/x/);resolve();}catch(e){reject(e);}} + })); + assert.equal(calls,1); + }); +}); diff --git a/test/release-5.test.js b/test/release-5.test.js new file mode 100644 index 0000000..ce371af --- /dev/null +++ b/test/release-5.test.js @@ -0,0 +1,113 @@ +/* eslint-disable */ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const os = require('node:os'); +const path = require('node:path'); +const { pathToFileURL } = require('node:url'); +const cheerio = require('cheerio'); +const inline = require('../'); + +describe('5.0 release regressions', function () { + let dir; + before(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'inline-css-regressions-')); }); + after(() => fs.rmSync(dir, { recursive: true, force: true })); + const options = extra => ({ url: 'https://example.test/email.html', ...extra }); + const run = (html, extra) => inline(html, options(extra)); + const dom = html => cheerio.load(html); + function sheet(css, filename = 'style.css') { + const file = path.join(dir, filename); + fs.writeFileSync(file, css); + return pathToFileURL(file).href; + } + const permitted = () => ({ resourcePolicy: { fileRoot: dir } }); + it('1 denies unpermitted local stylesheet reads', async () => { + await assert.rejects(run(`
x
`), { code: 'ERR_INLINE_CSS_RESOURCE_DENIED' }); + }); + it('3 enforces the per-stylesheet decoded byte limit', async () => { + await assert.rejects(run(``, { resourcePolicy: { fileRoot: dir, maxStylesheetBytes: 4 } }), { code: 'ERR_INLINE_CSS_RESOURCE_LIMIT' }); + }); + it('3 rejects excessive stylesheet load counts', async () => { + const href = sheet('p{color:red}'); + await assert.rejects(run(``, { resourcePolicy: { fileRoot: dir, maxStylesheets: 1 } }), { code: 'ERR_INLINE_CSS_RESOURCE_LIMIT' }); + }); + it('4 retains important selector specificity', async () => { + const $ = dom(await run('x
')); + assert.equal($('p').css('color'), 'red !important'); + }); + it('4 inline important beats stylesheet important', async () => { + const $ = dom(await run('x
')); + assert.equal($('p').css('color'), 'red !important'); + }); + it('5 later shorthand resets an earlier longhand', async () => { + const $ = dom(await run('x
')); + const style = $('p').attr('style'); + assert.ok(!style.includes('margin-left') || style.indexOf('margin-left') < style.lastIndexOf('margin:')); + }); + it('5 preserves duplicate fallback declarations', async () => { + const $ = dom(await run('x
')); + assert.match($('p').attr('style'), /display: -webkit-box;.*display: flex;/); + }); + it('6 linked and embedded styles retain document order', async () => { + const $ = dom(await run(`x
`, permitted())); + assert.equal($('p').css('color'), 'blue'); + }); + it('8 specificity ten classes beats two classes', async () => { + const $ = dom(await run('x
')); + assert.equal($('p').css('color'), 'red'); + }); + it('9 accepts empty style elements between nonempty styles', async () => { + const $ = dom(await run('x
')); + assert.equal($('p').css('color'), 'red'); + }); + it('10 selector lists do not inherit another branch specificity', async () => { + const $ = dom(await run('x
')); + assert.equal($('p').css('color'), 'blue'); + }); + it('B11 style media remains conditional', async () => { + const $ = dom(await run('x
', { preserveMediaQueries: true })); + assert.equal($('p').attr('style'), undefined); + assert.match($('style').text(), /@media print/); + }); + it('B11 linked media remains conditional', async () => { + const $ = dom(await run(`x
`, { ...permitted(), preserveMediaQueries: true })); + assert.equal($('p').attr('style'), undefined); + assert.match($('style').text(), /@media print/); + }); + it('B12 retains linked media alongside unconditional CSS', async () => { + const $ = dom(await run(`x
`, { ...permitted(), preserveMediaQueries: true })); + assert.equal($('p').css('color'), 'red'); + assert.match($('style').text(), /max-width:600px/); + assert.match($('style').text(), /color:blue|color: blue/); + }); + it('B12 retains extraCss media', async () => { + const $ = dom(await run('x
', { extraCss: '@media print{p{color:red}}', preserveMediaQueries: true })); + assert.match($('style').text(), /@media print/); + assert.equal($('p').attr('style'), undefined); + }); + it('B14 rejects selector stripping when CSS is retained', async () => { + await assert.rejects(run('x
', { preserveMediaQueries: true, removeHtmlSelectors: true }), { code: 'ERR_INLINE_CSS_SELECTOR_RETENTION_CONFLICT' }); + }); + it('B14 allows selector stripping without retained CSS', async () => { + const $ = dom(await run('x
', { removeHtmlSelectors: true })); + assert.equal($('p').attr('class'), undefined); + assert.equal($('p').css('color'), 'red'); + }); + it('B16 treats custom delimiters literally', async () => { + const result = await run('[[ value ]] {{default}}
', { codeBlocks: { X: { start: '[[', end: ']]' } } }); + assert.match(result, /\[\[ value \]\] \{\{default\}\}/); + }); + it('B17 preserves literal placeholder-shaped input', async () => { + const $ = dom(await run('EXCS_CODE_BLOCK_0_ {{value}}
')); + assert.equal($('p').text(), 'EXCS_CODE_BLOCK_0_ {{value}}'); + }); + for (const property of ['constructor', 'toString', '__proto__']) { + it(`B18 ${property} does not suppress valid declarations`, async () => { + const $ = dom(await run(`x
`)); + assert.equal($('p').css('color'), 'blue'); + assert.equal({}.polluted, undefined); + }); + } + it('invalid known options reject as Error objects before I/O', async () => { + await assert.rejects(run('x
', { resourcePolicy: { timeoutMs: 0 } }), { code: 'ERR_INLINE_CSS_INVALID_OPTIONS' }); + }); +}); diff --git a/test/release-command.test.js b/test/release-command.test.js new file mode 100644 index 0000000..84517c8 --- /dev/null +++ b/test/release-command.test.js @@ -0,0 +1,20 @@ +/* eslint-disable */ +const assert=require('node:assert/strict'); +const fs=require('node:fs'); +const path=require('node:path'); +const os=require('node:os'); +const {spawnSync}=require('node:child_process'); +describe('release publication orchestration',function(){ + this.timeout(10000); + (process.platform==='win32'?it.skip:it)('publishes workspaces using prerelease state, then the root under next',()=>{ + const dir=fs.mkdtempSync(path.join(os.tmpdir(),'inline-publish-')); + try{ + const log=path.join(dir,'commands.jsonl'); + const fakeNpm=path.join(dir,'npm'); + fs.writeFileSync(fakeNpm,'#!/usr/bin/env node\nrequire("node:fs").appendFileSync(process.env.INLINE_PUBLISH_LOG,JSON.stringify(process.argv.slice(2))+"\\n");\n',{mode:0o755}); + const result=spawnSync(process.execPath,['scripts/publish-release.js'],{cwd:path.resolve(__dirname,'..'),encoding:'utf8',env:{...process.env,PATH:dir+path.delimiter+process.env.PATH,INLINE_PUBLISH_LOG:log}}); + assert.equal(result.status,0,result.stderr); + assert.deepEqual(fs.readFileSync(log,'utf8').trim().split('\n').map(line=>JSON.parse(line)),[['exec','--','changeset','publish'],['publish','--tag','next']]); + }finally{fs.rmSync(dir,{recursive:true,force:true});} + }); +}); diff --git a/test/release-resources.test.js b/test/release-resources.test.js new file mode 100644 index 0000000..e90d83c --- /dev/null +++ b/test/release-resources.test.js @@ -0,0 +1,144 @@ +/* eslint-disable */ +const assert = require('node:assert/strict'); +const http = require('node:http'); +const zlib = require('node:zlib'); +const dns = require('node:dns'); +const inline = require('../'); +const remote = require('remote-content'); +const policyModule = require('../internal/resource-policy'); + +describe('5.0 bounded resource integration', function () { + this.timeout(5000); + let server, origin, active, peak, calls, proxyCalls; + before(async () => { + server = http.createServer((req, res) => { + calls.push(req.url); + if (req.url.startsWith('http')) { proxyCalls++; res.end('p{color:purple}'); return; } + if (req.url === '/redirect') { res.writeHead(302, { Location: '/plain' }); res.end(); return; } + if (req.url === '/off-origin') { res.writeHead(302, { Location: 'http://localhost:1/private' }); res.end(); return; } + if (req.url === '/loop') { res.writeHead(302, { Location: '/loop' }); res.end(); return; } + if (req.url === '/compressed') { res.writeHead(200, { 'Content-Encoding': 'gzip' }); res.end(zlib.gzipSync('p{color:red}' + ' '.repeat(4096))); return; } + if (req.url === '/headers') return; + if (req.url === '/stall') { res.writeHead(200); res.write('p{'); return; } + if (req.url === '/empty-reason') { res.writeHead(200, ''); res.end('p{color:red}'); return; } + if (req.url.startsWith('/delayed')) { + active++; peak = Math.max(active, peak); + setTimeout(() => { active--; res.end('p{color:red}'); }, 35); + return; + } + if (req.url === '/blue') { res.end('p{color:blue}'); return; } + if (req.url === '/credential-check' && req.headers.authorization) { res.end('p{color:purple}'); return; } + res.end('p{color:red}'); + }); + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); + origin = `http://127.0.0.1:${server.address().port}`; + }); + after(async () => { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); }); + beforeEach(() => { active = 0; peak = 0; calls = []; proxyCalls = 0; }); + const opts = changes => ({ url: 'https://example.test/email', resourcePolicy: { allowedOrigins: [origin], allowPrivateNetworks: true, ...changes } }); + const html = (...urls) => urls.map(url => ``).join('') + 'x
'; + it('allows authorized loading and numeric 200 without reason phrase', async () => { + assert.match(await inline(html('/empty-reason'), opts()), /color: red/); + }); + it('absolute hrefs work even with a relative documented base', async () => { + assert.match(await inline(html('/plain'), { ...opts(), url: './' }), /color: red/); + }); + it('redirects must remain authorized', async () => { + await assert.rejects(inline(html('/off-origin'), opts()), { code: 'ERR_INLINE_CSS_RESOURCE_DENIED' }); + assert.deepEqual(calls, ['/off-origin']); + }); + it('accepts a redirect at the exact budget', async () => assert.match(await inline(html('/redirect'), opts({ maxRedirects: 1 })), /color: red/)); + it('rejects the next redirect beyond the budget', async () => { + await assert.rejects(inline(html('/loop'), opts({ maxRedirects: 1 })), { code: 'ERR_INLINE_CSS_RESOURCE_LIMIT' }); + assert.equal(calls.length, 2); + }); + it('enforces decoded bytes on compressed responses', async () => { + await assert.rejects(inline(html('/compressed'), opts({ maxStylesheetBytes: 1024 })), { code: 'ERR_INLINE_CSS_RESOURCE_LIMIT' }); + }); + it('accepts the exact byte boundary and rejects one byte over', async () => { + assert.match(await inline(html('/plain'), opts({ maxStylesheetBytes: 12 })), /red/); + await assert.rejects(inline(html('/plain'), opts({ maxStylesheetBytes: 11 })), { code: 'ERR_INLINE_CSS_RESOURCE_LIMIT' }); + }); + it('enforces a shared aggregate byte budget', async () => { + await assert.rejects(inline(html('/plain', '/plain'), opts({ maxTotalBytes: 23 })), { code: 'ERR_INLINE_CSS_RESOURCE_LIMIT' }); + assert.match(await inline(html('/plain', '/plain'), opts({ maxTotalBytes: 24 })), /red/); + }); + for (const route of ['/headers', '/stall']) { + it(`times out ${route} with a stable reason`, async () => { + await assert.rejects(inline(html(route), opts({ timeoutMs: 50 })), { code: 'ERR_INLINE_CSS_RESOURCE_TIMEOUT' }); + }); + } + it('bounds concurrent loads and preserves all queued work', async () => { + await inline(html('/delayed1', '/delayed2', '/delayed3', '/delayed4'), opts({ concurrency: 2 })); + assert.equal(calls.length, 4); + assert.equal(peak, 2); + }); + it('completion order cannot reverse stylesheet source order', async () => { + const result = await inline(html('/delayed-red', '/blue'), opts()); + const $ = require('cheerio').load(result); + assert.equal($('p').css('color'), 'blue'); + }); + it('default load-count boundary accepts 32 and rejects 33 without I/O', async () => { + await inline(html(...Array(32).fill('/plain')), opts()); + assert.equal(calls.length, 32); + calls = []; + await assert.rejects(inline(html(...Array(33).fill('/plain')), opts()), { code: 'ERR_INLINE_CSS_RESOURCE_LIMIT' }); + assert.equal(calls.length, 0); + }); + it('deadline includes queue time, not a fresh timeout for each request', async () => { + await assert.rejects(inline(html('/delayed1', '/delayed2', '/delayed3'), opts({ concurrency: 1, timeoutMs: 50 })), { code: 'ERR_INLINE_CSS_RESOURCE_TIMEOUT' }); + assert.ok(calls.length < 3); + }); + it('first failure cancels queued work', async () => { + await assert.rejects(inline(html('/off-origin', '/plain', '/plain'), opts({ concurrency: 1 })), { code: 'ERR_INLINE_CSS_RESOURCE_DENIED' }); + assert.deepEqual(calls, ['/off-origin']); + }); + it('disabled fetching does not authorize or load links', async () => { + await inline(html('/plain'), { url: origin, applyLinkTags: false }); + assert.equal(calls.length, 0); + }); + it('standalone loader denies access and invokes its callback once', async () => { + let count = 0; + await new Promise((resolve, reject) => remote(`${origin}/plain`, {}, err => { + count++; + try { assert.equal(err.code, 'ERR_INLINE_CSS_RESOURCE_DENIED'); resolve(); } catch (e) { reject(e); } + })); + await new Promise(resolve => setTimeout(resolve, 10)); + assert.equal(count, 1); + assert.equal(calls.length, 0); + }); + it('proxy environment cannot intercept the direct request', async () => { + const keys = ['HTTP_PROXY','HTTPS_PROXY','ALL_PROXY','NO_PROXY','http_proxy','https_proxy','all_proxy','no_proxy']; + const saved = keys.map(key => process.env[key]); + try { + keys.forEach(key => { process.env[key] = key.toLowerCase() === 'no_proxy' ? '' : origin; }); + await inline(html('/plain'), opts()); + assert.equal(proxyCalls, 0); + assert.deepEqual(calls, ['/plain']); + assert.equal(process.env.HTTP_PROXY, origin); + } finally { keys.forEach((key, index) => { if (saved[index] === undefined) delete process.env[key]; else process.env[key] = saved[index]; }); } + }); + it('does not inherit global Axios authentication defaults', async () => { + const axios = require('axios'); + const previous = axios.defaults.headers.common.Authorization; + try { + axios.defaults.headers.common.Authorization = 'Bearer unrelated-application-secret'; + const result = await inline(html('/credential-check'), opts()); + assert.ok(!result.includes('purple')); + assert.match(result, /red/); + } finally { + if (previous === undefined) delete axios.defaults.headers.common.Authorization; + else axios.defaults.headers.common.Authorization = previous; + } + }); + it('checks every DNS answer and returns only validated addresses to the socket', async () => { + const saved = dns.lookup; + try { + dns.lookup = (host, options, callback) => callback(null, [{ address: '93.184.216.34', family: 4 }, { address: '127.0.0.1', family: 4 }]); + await assert.rejects(new Promise((resolve, reject) => policyModule.createLookup(policyModule.normalizePolicy())('test.invalid', { all: true }, (err, value) => err ? reject(err) : resolve(value))), { code: 'ERR_INLINE_CSS_RESOURCE_DENIED' }); + dns.lookup = (host, options, callback) => callback(null, [{ address: '93.184.216.34', family: 4 }]); + const addresses = await new Promise((resolve, reject) => policyModule.createLookup(policyModule.normalizePolicy())('test.invalid', { all: true }, (err, value) => err ? reject(err) : resolve(value))); + assert.deepEqual(addresses, [{ address: '93.184.216.34', family: 4 }]); + } finally { dns.lookup = saved; } + }); +}); diff --git a/test/test-discovery.test.js b/test/test-discovery.test.js new file mode 100644 index 0000000..893566b --- /dev/null +++ b/test/test-discovery.test.js @@ -0,0 +1,17 @@ +/* eslint-disable */ +const assert = require('node:assert/strict'); +const path = require('node:path'); +const { spawnSync } = require('node:child_process'); +describe('cross-platform test discovery', function () { + this.timeout(10000); + it('discovers workspace tests without shell glob expansion', () => { + const script = require('../package.json').scripts.test; + const args = script.match(/"[^"]*"|\S+/g).slice(1).map(arg => arg.replace(/^"|"$/g, '')); + const child = spawnSync(process.execPath, [path.resolve(__dirname, '../node_modules/mocha/bin/mocha.js'), ...args, '--dry-run', '--reporter=json'], {encoding:'utf8', maxBuffer:10*1024*1024}); + assert.equal(child.status, 0, child.stderr); + const titles = JSON.parse(child.stdout).tests.map(test => test.fullTitle); + for (const suite of ['resource policy ', 'template protection ', 'ordered CSS rules ', 'style-data 5.0 callbacks and conditions ']) { + assert.ok(titles.some(title => title.startsWith(suite)), `Missing workspace suite: ${suite}`); + } + }); +});