Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Indicate weak validator in ETag header #1711

Merged
merged 1 commit into from
Jan 4, 2025
Merged
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
1 change: 0 additions & 1 deletion packages/web-api-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const DELAY_AFTER = 50

const app = express()
.set('trust proxy', 1)
.set('etag', 'strong')
.use(cors({
exposedHeaders: [
'ETag',
Expand Down
9 changes: 7 additions & 2 deletions packages/web-api-server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ export async function sendGitFile(
path: string,
) {
const hash = (await git.log(['--format=%H', '-1', branch, '--', path])).latest!.hash
res.setHeader('ETag', hash)
// Git commit sha is a [weak validator](https://datatracker.ietf.org/doc/html/rfc2616#section-3.11),
// since the same value is used for different representations of the same resource (e.g. gzip
// compressed v.s. no compression). It can only guarantee semantic equivalence, not byte-for-byte
// equivalence.
res.setHeader('ETag', `W/"${hash}"`)
if (req.headers['if-none-match'] === hash) {
res.status(304).end()
await rateLimiter.reward(req.ip!, CHEAP_REQUEST_POINTS)
Expand All @@ -117,7 +121,8 @@ export async function sendGitTarball(
fileName = branch,
) {
const hash = (await git.log(['--format=%H', '-1', branch])).latest!.hash
res.setHeader('ETag', hash)
// See comments in sendGitFile() for why this is a weak validator.
res.setHeader('ETag', `W/"${hash}"`)
if (req.headers['if-none-match'] === hash) {
res.status(304).end()
await rateLimiter.reward(req.ip!, EXPENSIVE_REQUEST_POINTS)
Expand Down
Loading