|
| 1 | +package org.owasp.wrongsecrets.challenges.docker; |
| 2 | + |
| 3 | +import static org.owasp.wrongsecrets.Challenges.ErrorResponses.FILE_MOUNT_ERROR; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.util.List; |
| 10 | +import java.util.regex.Pattern; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.owasp.wrongsecrets.challenges.FixedAnswerChallenge; |
| 13 | +import org.springframework.beans.factory.annotation.Value; |
| 14 | +import org.springframework.stereotype.Component; |
| 15 | + |
| 16 | +/** |
| 17 | + * Challenge about secrets that are committed to AI agent instruction/context files. The dev |
| 18 | + * container ships {@code AGENTS.md}, {@code CLAUDE.md}, {@code .cursor/rules/project.mdc} and |
| 19 | + * {@code .windsurfrules}; one of them inlines a shared credential that every agent (and every |
| 20 | + * reader of the repository) picks up. |
| 21 | + */ |
| 22 | +@Slf4j |
| 23 | +@Component |
| 24 | +public class Challenge73 extends FixedAnswerChallenge { |
| 25 | + |
| 26 | + private static final Pattern TOKEN_PATTERN = Pattern.compile("STAGING_DEPLOY_TOKEN=\"([^\"]+)\""); |
| 27 | + |
| 28 | + private static final List<String> AGENT_CONTEXT_FILES = |
| 29 | + List.of("AGENTS.md", "CLAUDE.md", ".cursor/rules/project.mdc", ".windsurfrules"); |
| 30 | + |
| 31 | + private final String agentContextPath; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constructor for creating a new Challenge73 object. |
| 35 | + * |
| 36 | + * @param agentContextPath directory in the dev container that holds the agent instruction files. |
| 37 | + */ |
| 38 | + public Challenge73(@Value("${AGENT_CONTEXT_PATH}") String agentContextPath) { |
| 39 | + this.agentContextPath = agentContextPath; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getAnswer() { |
| 44 | + var basePath = Path.of(agentContextPath); |
| 45 | + for (var fileName : AGENT_CONTEXT_FILES) { |
| 46 | + var file = basePath.resolve(fileName); |
| 47 | + try { |
| 48 | + var content = Files.readString(file, StandardCharsets.UTF_8); |
| 49 | + var matcher = TOKEN_PATTERN.matcher(content); |
| 50 | + if (matcher.find()) { |
| 51 | + return matcher.group(1); |
| 52 | + } |
| 53 | + } catch (IOException e) { |
| 54 | + log.warn("Could not read agent context file {} of challenge 73", file, e); |
| 55 | + } |
| 56 | + } |
| 57 | + log.warn("Could not find the staging token in the agent context files of challenge 73"); |
| 58 | + return FILE_MOUNT_ERROR; |
| 59 | + } |
| 60 | +} |
0 commit comments