Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Fix infinite loop when `theme()` function is called recursively inside `@theme` blocks

## [4.1.6] - 2025-05-09

Expand Down
34 changes: 34 additions & 0 deletions packages/tailwindcss/src/css-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,27 @@ describe('--theme(…)', () => {
}"
`)
})

test('--theme(…) prevents infinite loops with circular references', async () => {
expect(
await compileCss(css`
@theme {
--font-sans: 'Inter', --theme(--font-sans);
}
.font {
font-family: var(--font-sans);
}
`),
).toMatchInlineSnapshot(`
":root, :host {
--font-sans: "Inter", var(--font-sans);
}

.font {
font-family: var(--font-sans);
}"
`)
})
})

describe('theme(…)', () => {
Expand Down Expand Up @@ -903,6 +924,19 @@ describe('theme(…)', () => {
}"
`)
})

test('theme(…) prevents infinite loops with circular references', async () => {
await expect(
compileCss(css`
@theme {
--font-sans: 'Inter', theme(fontFamily.sans);
}
.font {
font-family: var(--font-sans);
}
`),
).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: Could not resolve value for theme function: \`theme(fontFamily.sans)\`. The resolved value \`'Inter', theme(fontFamily.sans)\` contains a recursive reference to itself.]`)
})
})

describe('with CSS variable syntax', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/tailwindcss/src/css-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ function legacyTheme(
)
}

// Detect eventual recursive theme function calls.
let regex = new RegExp('theme\\(\\s*[\'\"]?' + path)
if (typeof resolvedValue === 'string' && resolvedValue.match(regex)) {
throw new Error(
`Could not resolve value for theme function: \`theme(${path})\`. The resolved value \`${resolvedValue}\` contains a recursive reference to itself.`,
)
}

return resolvedValue
}

Expand Down